text_form_field.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * @Author: XianKaiQun
  3. * @Date: 2020-08-24 15:39:09
  4. * @LastEditors : WuWei
  5. * @LastEditTime : 2023-05-25 15:51:14
  6. * @Description:
  7. */
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter/services.dart';
  10. import 'package:wisdom_cli/wisdom_cli.dart';
  11. ///与[TextFormField]几乎一致,
  12. ///差距在于这里使用[field]进行绑定,
  13. ///[field]为[WForm.initialValues]的[Map.key]
  14. ///
  15. class WTextFormField extends WFormField<String> {
  16. WTextFormField({
  17. Key? key,
  18. this.controller,
  19. required String field,
  20. FocusNode? focusNode,
  21. InputDecoration? decoration = const InputDecoration(),
  22. TextInputType? keyboardType,
  23. TextCapitalization textCapitalization = TextCapitalization.none,
  24. TextInputAction? textInputAction,
  25. TextStyle? style,
  26. StrutStyle? strutStyle,
  27. TextDirection? textDirection,
  28. TextAlign textAlign = TextAlign.start,
  29. TextAlignVertical? textAlignVertical,
  30. bool autofocus = false,
  31. bool readOnly = false,
  32. ToolbarOptions? toolbarOptions,
  33. bool? showCursor,
  34. String obscuringCharacter = '•',
  35. bool obscureText = false,
  36. bool autocorrect = true,
  37. SmartDashesType? smartDashesType,
  38. SmartQuotesType? smartQuotesType,
  39. bool enableSuggestions = true,
  40. AutovalidateMode? autovalidateMode,
  41. MaxLengthEnforcement maxLengthEnforcement = MaxLengthEnforcement.enforced,
  42. int maxLines = 1,
  43. int? minLines,
  44. bool expands = false,
  45. int? maxLength,
  46. ValueChanged<String>? onChanged,
  47. GestureTapCallback? onTap,
  48. VoidCallback? onEditingComplete,
  49. ValueChanged<String>? onFieldSubmitted,
  50. FormFieldSetter<String>? onSaved,
  51. FormFieldValidator<String>? validator,
  52. List<TextInputFormatter>? inputFormatters,
  53. bool? enabled,
  54. double cursorWidth = 2.0,
  55. Radius? cursorRadius,
  56. Color? cursorColor,
  57. Brightness? keyboardAppearance,
  58. EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  59. bool enableInteractiveSelection = true,
  60. InputCounterWidgetBuilder? buildCounter,
  61. ScrollPhysics? scrollPhysics,
  62. Iterable<String>? autofillHints,
  63. bool showClose = true,
  64. }) : assert(controller == null),
  65. assert(minLines == null || minLines > 0),
  66. assert(!obscureText || maxLines == 1,
  67. 'Obscured fields cannot be multiline.'),
  68. assert(maxLength == null || maxLength > 0),
  69. super(
  70. key: key,
  71. field: field,
  72. onSaved: onSaved,
  73. validator: validator,
  74. autovalidateMode: autovalidateMode,
  75. enabled: enabled ?? true,
  76. builder: (WFormFieldState<String> field) {
  77. final _WTextFormFieldState state = field as _WTextFormFieldState;
  78. var _decoration = (decoration ?? const InputDecoration());
  79. _decoration = _decoration.applyDefaults(
  80. Theme.of(field.context).inputDecorationTheme,
  81. );
  82. _decoration = _decoration.copyWith(errorText: field.errorText);
  83. if ((state.value ?? '') != '' && showClose == true)
  84. _decoration = _decoration.copyWith(
  85. suffix: _decoration.suffix ??
  86. Padding(
  87. padding: EdgeInsets.only(right: 5.pt),
  88. child: WDot.delete(
  89. color: Color(0xfff2f2f2),
  90. iconTheme: IconThemeData(color: Color(0xff999999)),
  91. onTap: () => state.setValue('', rebuild: true),
  92. ),
  93. ),
  94. );
  95. void onChangedHandler(String value) {
  96. if (onChanged != null) {
  97. onChanged(value);
  98. }
  99. field.didChange(value);
  100. }
  101. final controller = state._effectiveController!;
  102. controller.value = controller.value.copyWith(text: state.value);
  103. return TextField(
  104. controller: controller,
  105. focusNode: focusNode,
  106. decoration: _decoration,
  107. keyboardType: keyboardType,
  108. textInputAction: textInputAction,
  109. style: style,
  110. strutStyle: strutStyle,
  111. textAlign: textAlign,
  112. textAlignVertical: textAlignVertical,
  113. textDirection: textDirection,
  114. textCapitalization: textCapitalization,
  115. autofocus: autofocus,
  116. toolbarOptions: toolbarOptions,
  117. readOnly: readOnly,
  118. showCursor: showCursor,
  119. obscuringCharacter: obscuringCharacter,
  120. obscureText: obscureText,
  121. autocorrect: autocorrect,
  122. smartDashesType: smartDashesType ??
  123. (obscureText
  124. ? SmartDashesType.disabled
  125. : SmartDashesType.enabled),
  126. smartQuotesType: smartQuotesType ??
  127. (obscureText
  128. ? SmartQuotesType.disabled
  129. : SmartQuotesType.enabled),
  130. enableSuggestions: enableSuggestions,
  131. maxLengthEnforcement: maxLengthEnforcement,
  132. maxLines: maxLines,
  133. minLines: minLines,
  134. expands: expands,
  135. maxLength: maxLength,
  136. onChanged: onChangedHandler,
  137. onTap: onTap,
  138. onEditingComplete: onEditingComplete,
  139. onSubmitted: onFieldSubmitted,
  140. inputFormatters: inputFormatters,
  141. enabled: enabled ?? decoration?.enabled ?? true,
  142. cursorWidth: cursorWidth,
  143. cursorRadius: cursorRadius,
  144. cursorColor: cursorColor,
  145. scrollPadding: scrollPadding,
  146. scrollPhysics: scrollPhysics,
  147. keyboardAppearance: keyboardAppearance,
  148. enableInteractiveSelection: enableInteractiveSelection,
  149. buildCounter: buildCounter,
  150. autofillHints: autofillHints,
  151. );
  152. },
  153. );
  154. final TextEditingController? controller;
  155. @override
  156. _WTextFormFieldState createState() => _WTextFormFieldState();
  157. }
  158. class _WTextFormFieldState extends WFormFieldState<String> {
  159. TextEditingController? _controller;
  160. TextEditingController? get _effectiveController =>
  161. widget.controller ?? _controller;
  162. @override
  163. WTextFormField get widget => super.widget as WTextFormField;
  164. @override
  165. void initState() {
  166. super.initState();
  167. if (widget.controller != null) {
  168. widget.controller!.addListener(_handleControllerChanged);
  169. } else {
  170. _controller = TextEditingController();
  171. }
  172. }
  173. @override
  174. void didUpdateWidget(WTextFormField oldWidget) {
  175. super.didUpdateWidget(oldWidget);
  176. if (widget.controller != oldWidget.controller) {
  177. oldWidget.controller?.removeListener(_handleControllerChanged);
  178. widget.controller?.addListener(_handleControllerChanged);
  179. if (oldWidget.controller != null && widget.controller == null)
  180. _controller =
  181. TextEditingController.fromValue(oldWidget.controller!.value);
  182. if (widget.controller != null) {
  183. setValue(widget.controller!.text);
  184. if (oldWidget.controller == null) _controller = null;
  185. }
  186. }
  187. }
  188. @override
  189. void dispose() {
  190. widget.controller?.removeListener(_handleControllerChanged);
  191. super.dispose();
  192. }
  193. @override
  194. void didChange(String? value) {
  195. super.didChange(value);
  196. if (_effectiveController!.text != value)
  197. _effectiveController!.text = value!;
  198. }
  199. void _handleControllerChanged() {
  200. if (_effectiveController!.text != value)
  201. didChange(_effectiveController!.text);
  202. }
  203. }