my_text_view.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.alignment,
  100. this.onClick,
  101. this.fontStyle,
  102. this.type = ClickType.none, //默认没有点击类型
  103. this.milliseconds = 500, //点击类型的时间戳(毫秒)
  104. }) : super(key: key) {
  105. if (padding != null && padding! > 0) {
  106. paddingLeft = padding;
  107. paddingRight = padding;
  108. paddingBottom = padding;
  109. paddingTop = padding;
  110. }
  111. if (margin != null && margin! > 0) {
  112. marginLeft = margin;
  113. marginTop = margin;
  114. marginRight = margin;
  115. marginBottom = margin;
  116. }
  117. if (isFontLight != null && isFontLight!) {
  118. fontWeight = FontWeight.w300;
  119. } else if (isFontRegular != null && isFontRegular!) {
  120. fontWeight = FontWeight.w400;
  121. } else if (isFontMedium != null && isFontMedium!) {
  122. fontWeight = FontWeight.w500;
  123. } else if (isFontBold != null && isFontBold!) {
  124. fontWeight = FontWeight.w700;
  125. } else {
  126. fontWeight = fontWeight ?? FontWeight.normal;
  127. }
  128. }
  129. @override
  130. Widget build(BuildContext context) {
  131. //如果需要 Container 就用 Container 包裹,否则直接返回Text
  132. if (onClick != null ||
  133. (cornerRadius ?? 0) > 0 ||
  134. (borderWidth ?? 0) > 0 ||
  135. boxWidth != null ||
  136. boxHeight != null ||
  137. backgroundColor != Colors.transparent ||
  138. (padding ?? 0) > 0 ||
  139. (margin ?? 0) > 0 ||
  140. (paddingLeft ?? 0) > 0 ||
  141. (paddingRight ?? 0) > 0 ||
  142. (paddingRight ?? 0) > 0 ||
  143. (paddingTop ?? 0) > 0 ||
  144. (paddingBottom ?? 0) > 0 ||
  145. (marginLeft ?? 0) > 0 ||
  146. (marginRight ?? 0) > 0 ||
  147. (marginTop ?? 0) > 0 ||
  148. (marginBottom ?? 0) > 0 ||
  149. alignment != null) {
  150. return _warpWithContainer();
  151. } else {
  152. return _childText();
  153. }
  154. }
  155. //TextView需要用容器包裹
  156. Widget _warpWithContainer() {
  157. return Container(
  158. width: boxWidth,
  159. height: boxHeight,
  160. alignment: alignment,
  161. margin: EdgeInsets.fromLTRB(marginLeft ?? 0, marginTop ?? 0, marginRight ?? 0, marginBottom ?? 0),
  162. decoration: BoxDecoration(
  163. border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
  164. color: backgroundColor,
  165. borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
  166. ),
  167. padding: EdgeInsets.fromLTRB(paddingLeft ?? 0, paddingTop ?? 0, paddingRight ?? 0, paddingBottom ?? 0),
  168. child: onClick != null
  169. ? _childText().onTap(
  170. //使用扩展的方式点击
  171. onClick!,
  172. type: type,
  173. milliseconds: milliseconds,
  174. )
  175. : _childText(),
  176. );
  177. }
  178. //真正的Tex控件配置
  179. Widget _childText() {
  180. return Text(
  181. content.isEmpty ? hint ?? content : content,
  182. textAlign: textAlign ?? TextAlign.start,
  183. maxLines: maxLines,
  184. style: TextStyle(
  185. color: content.isEmpty ? textHintColor ?? textColor : textColor,
  186. fontSize: fontSize ?? 14,
  187. fontWeight: fontWeight,
  188. fontStyle: fontStyle,
  189. overflow: isTextEllipsis ?? false ? TextOverflow.ellipsis : TextOverflow.clip,
  190. // 可选,设置下划线的颜色
  191. decoration: textDecoration,
  192. decorationColor: decorationColor,
  193. // 可选,设置下划线的粗细
  194. decorationThickness: decorationThickness,
  195. decorationStyle: decorationStyle, // 可选,设置下划线的样式
  196. ),
  197. );
  198. }
  199. }