text_field.dart 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import 'package:flutter/gestures.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. extension TextFieldX on TextField {
  5. ///字体
  6. ///配置中先注册family
  7. ///
  8. ///config TextField fontFamily
  9. TextField fontFamily(String family) {
  10. var newStyle = TextStyle(fontFamily: family);
  11. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  12. }
  13. ///字体粗细、正斜等
  14. TextField fontWeight(FontWeight? weight) {
  15. var newStyle = TextStyle(fontWeight: weight);
  16. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  17. }
  18. ///粗体
  19. ///
  20. TextField bold() {
  21. var newStyle = const TextStyle(fontWeight: FontWeight.bold);
  22. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  23. }
  24. ///斜体
  25. ///
  26. TextField italic() {
  27. var newStyle = const TextStyle(fontStyle: FontStyle.italic);
  28. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  29. }
  30. ///删除线
  31. ///
  32. TextField strikethrough({bool active = true, Color? color}) {
  33. var newStyle = TextStyle(
  34. decoration: active == true ? TextDecoration.lineThrough : TextDecoration.none,
  35. decorationColor: color);
  36. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  37. }
  38. ///下划线
  39. ///
  40. TextField underLine({bool active = true, Color? color}) {
  41. var newStyle = TextStyle(
  42. decoration: active == true ? TextDecoration.underline : TextDecoration.none,
  43. decorationColor: color);
  44. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  45. }
  46. ///上划线
  47. ///
  48. TextField overLine({bool active = true, Color? color}) {
  49. var newStyle = TextStyle(
  50. decoration: active == true ? TextDecoration.overline : TextDecoration.none,
  51. decorationColor: color);
  52. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  53. }
  54. ///字母间距,可为负数
  55. ///
  56. TextField letterSpacing(double spacing) {
  57. var newStyle = TextStyle(letterSpacing: spacing);
  58. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  59. }
  60. ///单词间距,可为负数
  61. ///
  62. TextField wordSpacing(double spacing) {
  63. var newStyle = TextStyle(wordSpacing: spacing);
  64. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  65. }
  66. ///行间距,倍数
  67. ///
  68. TextField lineSpacing(double spacing) {
  69. var newStyle = TextStyle(height: spacing);
  70. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  71. }
  72. TextField colorInt(int color) {
  73. var newStyle = TextStyle(color: Color(color));
  74. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  75. }
  76. TextField color(Color color) {
  77. var newStyle = TextStyle(color: color);
  78. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  79. }
  80. TextField fontSize(double size) {
  81. var newStyle = TextStyle(fontSize: size);
  82. return copyWith(style: style?.merge(newStyle) ?? newStyle);
  83. }
  84. TextField lineLimit(int? num) {
  85. return copyWith(maxLines: num);
  86. }
  87. ///无行数限制
  88. TextField noLineLimit() {
  89. return lineLimit(null);
  90. }
  91. TextField singleLine() {
  92. return lineLimit(1);
  93. }
  94. TextField textAlignment(TextAlign align) {
  95. return copyWith(textAlign: align);
  96. }
  97. ///set hintText
  98. TextField hint(String hint) {
  99. return copyWith(decoration: decoration?.copyWith(hintText: hint));
  100. }
  101. TextField hintStyle(TextStyle hintStyle) {
  102. return copyWith(decoration: decoration?.copyWith(hintStyle: hintStyle));
  103. }
  104. TextField hintWeight(FontWeight weight) {
  105. var newStyle = TextStyle(fontWeight: weight);
  106. var mergeStyle = decoration?.hintStyle?.merge(newStyle) ?? newStyle;
  107. return copyWith(decoration: decoration?.copyWith(hintStyle: mergeStyle));
  108. }
  109. TextField hintColor(Color color) {
  110. var newStyle = TextStyle(color: color);
  111. var mergeStyle = decoration?.hintStyle?.merge(newStyle) ?? newStyle;
  112. return copyWith(decoration: decoration?.copyWith(hintStyle: mergeStyle));
  113. }
  114. TextField hintSize(double size) {
  115. var newStyle = TextStyle(fontSize: size);
  116. var mergeStyle = decoration?.hintStyle?.merge(newStyle) ?? newStyle;
  117. return copyWith(decoration: decoration?.copyWith(hintStyle: mergeStyle));
  118. }
  119. TextField hintFamily(String family) {
  120. var newStyle = TextStyle(fontFamily: family);
  121. var mergeStyle = decoration?.hintStyle?.merge(newStyle) ?? newStyle;
  122. return copyWith(decoration: decoration?.copyWith(hintStyle: mergeStyle));
  123. }
  124. TextField border(InputBorder border) {
  125. return copyWith(decoration: decoration?.copyWith(border: border));
  126. }
  127. ///无边框
  128. TextField noBorder() {
  129. return border(InputBorder.none);
  130. }
  131. TextField copyWith({
  132. Key? key,
  133. TextEditingController? controller,
  134. FocusNode? focusNode,
  135. InputDecoration? decoration = const InputDecoration(),
  136. TextInputType? keyboardType,
  137. TextInputAction? textInputAction,
  138. TextCapitalization? textCapitalization,
  139. TextStyle? style,
  140. StrutStyle? strutStyle,
  141. TextAlign? textAlign,
  142. TextAlignVertical? textAlignVertical,
  143. TextDirection? textDirection,
  144. bool? readOnly,
  145. ToolbarOptions? toolbarOptions,
  146. bool? showCursor,
  147. bool? autofocus,
  148. String? obscuringCharacter,
  149. bool? obscureText,
  150. bool? autocorrect,
  151. SmartDashesType? smartDashesType,
  152. SmartQuotesType? smartQuotesType,
  153. bool? enableSuggestions,
  154. int? maxLines = 1,
  155. int? minLines,
  156. bool? expands,
  157. int? maxLength,
  158. bool? maxLengthEnforced,
  159. MaxLengthEnforcement? maxLengthEnforcement,
  160. void Function(String)? onChanged,
  161. void Function()? onEditingComplete,
  162. void Function(String)? onSubmitted,
  163. void Function(String, Map)? onAppPrivateCommand,
  164. List<TextInputFormatter>? inputFormatters,
  165. bool? enabled,
  166. double? cursorWidth,
  167. double? cursorHeight,
  168. Radius? cursorRadius,
  169. Color? cursorColor,
  170. // BoxHeightStyle? selectionHeightStyle,
  171. // BoxWidthStyle? selectionWidthStyle,
  172. Brightness? keyboardAppearance,
  173. EdgeInsets? scrollPadding,
  174. DragStartBehavior? dragStartBehavior,
  175. bool? enableInteractiveSelection,
  176. TextSelectionControls? selectionControls,
  177. void Function()? onTap,
  178. MouseCursor? mouseCursor,
  179. Widget? Function(BuildContext,
  180. {required int currentLength, required bool isFocused, required int? maxLength})?
  181. buildCounter,
  182. ScrollController? scrollController,
  183. ScrollPhysics? scrollPhysics,
  184. Iterable<String>? autofillHints,
  185. Clip? clipBehavior,
  186. String? restorationId,
  187. bool? enableIMEPersonalizedLearning,
  188. }) {
  189. return TextField(
  190. key: key ?? this.key,
  191. controller: controller ?? this.controller,
  192. focusNode: focusNode ?? this.focusNode,
  193. decoration: decoration ?? this.decoration,
  194. keyboardType: keyboardType ?? this.keyboardType,
  195. textInputAction: textInputAction ?? this.textInputAction,
  196. textCapitalization: textCapitalization ?? this.textCapitalization,
  197. style: style ?? this.style,
  198. strutStyle: strutStyle ?? this.strutStyle,
  199. textAlign: textAlign ?? this.textAlign,
  200. textAlignVertical: textAlignVertical ?? this.textAlignVertical,
  201. textDirection: textDirection ?? this.textDirection,
  202. readOnly: readOnly ?? this.readOnly,
  203. toolbarOptions: toolbarOptions ?? this.toolbarOptions,
  204. showCursor: showCursor ?? this.showCursor,
  205. autofocus: autofocus ?? this.autofocus,
  206. obscuringCharacter: obscuringCharacter ?? this.obscuringCharacter,
  207. obscureText: obscureText ?? this.obscureText,
  208. autocorrect: autocorrect ?? this.autocorrect,
  209. smartDashesType: smartDashesType ?? this.smartDashesType,
  210. smartQuotesType: smartQuotesType ?? this.smartQuotesType,
  211. enableSuggestions: enableSuggestions ?? this.enableSuggestions,
  212. maxLines: maxLines ?? this.maxLines,
  213. minLines: minLines ?? this.minLines,
  214. expands: expands ?? this.expands,
  215. maxLength: maxLength ?? this.maxLength,
  216. maxLengthEnforcement: maxLengthEnforcement ?? this.maxLengthEnforcement,
  217. onChanged: onChanged ?? this.onChanged,
  218. onEditingComplete: onEditingComplete ?? this.onEditingComplete,
  219. onSubmitted: onSubmitted ?? this.onSubmitted,
  220. onAppPrivateCommand: onAppPrivateCommand ?? this.onAppPrivateCommand,
  221. inputFormatters: inputFormatters ?? this.inputFormatters,
  222. enabled: enabled ?? this.enabled,
  223. cursorWidth: cursorWidth ?? this.cursorWidth,
  224. cursorHeight: cursorHeight ?? this.cursorHeight,
  225. cursorRadius: cursorRadius ?? this.cursorRadius,
  226. cursorColor: cursorColor ?? this.cursorColor,
  227. // selectionHeightStyle: selectionHeightStyle ?? this.selectionHeightStyle,
  228. // selectionWidthStyle: selectionWidthStyle ?? this.selectionWidthStyle,
  229. keyboardAppearance: keyboardAppearance ?? this.keyboardAppearance,
  230. scrollPadding: scrollPadding ?? this.scrollPadding,
  231. dragStartBehavior: dragStartBehavior ?? this.dragStartBehavior,
  232. enableInteractiveSelection: enableInteractiveSelection ?? this.enableInteractiveSelection,
  233. selectionControls: selectionControls ?? this.selectionControls,
  234. onTap: onTap ?? this.onTap,
  235. mouseCursor: mouseCursor ?? this.mouseCursor,
  236. buildCounter: buildCounter ?? this.buildCounter,
  237. scrollController: scrollController ?? this.scrollController,
  238. scrollPhysics: scrollPhysics ?? this.scrollPhysics,
  239. autofillHints: autofillHints ?? this.autofillHints,
  240. clipBehavior: clipBehavior ?? this.clipBehavior,
  241. restorationId: restorationId ?? this.restorationId,
  242. enableIMEPersonalizedLearning:
  243. enableIMEPersonalizedLearning ?? this.enableIMEPersonalizedLearning,
  244. );
  245. }
  246. }