import 'package:flutter/material.dart'; // ignore: slash_for_doc_comments /** TextView + 容器的使用 内部使用各种容器包裹Text控件,用于一些复杂的UI效果 模板: TextView( "自定义textview自定义textview自定义textview自定义textview自定义textview", backgroundColor: Colors.red, textColor: Colors.white, padding: 10, cornerRadius: 10, borderColor: Colors.yellow, borderWidth: 1, marginTop: 5, singleLine: false, onClick: (){ print("点击事件"); }), */ class MyTextView extends StatelessWidget { double? padding = 0; double? margin = 0; double? paddingLeft = 0; double? paddingRight = 0; double? paddingTop = 0; double? paddingBottom = 0; double? marginLeft = 0; double? marginRight = 0; double? marginTop = 0; double? marginBottom = 0; double? fontSize = 0; Color? textColor = Colors.black; Color? backgroundColor = Colors.transparent; AlignmentGeometry? alignment = Alignment.center; double? cornerRadius = 0; double? borderWidth = 0; Color? borderColor = Colors.transparent; String content = ""; bool? singleLine = false; VoidCallback? onClick; bool? isFontLight; bool? isFontRegular; bool? isFontMedium; bool? isFontBold; FontWeight? fontWeight; TextAlign? textAlign; MyTextView(this.content, {this.textColor, this.backgroundColor, this.padding, this.paddingTop, this.paddingBottom, this.paddingRight, this.paddingLeft, this.cornerRadius, this.borderColor, this.borderWidth, this.marginBottom, this.marginLeft, this.marginRight, this.marginTop, this.margin, this.fontSize, this.singleLine, this.isFontLight, this.isFontRegular, this.isFontMedium, this.isFontBold, this.fontWeight, this.textAlign, this.onClick}) { if (padding != null) { if (padding != null && padding! > 0) { paddingLeft = padding; paddingRight = padding; paddingBottom = padding; paddingTop = padding; } } if (margin != null) { if (margin != null && margin! > 0) { marginLeft = margin; marginTop = margin; marginRight = margin; marginBottom = margin; } } onClick ??= () {}; if (isFontLight != null && isFontLight!) { fontWeight = FontWeight.w300; } else if (isFontRegular != null && isFontRegular!) { fontWeight = FontWeight.w400; } else if (isFontMedium != null && isFontMedium!) { fontWeight = FontWeight.w500; } else if (isFontBold != null && isFontBold!) { fontWeight = FontWeight.w700; } else { fontWeight = FontWeight.normal; } } @override Widget build(BuildContext context) { return Container( margin: EdgeInsets.fromLTRB(marginLeft ?? 0, marginTop ?? 0, marginRight ?? 0, marginBottom ?? 0), decoration: BoxDecoration( border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent), color: backgroundColor, borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)), ), padding: EdgeInsets.fromLTRB(paddingLeft ?? 0, paddingTop ?? 0, paddingRight ?? 0, paddingBottom ?? 0), child: GestureDetector( onTap: onClick, child: Text( content, textAlign: textAlign ?? TextAlign.start, style: TextStyle( color: textColor, fontSize: fontSize ?? 14, fontWeight: fontWeight, overflow: singleLine ?? false ? TextOverflow.ellipsis : TextOverflow.clip, ), ), )); } }