import 'dart:ffi'; import 'dart:ui'; import 'package:flutter/material.dart'; import 'ext/ex_widget.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? fontSize = 0; Color? textColor = Colors.black; Color? textHintColor; String content = ""; String? hint; bool? isTextEllipsis = false; bool? isFontLight; bool? isFontRegular; bool? isFontMedium; bool? isFontBold; FontWeight? fontWeight; TextAlign? textAlign; FontStyle? fontStyle; int? maxLines; TextDecoration? textDecoration; //设置文本的装饰 Color? decorationColor; //文本的装饰颜色 double? decorationThickness; //装饰粗细 TextDecorationStyle? decorationStyle; //装饰样式 //底部是Text的父容器属性 VoidCallback? onClick; double? cornerRadius = 0; double? borderWidth = 0; Color? borderColor = Colors.transparent; double? boxWidth; //限制TextView的宽高,为外部 Container 的属性 double? boxHeight; //限制TextView的宽高,为外部 Container 的属性 AlignmentGeometry? alignment; Color? backgroundColor = Colors.transparent; 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; final ClickType type; //默认没有点击类型 final int milliseconds; //点击类型的时间戳(毫秒) MyTextView( this.content, { Key? key, this.textColor, this.backgroundColor, this.padding, this.paddingTop, this.paddingBottom, this.paddingRight, this.textHintColor, this.hint, this.paddingLeft, this.cornerRadius, this.borderColor, this.borderWidth, this.marginBottom, this.marginLeft, this.marginRight, this.marginTop, this.margin, this.maxLines, this.fontSize, this.textDecoration, this.decorationColor, this.decorationThickness, this.decorationStyle, this.isTextEllipsis, this.isFontLight, this.isFontRegular, this.isFontMedium, this.isFontBold, this.fontWeight, this.textAlign, this.boxWidth, this.boxHeight, this.alignment, this.onClick, this.fontStyle, this.type = ClickType.none, //默认没有点击类型 this.milliseconds = 500, //点击类型的时间戳(毫秒) }) : super(key: key) { if (padding != null && padding! > 0) { paddingLeft = padding; paddingRight = padding; paddingBottom = padding; paddingTop = padding; } if (margin != null && margin! > 0) { marginLeft = margin; marginTop = margin; marginRight = margin; marginBottom = margin; } 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 ?? FontWeight.normal; } } @override Widget build(BuildContext context) { //如果需要 Container 就用 Container 包裹,否则直接返回Text if (onClick != null || (cornerRadius ?? 0) > 0 || (borderWidth ?? 0) > 0 || boxWidth != null || boxHeight != null || backgroundColor != Colors.transparent || (padding ?? 0) > 0 || (margin ?? 0) > 0 || (paddingLeft ?? 0) > 0 || (paddingRight ?? 0) > 0 || (paddingRight ?? 0) > 0 || (paddingTop ?? 0) > 0 || (paddingBottom ?? 0) > 0 || (marginLeft ?? 0) > 0 || (marginRight ?? 0) > 0 || (marginTop ?? 0) > 0 || (marginBottom ?? 0) > 0 || alignment != null) { return _warpWithContainer(); } else { return _childText(); } } //TextView需要用容器包裹 Widget _warpWithContainer() { return Container( width: boxWidth, height: boxHeight, alignment: alignment, 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: onClick != null ? _childText().onTap( //使用扩展的方式点击 onClick!, type: type, milliseconds: milliseconds, ) : _childText(), ); } //真正的Tex控件配置 Widget _childText() { return Text( content.isEmpty ? hint ?? content : content, textAlign: textAlign ?? TextAlign.start, maxLines: maxLines, style: TextStyle( color: content.isEmpty ? textHintColor ?? textColor : textColor, fontSize: fontSize ?? 14, fontWeight: fontWeight, fontStyle: fontStyle, overflow: isTextEllipsis ?? false ? TextOverflow.ellipsis : TextOverflow.clip, // 可选,设置下划线的颜色 decoration: textDecoration, decorationColor: decorationColor, // 可选,设置下划线的粗细 decorationThickness: decorationThickness, decorationStyle: decorationStyle, // 可选,设置下划线的样式 ), ); } }