month_picker_field.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:wisdom_cli/wisdom_cli.dart';
  4. typedef WMonthPickerFieldBuilder = Widget Function(
  5. DateTime? value,
  6. Future<void> Function() trigger,
  7. );
  8. typedef WMonthPickerFieldOnChanged = void Function(DateTime? value);
  9. ///减少逻辑代码的单选组件
  10. class WMonthPickerField extends StatefulWidget {
  11. WMonthPickerField({
  12. Key? key,
  13. this.initialDate,
  14. required this.builder,
  15. this.title,
  16. this.firstDate,
  17. this.lastDate,
  18. this.onChanged,
  19. }) : super(key: key);
  20. final WMonthPickerFieldBuilder builder;
  21. final Widget? title;
  22. final DateTime? firstDate;
  23. final DateTime? lastDate;
  24. final DateTime? initialDate;
  25. final WMonthPickerFieldOnChanged? onChanged;
  26. @override
  27. _WMonthPickerFieldState createState() => _WMonthPickerFieldState();
  28. }
  29. class _WMonthPickerFieldState extends State<WMonthPickerField> {
  30. DateTime? _value;
  31. DateTime? get value => _value;
  32. @override
  33. void initState() {
  34. super.initState();
  35. _value = widget.initialDate;
  36. }
  37. ///触发单选弹出层
  38. Future<void> _trigger() async {
  39. final res = await WPickerUtil.month(
  40. context,
  41. title: widget.title,
  42. initialDate: _value,
  43. firstDate: widget.firstDate,
  44. lastDate: widget.lastDate,
  45. );
  46. if (res != null) {
  47. _onChanged(res);
  48. }
  49. }
  50. ///触发[widget.onChanged],
  51. ///刷新视图[widget.builder]
  52. void _onChanged(DateTime value) {
  53. if (_value == value) return;
  54. _value = value;
  55. setState(() {});
  56. if (widget.onChanged != null) {
  57. widget.onChanged!(value);
  58. }
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. return widget.builder(value, () => _trigger());
  63. }
  64. }
  65. ///picker表单
  66. class WMonthPickerFormField extends WFormField<DateTime> {
  67. WMonthPickerFormField({
  68. Key? key,
  69. required WMonthPickerFieldBuilder builder,
  70. required this.field,
  71. DateTime? firstDate,
  72. DateTime? lastDate,
  73. Widget? title,
  74. this.onChanged,
  75. FormFieldSetter<DateTime>? onSaved,
  76. FormFieldValidator<DateTime>? validator,
  77. AutovalidateMode? autovalidateMode,
  78. bool enabled = true,
  79. }) : super(
  80. key: key,
  81. field: field,
  82. onSaved: onSaved,
  83. validator: validator,
  84. autovalidateMode: autovalidateMode,
  85. enabled: enabled,
  86. builder: (WFormFieldState<DateTime> fieldState) {
  87. final _fieldState = fieldState as _WMonthPickerFormFieldState;
  88. return WMonthPickerField(
  89. title: title,
  90. initialDate: _fieldState.value,
  91. firstDate: firstDate,
  92. lastDate: lastDate,
  93. builder: builder,
  94. onChanged: (value) {
  95. _fieldState.didChange(value);
  96. },
  97. );
  98. },
  99. );
  100. ///直接使用[textField]进行builder
  101. WMonthPickerFormField.textField({
  102. Key? key,
  103. required String Function(DateTime? value) buildName,
  104. required this.field,
  105. DateTime? firstDate,
  106. DateTime? lastDate,
  107. Widget? title,
  108. this.onChanged,
  109. TextEditingController? controller,
  110. FormFieldSetter<DateTime>? onSaved,
  111. FormFieldValidator<DateTime>? validator,
  112. AutovalidateMode? autovalidateMode,
  113. bool enabled = true,
  114. FocusNode? focusNode,
  115. InputDecoration? decoration,
  116. TextInputType? keyboardType,
  117. TextCapitalization textCapitalization = TextCapitalization.none,
  118. TextInputAction? textInputAction,
  119. TextStyle? style,
  120. StrutStyle? strutStyle,
  121. TextDirection? textDirection,
  122. TextAlign textAlign = TextAlign.start,
  123. TextAlignVertical? textAlignVertical,
  124. bool autofocus = false,
  125. // bool readOnly = false,
  126. ToolbarOptions? toolbarOptions,
  127. bool? showCursor,
  128. String obscuringCharacter = '•',
  129. bool obscureText = false,
  130. bool autocorrect = true,
  131. SmartDashesType? smartDashesType,
  132. SmartQuotesType? smartQuotesType,
  133. bool enableSuggestions = true,
  134. MaxLengthEnforcement maxLengthEnforcement = MaxLengthEnforcement.enforced,
  135. int maxLines = 1,
  136. int? minLines,
  137. bool expands = false,
  138. int? maxLength,
  139. // GestureTapCallback onTap,
  140. VoidCallback? onEditingComplete,
  141. ValueChanged<String>? onFieldSubmitted,
  142. List<TextInputFormatter>? inputFormatters,
  143. double cursorWidth = 2.0,
  144. Radius? cursorRadius,
  145. Color? cursorColor,
  146. Brightness? keyboardAppearance,
  147. EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  148. bool enableInteractiveSelection = true,
  149. InputCounterWidgetBuilder? buildCounter,
  150. ScrollPhysics? scrollPhysics,
  151. Iterable<String>? autofillHints,
  152. }) : super(
  153. key: key,
  154. field: field,
  155. onSaved: onSaved,
  156. validator: validator,
  157. autovalidateMode: autovalidateMode,
  158. enabled: enabled,
  159. builder: (WFormFieldState<DateTime> fieldState) {
  160. final state = fieldState as _WMonthPickerFormFieldState;
  161. //主题
  162. InputDecoration _decoration = decoration ?? InputDecoration();
  163. _decoration = _decoration.applyDefaults(
  164. Theme.of(state.context).inputDecorationTheme,
  165. );
  166. _decoration = _decoration.copyWith(
  167. suffixIconConstraints:
  168. _decoration.suffixIconConstraints ?? const BoxConstraints(),
  169. suffixIcon: _decoration.suffixIcon ??
  170. Icon(
  171. Icons.chevron_right,
  172. color: Colors.black38,
  173. ),
  174. errorText: state.errorText,
  175. );
  176. final _controller = controller ?? TextEditingController();
  177. _controller.value =
  178. _controller.value.copyWith(text: buildName(state.value));
  179. return WMonthPickerField(
  180. title: title,
  181. initialDate: state.value,
  182. firstDate: firstDate,
  183. lastDate: lastDate,
  184. onChanged: (value) => state.didChange(value),
  185. builder: (value, trigger) {
  186. return TextField(
  187. controller: _controller,
  188. focusNode: focusNode,
  189. decoration: _decoration,
  190. keyboardType: keyboardType,
  191. textInputAction: textInputAction,
  192. style: style,
  193. strutStyle: strutStyle,
  194. textAlign: textAlign,
  195. textAlignVertical: textAlignVertical,
  196. textDirection: textDirection,
  197. textCapitalization: textCapitalization,
  198. autofocus: autofocus,
  199. toolbarOptions: toolbarOptions,
  200. readOnly: true,
  201. showCursor: showCursor,
  202. obscuringCharacter: obscuringCharacter,
  203. obscureText: obscureText,
  204. autocorrect: autocorrect,
  205. smartDashesType: smartDashesType ??
  206. (obscureText
  207. ? SmartDashesType.disabled
  208. : SmartDashesType.enabled),
  209. smartQuotesType: smartQuotesType ??
  210. (obscureText
  211. ? SmartQuotesType.disabled
  212. : SmartQuotesType.enabled),
  213. enableSuggestions: enableSuggestions,
  214. maxLengthEnforcement: maxLengthEnforcement,
  215. maxLines: maxLines,
  216. minLines: minLines,
  217. expands: expands,
  218. maxLength: maxLength,
  219. // onChanged: onChangedHandler,
  220. onTap: () => trigger(),
  221. onEditingComplete: onEditingComplete,
  222. onSubmitted: onFieldSubmitted,
  223. inputFormatters: inputFormatters,
  224. enabled: enabled,
  225. cursorWidth: cursorWidth,
  226. cursorRadius: cursorRadius,
  227. cursorColor: cursorColor,
  228. scrollPadding: scrollPadding,
  229. scrollPhysics: scrollPhysics,
  230. keyboardAppearance: keyboardAppearance,
  231. enableInteractiveSelection: enableInteractiveSelection,
  232. buildCounter: buildCounter,
  233. autofillHints: autofillHints,
  234. );
  235. },
  236. );
  237. },
  238. );
  239. final String field;
  240. final WMonthPickerFieldOnChanged? onChanged;
  241. @override
  242. _WMonthPickerFormFieldState createState() => _WMonthPickerFormFieldState();
  243. }
  244. class _WMonthPickerFormFieldState extends WFormFieldState<DateTime> {
  245. @override
  246. WMonthPickerFormField get widget => super.widget as WMonthPickerFormField;
  247. @override
  248. void didChange(DateTime? value) {
  249. super.didChange(value);
  250. if (widget.onChanged != null) {
  251. widget.onChanged!(value);
  252. }
  253. }
  254. }