my_text_view.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import 'dart:ffi';
  2. import 'dart:ui';
  3. import 'package:flutter/material.dart';
  4. import 'ext/ex_widget.dart';
  5. // ignore: slash_for_doc_comments
  6. /**
  7. TextView + 容器的使用
  8. 内部使用各种容器包裹Text控件,用于一些复杂的UI效果
  9. 模板:
  10. TextView(
  11. "自定义textview自定义textview自定义textview自定义textview自定义textview",
  12. backgroundColor: Colors.red,
  13. textColor: Colors.white,
  14. padding: 10,
  15. cornerRadius: 10,
  16. borderColor: Colors.yellow,
  17. borderWidth: 1,
  18. marginTop: 5,
  19. singleLine: false,
  20. onClick: (){
  21. print("点击事件");
  22. }),
  23. */
  24. class MyTextView extends StatelessWidget {
  25. double? fontSize = 0;
  26. Color? textColor = Colors.black;
  27. Color? textHintColor;
  28. String content = "";
  29. String? hint;
  30. bool? isTextEllipsis = false;
  31. bool? isFontLight;
  32. bool? isFontRegular;
  33. bool? isFontMedium;
  34. bool? isFontBold;
  35. FontWeight? fontWeight;
  36. TextAlign? textAlign;
  37. FontStyle? fontStyle;
  38. TextStyle? textStyle;
  39. int? maxLines;
  40. TextDecoration? textDecoration; //设置文本的装饰
  41. Color? decorationColor; //文本的装饰颜色
  42. double? decorationThickness; //装饰粗细
  43. TextDecorationStyle? decorationStyle; //装饰样式
  44. //底部是Text的父容器属性
  45. VoidCallback? onClick;
  46. double? cornerRadius = 0;
  47. double? borderWidth = 0;
  48. Color? borderColor = Colors.transparent;
  49. double? boxWidth; //限制TextView的宽高,为外部 Container 的属性
  50. double? boxHeight; //限制TextView的宽高,为外部 Container 的属性
  51. AlignmentGeometry? alignment;
  52. Color? backgroundColor = Colors.transparent;
  53. double? padding = 0;
  54. double? margin = 0;
  55. double? paddingLeft = 0;
  56. double? paddingRight = 0;
  57. double? paddingTop = 0;
  58. double? paddingBottom = 0;
  59. double? marginLeft = 0;
  60. double? marginRight = 0;
  61. double? marginTop = 0;
  62. double? marginBottom = 0;
  63. final ClickType type; //默认没有点击类型
  64. final int milliseconds; //点击类型的时间戳(毫秒)
  65. MyTextView(
  66. this.content, {
  67. Key? key,
  68. this.textColor,
  69. this.backgroundColor,
  70. this.padding,
  71. this.paddingTop,
  72. this.paddingBottom,
  73. this.paddingRight,
  74. this.textHintColor,
  75. this.hint,
  76. this.paddingLeft,
  77. this.cornerRadius,
  78. this.borderColor,
  79. this.borderWidth,
  80. this.marginBottom,
  81. this.marginLeft,
  82. this.marginRight,
  83. this.marginTop,
  84. this.margin,
  85. this.maxLines,
  86. this.fontSize,
  87. this.textDecoration,
  88. this.decorationColor,
  89. this.decorationThickness, //下划线
  90. this.decorationStyle,
  91. this.isTextEllipsis,
  92. this.isFontLight,
  93. this.isFontRegular,
  94. this.isFontMedium,
  95. this.isFontBold,
  96. this.fontWeight,
  97. this.textAlign,
  98. this.boxWidth,
  99. this.boxHeight,
  100. this.alignment,
  101. this.onClick,
  102. this.fontStyle,
  103. this.textStyle,
  104. this.type = ClickType.none, //默认没有点击类型
  105. this.milliseconds = 500, //点击类型的时间戳(毫秒)
  106. }) : super(key: key) {
  107. if (padding != null && padding! > 0) {
  108. paddingLeft = padding;
  109. paddingRight = padding;
  110. paddingBottom = padding;
  111. paddingTop = padding;
  112. }
  113. if (margin != null && margin! > 0) {
  114. marginLeft = margin;
  115. marginTop = margin;
  116. marginRight = margin;
  117. marginBottom = margin;
  118. }
  119. if (isFontLight != null && isFontLight!) {
  120. fontWeight = FontWeight.w300;
  121. } else if (isFontRegular != null && isFontRegular!) {
  122. fontWeight = FontWeight.w400;
  123. } else if (isFontMedium != null && isFontMedium!) {
  124. fontWeight = FontWeight.w500;
  125. } else if (isFontBold != null && isFontBold!) {
  126. fontWeight = FontWeight.w700;
  127. } else {
  128. fontWeight = fontWeight ?? FontWeight.normal;
  129. }
  130. }
  131. @override
  132. Widget build(BuildContext context) {
  133. //如果需要 Container 就用 Container 包裹,否则直接返回Text
  134. if (onClick != null ||
  135. (cornerRadius ?? 0) > 0 ||
  136. (borderWidth ?? 0) > 0 ||
  137. boxWidth != null ||
  138. boxHeight != null ||
  139. backgroundColor != Colors.transparent ||
  140. (padding ?? 0) > 0 ||
  141. (margin ?? 0) > 0 ||
  142. (paddingLeft ?? 0) > 0 ||
  143. (paddingRight ?? 0) > 0 ||
  144. (paddingRight ?? 0) > 0 ||
  145. (paddingTop ?? 0) > 0 ||
  146. (paddingBottom ?? 0) > 0 ||
  147. (marginLeft ?? 0) > 0 ||
  148. (marginRight ?? 0) > 0 ||
  149. (marginTop ?? 0) > 0 ||
  150. (marginBottom ?? 0) > 0 ||
  151. alignment != null) {
  152. return onClick != null
  153. ? _warpWithContainer().onTap(
  154. //使用扩展的方式点击
  155. onClick!,
  156. type: type,
  157. milliseconds: milliseconds,
  158. )
  159. : _warpWithContainer();
  160. } else {
  161. return onClick != null
  162. ? _childText().onTap(
  163. //使用扩展的方式点击
  164. onClick!,
  165. type: type,
  166. milliseconds: milliseconds,
  167. )
  168. : _childText();
  169. }
  170. }
  171. //TextView需要用容器包裹
  172. Widget _warpWithContainer() {
  173. return Container(
  174. width: boxWidth,
  175. height: boxHeight,
  176. alignment: alignment,
  177. margin: EdgeInsets.fromLTRB(marginLeft ?? 0, marginTop ?? 0, marginRight ?? 0, marginBottom ?? 0),
  178. decoration: BoxDecoration(
  179. border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
  180. color: backgroundColor,
  181. borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
  182. ),
  183. padding: EdgeInsets.fromLTRB(paddingLeft ?? 0, paddingTop ?? 0, paddingRight ?? 0, paddingBottom ?? 0),
  184. child: _childText(),
  185. );
  186. }
  187. //真正的Tex控件配置
  188. Widget _childText() {
  189. return Text(
  190. content.isEmpty ? hint ?? content : content,
  191. textAlign: textAlign ?? TextAlign.start,
  192. maxLines: maxLines,
  193. style: TextStyle(
  194. color: content.isEmpty ? textHintColor ?? textColor : textColor,
  195. fontSize: fontSize ?? 14,
  196. fontWeight: fontWeight,
  197. fontStyle: fontStyle,
  198. overflow: isTextEllipsis ?? false ? TextOverflow.ellipsis : TextOverflow.clip,
  199. // 可选,设置下划线的颜色
  200. decoration: textDecoration,
  201. decorationColor: decorationColor,
  202. // 可选,设置下划线的粗细
  203. decorationThickness: decorationThickness,
  204. decorationStyle: decorationStyle, // 可选,设置下划线的样式
  205. ).merge(textStyle),
  206. );
  207. }
  208. }