my_text_view.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 onClick != null
  151. ? _warpWithContainer().onTap(
  152. //使用扩展的方式点击
  153. onClick!,
  154. type: type,
  155. milliseconds: milliseconds,
  156. )
  157. : _warpWithContainer();
  158. } else {
  159. return onClick != null
  160. ? _childText().onTap(
  161. //使用扩展的方式点击
  162. onClick!,
  163. type: type,
  164. milliseconds: milliseconds,
  165. )
  166. : _childText();
  167. }
  168. }
  169. //TextView需要用容器包裹
  170. Widget _warpWithContainer() {
  171. return Container(
  172. width: boxWidth,
  173. height: boxHeight,
  174. alignment: alignment,
  175. margin: EdgeInsets.fromLTRB(marginLeft ?? 0, marginTop ?? 0, marginRight ?? 0, marginBottom ?? 0),
  176. decoration: BoxDecoration(
  177. border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
  178. color: backgroundColor,
  179. borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
  180. ),
  181. padding: EdgeInsets.fromLTRB(paddingLeft ?? 0, paddingTop ?? 0, paddingRight ?? 0, paddingBottom ?? 0),
  182. child: _childText(),
  183. );
  184. }
  185. //真正的Tex控件配置
  186. Widget _childText() {
  187. return Text(
  188. content.isEmpty ? hint ?? content : content,
  189. textAlign: textAlign ?? TextAlign.start,
  190. maxLines: maxLines,
  191. style: TextStyle(
  192. color: content.isEmpty ? textHintColor ?? textColor : textColor,
  193. fontSize: fontSize ?? 14,
  194. fontWeight: fontWeight,
  195. fontStyle: fontStyle,
  196. overflow: isTextEllipsis ?? false ? TextOverflow.ellipsis : TextOverflow.clip,
  197. // 可选,设置下划线的颜色
  198. decoration: textDecoration,
  199. decorationColor: decorationColor,
  200. // 可选,设置下划线的粗细
  201. decorationThickness: decorationThickness,
  202. decorationStyle: decorationStyle, // 可选,设置下划线的样式
  203. ),
  204. );
  205. }
  206. }