text.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import 'package:flutter/material.dart';
  2. extension TextX on Text{
  3. ///字体
  4. ///配置中先注册family
  5. ///
  6. ///config text fontFamily
  7. Text fontFamily(String family) {
  8. var newStyle = TextStyle(fontFamily: family);
  9. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  10. }
  11. ///字体粗细、正斜等
  12. Text fontWeight(FontWeight? weight) {
  13. var newStyle = TextStyle(fontWeight: weight);
  14. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  15. }
  16. ///粗体
  17. ///
  18. Text bold() {
  19. var newStyle = const TextStyle(fontWeight: FontWeight.bold);
  20. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  21. }
  22. ///斜体
  23. ///
  24. Text italic() {
  25. var newStyle = const TextStyle(fontStyle: FontStyle.italic);
  26. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  27. }
  28. ///删除线
  29. ///
  30. Text strikethrough({bool active = true, Color? color}) {
  31. var newStyle = TextStyle(
  32. decoration: active == true ? TextDecoration.lineThrough : TextDecoration.none,
  33. decorationColor: color);
  34. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  35. }
  36. ///下划线
  37. ///
  38. Text underLine({bool active = true, Color? color}) {
  39. var newStyle = TextStyle(
  40. decoration: active == true ? TextDecoration.underline : TextDecoration.none,
  41. decorationColor: color);
  42. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  43. }
  44. ///上划线
  45. ///
  46. Text overLine({bool active = true, Color? color}) {
  47. var newStyle = TextStyle(
  48. decoration: active == true ? TextDecoration.overline : TextDecoration.none,
  49. decorationColor: color);
  50. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  51. }
  52. ///字母间距,可为负数
  53. ///
  54. Text letterSpacing(double spacing) {
  55. var newStyle = TextStyle(letterSpacing: spacing);
  56. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  57. }
  58. ///单词间距,可为负数
  59. ///
  60. Text wordSpacing(double spacing) {
  61. var newStyle = TextStyle(wordSpacing: spacing);
  62. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  63. }
  64. ///行间距,倍数
  65. ///
  66. Text lineSpacing(double spacing) {
  67. var newStyle = TextStyle(height: spacing);
  68. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  69. }
  70. Text colorInt(int color) {
  71. var newStyle = TextStyle(color: Color(color));
  72. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  73. }
  74. Text color(Color color) {
  75. var newStyle = TextStyle(color: color);
  76. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  77. }
  78. Text fontSize(double size) {
  79. var newStyle = TextStyle(fontSize: size);
  80. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  81. }
  82. Text lineLimit(int num, {TextOverflow? overflow = TextOverflow.ellipsis}) {
  83. return copyWith(maxLines: num, overflow: overflow);
  84. }
  85. Text singleLine({TextOverflow? overflow = TextOverflow.ellipsis}) {
  86. return lineLimit(1);
  87. }
  88. Text textAlignment(TextAlign align) {
  89. return copyWith(textAlign: align);
  90. }
  91. ///todo 手机号中间四位省略为*号,或类似场景。正则?
  92. ///truncationMode leading tail middle
  93. // String phoneNumber = '13888888888';
  94. // String phoneNumberStr = phoneNumber.replaceFirst(RegExp(r'\d{4}'), '****', 3);
  95. ///todo 艺术字效果 边框、渐变……
  96. ///foreground paint
  97. Text copyWith({
  98. String? data,
  99. Key? key,
  100. TextStyle? style,
  101. StrutStyle? strutStyle,
  102. TextAlign? textAlign,
  103. TextDirection? textDirection,
  104. Locale? locale,
  105. bool? softWrap,
  106. TextOverflow? overflow,
  107. double? textScaleFactor,
  108. int? maxLines,
  109. String? semanticsLabel,
  110. TextWidthBasis? textWidthBasis,
  111. TextHeightBehavior? textHeightBehavior,
  112. }) {
  113. return Text(data ?? this.data!,
  114. key: key ?? this.key,
  115. style: style ?? this.style,
  116. strutStyle: strutStyle ?? this.strutStyle,
  117. textAlign: textAlign ?? this.textAlign,
  118. textDirection: textDirection ?? this.textDirection,
  119. locale: locale ?? this.locale,
  120. softWrap: softWrap ?? this.softWrap,
  121. overflow: overflow ?? this.overflow,
  122. textScaleFactor: textScaleFactor ?? this.textScaleFactor,
  123. maxLines: maxLines ?? this.maxLines,
  124. semanticsLabel: semanticsLabel ?? this.semanticsLabel,
  125. textWidthBasis: textWidthBasis ?? this.textWidthBasis,
  126. textHeightBehavior: textHeightBehavior ?? this.textHeightBehavior);
  127. }
  128. }