date_picker_field.dart 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:wisdom_cli/wisdom_cli.dart';
  4. typedef WDatePickerFieldBuilder = Widget Function(
  5. DateTime? value,
  6. Future<void> Function() trigger,
  7. );
  8. typedef WDatePickerFieldOnChanged = void Function(DateTime? value);
  9. ///减少逻辑代码的单选组件
  10. class WDatePickerField extends StatefulWidget {
  11. WDatePickerField({
  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 WDatePickerFieldBuilder builder;
  21. final Widget? title;
  22. final DateTime? firstDate;
  23. final DateTime? lastDate;
  24. final DateTime? initialDate;
  25. final WDatePickerFieldOnChanged? onChanged;
  26. @override
  27. _WDatePickerFieldState createState() => _WDatePickerFieldState();
  28. }
  29. class _WDatePickerFieldState extends State<WDatePickerField> {
  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.date(
  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 WDatePickerFormField extends WFormField<DateTime> {
  67. WDatePickerFormField({
  68. Key? key,
  69. required WDatePickerFieldBuilder 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 = AutovalidateMode.disabled,
  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 _WDatePickerFormFieldState;
  88. return WDatePickerField(
  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. WDatePickerFormField.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. bool autovalidate = false,
  113. bool enabled = true,
  114. FocusNode? focusNode,
  115. InputDecoration decoration = const InputDecoration(
  116. suffixIconConstraints: const BoxConstraints(),
  117. suffixIcon: Icon(Icons.chevron_right),
  118. ),
  119. TextInputType? keyboardType,
  120. TextCapitalization textCapitalization = TextCapitalization.none,
  121. TextInputAction? textInputAction,
  122. TextStyle? style,
  123. StrutStyle? strutStyle,
  124. TextDirection? textDirection,
  125. TextAlign textAlign = TextAlign.start,
  126. TextAlignVertical? textAlignVertical,
  127. bool autofocus = false,
  128. // bool readOnly = false,
  129. ToolbarOptions? toolbarOptions,
  130. bool? showCursor,
  131. String obscuringCharacter = '•',
  132. bool obscureText = false,
  133. bool autocorrect = true,
  134. SmartDashesType? smartDashesType,
  135. SmartQuotesType? smartQuotesType,
  136. bool enableSuggestions = true,
  137. MaxLengthEnforcement maxLengthEnforcement = MaxLengthEnforcement.enforced,
  138. int maxLines = 1,
  139. int? minLines,
  140. bool expands = false,
  141. int? maxLength,
  142. // GestureTapCallback onTap,
  143. VoidCallback? onEditingComplete,
  144. ValueChanged<String>? onFieldSubmitted,
  145. List<TextInputFormatter>? inputFormatters,
  146. double cursorWidth = 2.0,
  147. Radius? cursorRadius,
  148. Color? cursorColor,
  149. Brightness? keyboardAppearance,
  150. EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  151. bool enableInteractiveSelection = true,
  152. InputCounterWidgetBuilder? buildCounter,
  153. ScrollPhysics? scrollPhysics,
  154. Iterable<String>? autofillHints,
  155. }) : super(
  156. key: key,
  157. field: field,
  158. onSaved: onSaved,
  159. validator: validator,
  160. autovalidateMode: AutovalidateMode.disabled,
  161. enabled: enabled,
  162. builder: (WFormFieldState<DateTime> fieldState) {
  163. final state = fieldState as _WDatePickerFormFieldState;
  164. //主题
  165. InputDecoration _decoration = decoration;
  166. _decoration = _decoration.applyDefaults(
  167. Theme.of(state.context).inputDecorationTheme,
  168. );
  169. _decoration = _decoration.copyWith(
  170. suffixIconConstraints:
  171. _decoration.suffixIconConstraints ?? const BoxConstraints(),
  172. suffixIcon: _decoration.suffixIcon ??
  173. Icon(
  174. Icons.chevron_right,
  175. color: Colors.black38,
  176. ),
  177. errorText: state.errorText,
  178. );
  179. final _controller = controller ?? TextEditingController();
  180. _controller.value =
  181. _controller.value.copyWith(text: buildName(state.value));
  182. return WDatePickerField(
  183. title: title,
  184. initialDate: state.value,
  185. firstDate: firstDate,
  186. lastDate: lastDate,
  187. onChanged: (value) => state.didChange(value),
  188. builder: (value, trigger) {
  189. return TextField(
  190. controller: _controller,
  191. focusNode: focusNode,
  192. decoration: _decoration,
  193. keyboardType: keyboardType,
  194. textInputAction: textInputAction,
  195. style: style,
  196. strutStyle: strutStyle,
  197. textAlign: textAlign,
  198. textAlignVertical: textAlignVertical,
  199. textDirection: textDirection,
  200. textCapitalization: textCapitalization,
  201. autofocus: autofocus,
  202. toolbarOptions: toolbarOptions,
  203. readOnly: true,
  204. showCursor: showCursor,
  205. obscuringCharacter: obscuringCharacter,
  206. obscureText: obscureText,
  207. autocorrect: autocorrect,
  208. smartDashesType: smartDashesType ??
  209. (obscureText
  210. ? SmartDashesType.disabled
  211. : SmartDashesType.enabled),
  212. smartQuotesType: smartQuotesType ??
  213. (obscureText
  214. ? SmartQuotesType.disabled
  215. : SmartQuotesType.enabled),
  216. enableSuggestions: enableSuggestions,
  217. maxLengthEnforcement: maxLengthEnforcement,
  218. maxLines: maxLines,
  219. minLines: minLines,
  220. expands: expands,
  221. maxLength: maxLength,
  222. // onChanged: onChangedHandler,
  223. onTap: () => trigger(),
  224. onEditingComplete: onEditingComplete,
  225. onSubmitted: onFieldSubmitted,
  226. inputFormatters: inputFormatters,
  227. enabled: enabled,
  228. cursorWidth: cursorWidth,
  229. cursorRadius: cursorRadius,
  230. cursorColor: cursorColor,
  231. scrollPadding: scrollPadding,
  232. scrollPhysics: scrollPhysics,
  233. keyboardAppearance: keyboardAppearance,
  234. enableInteractiveSelection: enableInteractiveSelection,
  235. buildCounter: buildCounter,
  236. autofillHints: autofillHints,
  237. );
  238. },
  239. );
  240. },
  241. );
  242. final String field;
  243. final WDatePickerFieldOnChanged? onChanged;
  244. @override
  245. _WDatePickerFormFieldState createState() => _WDatePickerFormFieldState();
  246. }
  247. class _WDatePickerFormFieldState extends WFormFieldState<DateTime> {
  248. @override
  249. WDatePickerFormField get widget => super.widget as WDatePickerFormField;
  250. @override
  251. void didChange(DateTime? value) {
  252. super.didChange(value);
  253. if (widget.onChanged != null) {
  254. widget.onChanged!(value);
  255. }
  256. }
  257. }