my_text_view.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. int? maxLines;
  39. TextDecoration? textDecoration; //设置文本的装饰
  40. Color? decorationColor; //文本的装饰颜色
  41. double? decorationThickness; //装饰粗细
  42. TextDecorationStyle? decorationStyle; //装饰样式
  43. //底部是Text的父容器属性
  44. VoidCallback? onClick;
  45. double? cornerRadius = 0;
  46. double? borderWidth = 0;
  47. Color? borderColor = Colors.transparent;
  48. double? boxWidth; //限制TextView的宽高,为外部 Container 的属性
  49. double? boxHeight; //限制TextView的宽高,为外部 Container 的属性
  50. AlignmentGeometry? alignment;
  51. Color? backgroundColor = Colors.transparent;
  52. double? padding = 0;
  53. double? margin = 0;
  54. double? paddingLeft = 0;
  55. double? paddingRight = 0;
  56. double? paddingTop = 0;
  57. double? paddingBottom = 0;
  58. double? marginLeft = 0;
  59. double? marginRight = 0;
  60. double? marginTop = 0;
  61. double? marginBottom = 0;
  62. final ClickType type; //默认没有点击类型
  63. final int milliseconds; //点击类型的时间戳(毫秒)
  64. MyTextView(
  65. this.content, {
  66. Key? key,
  67. this.textColor,
  68. this.backgroundColor,
  69. this.padding,
  70. this.paddingTop,
  71. this.paddingBottom,
  72. this.paddingRight,
  73. this.textHintColor,
  74. this.hint,
  75. this.paddingLeft,
  76. this.cornerRadius,
  77. this.borderColor,
  78. this.borderWidth,
  79. this.marginBottom,
  80. this.marginLeft,
  81. this.marginRight,
  82. this.marginTop,
  83. this.margin,
  84. this.maxLines,
  85. this.fontSize,
  86. this.textDecoration,
  87. this.decorationColor,
  88. this.decorationThickness,
  89. this.decorationStyle,
  90. this.isTextEllipsis,
  91. this.isFontLight,
  92. this.isFontRegular,
  93. this.isFontMedium,
  94. this.isFontBold,
  95. this.fontWeight,
  96. this.textAlign,
  97. this.boxWidth,
  98. this.boxHeight,
  99. this.onClick,
  100. this.fontStyle,
  101. this.type = ClickType.none, //默认没有点击类型
  102. this.milliseconds = 500, //点击类型的时间戳(毫秒)
  103. }) : super(key: key) {
  104. if (padding != null && padding! > 0) {
  105. paddingLeft = padding;
  106. paddingRight = padding;
  107. paddingBottom = padding;
  108. paddingTop = padding;
  109. }
  110. if (margin != null && margin! > 0) {
  111. marginLeft = margin;
  112. marginTop = margin;
  113. marginRight = margin;
  114. marginBottom = margin;
  115. }
  116. if (isFontLight != null && isFontLight!) {
  117. fontWeight = FontWeight.w300;
  118. } else if (isFontRegular != null && isFontRegular!) {
  119. fontWeight = FontWeight.w400;
  120. } else if (isFontMedium != null && isFontMedium!) {
  121. fontWeight = FontWeight.w500;
  122. } else if (isFontBold != null && isFontBold!) {
  123. fontWeight = FontWeight.w700;
  124. } else {
  125. fontWeight = fontWeight ?? FontWeight.normal;
  126. }
  127. }
  128. @override
  129. Widget build(BuildContext context) {
  130. //如果需要 Container 就用 Container 包裹,否则直接返回Text
  131. if (onClick != null ||
  132. (cornerRadius ?? 0) > 0 ||
  133. (borderWidth ?? 0) > 0 ||
  134. boxWidth != null ||
  135. boxHeight != null ||
  136. backgroundColor != Colors.transparent ||
  137. (padding ?? 0) > 0 ||
  138. (margin ?? 0) > 0 ||
  139. (paddingLeft ?? 0) > 0 ||
  140. (paddingRight ?? 0) > 0 ||
  141. (paddingRight ?? 0) > 0 ||
  142. (paddingTop ?? 0) > 0 ||
  143. (paddingBottom ?? 0) > 0 ||
  144. (marginLeft ?? 0) > 0 ||
  145. (marginRight ?? 0) > 0 ||
  146. (marginTop ?? 0) > 0 ||
  147. (marginBottom ?? 0) > 0 ||
  148. alignment != null) {
  149. return _warpWithContainer();
  150. } else {
  151. return _childText();
  152. }
  153. }
  154. //TextView需要用容器包裹
  155. Widget _warpWithContainer() {
  156. return Container(
  157. width: boxWidth,
  158. height: boxHeight,
  159. alignment: alignment,
  160. margin: EdgeInsets.fromLTRB(marginLeft ?? 0, marginTop ?? 0, marginRight ?? 0, marginBottom ?? 0),
  161. decoration: BoxDecoration(
  162. border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
  163. color: backgroundColor,
  164. borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
  165. ),
  166. padding: EdgeInsets.fromLTRB(paddingLeft ?? 0, paddingTop ?? 0, paddingRight ?? 0, paddingBottom ?? 0),
  167. child: onClick != null
  168. ? _childText().onTap(
  169. //使用扩展的方式点击
  170. onClick!,
  171. type: type,
  172. milliseconds: milliseconds,
  173. )
  174. : _childText(),
  175. );
  176. }
  177. //真正的Tex控件配置
  178. Widget _childText() {
  179. return Text(
  180. content.isEmpty ? hint ?? content : content,
  181. textAlign: textAlign ?? TextAlign.start,
  182. maxLines: maxLines,
  183. style: TextStyle(
  184. color: content.isEmpty ? textHintColor ?? textColor : textColor,
  185. fontSize: fontSize ?? 14,
  186. fontWeight: fontWeight,
  187. fontStyle: fontStyle,
  188. overflow: isTextEllipsis ?? false ? TextOverflow.ellipsis : TextOverflow.clip,
  189. // 可选,设置下划线的颜色
  190. decoration: textDecoration,
  191. decorationColor: decorationColor,
  192. // 可选,设置下划线的粗细
  193. decorationThickness: decorationThickness,
  194. decorationStyle: decorationStyle, // 可选,设置下划线的样式
  195. ),
  196. );
  197. }
  198. }