my_text_view.dart 5.2 KB

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