my_text_view.dart 5.8 KB

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