|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import '../actions.dart'; |
| 3 | +import '../flet_app_services.dart'; |
| 4 | +import '../models/control.dart'; |
| 5 | +import '../protocol/update_control_props_payload.dart'; |
| 6 | +import 'form_field.dart'; |
| 7 | +import '../utils/icons.dart'; |
| 8 | + |
| 9 | +class DatePickerControl extends StatefulWidget { |
| 10 | + final Control? parent; |
| 11 | + final Control control; |
| 12 | + final List<Control> children; |
| 13 | + final bool parentDisabled; |
| 14 | + final dynamic dispatch; |
| 15 | + |
| 16 | + const DatePickerControl({ |
| 17 | + Key? key, |
| 18 | + this.parent, |
| 19 | + required this.control, |
| 20 | + required this.children, |
| 21 | + required this.parentDisabled, |
| 22 | + required this.dispatch, |
| 23 | + }) : super(key: key); |
| 24 | + |
| 25 | + @override |
| 26 | + State<DatePickerControl> createState() => _DatePickerControlState(); |
| 27 | +} |
| 28 | + |
| 29 | +class _DatePickerControlState extends State<DatePickerControl> { |
| 30 | + bool _open = false; |
| 31 | + |
| 32 | + @override |
| 33 | + Widget build(BuildContext context) { |
| 34 | + debugPrint("DatePicker build: ${widget.control.id}"); |
| 35 | + var open = widget.control.attrBool("open", false)!; |
| 36 | + DateTime? value = widget.control.attrDateTime("value"); |
| 37 | + DateTime? firstDate = widget.control.attrDateTime("firstDate"); |
| 38 | + DateTime? lastDate = widget.control.attrDateTime("lastDate"); |
| 39 | + DateTime? currentDate = widget.control.attrDateTime("currentDate"); |
| 40 | + //String? localeString = widget.control.attrString("locale"); |
| 41 | + String? helpText = widget.control.attrString("helpText"); |
| 42 | + String? cancelText = widget.control.attrString("cancelText"); |
| 43 | + String? confirmText = widget.control.attrString("confirmText"); |
| 44 | + String? errorFormatText = widget.control.attrString("errorFormatText"); |
| 45 | + String? errorInvalidText = widget.control.attrString("errorInvalidText"); |
| 46 | + TextInputType keyboardType = |
| 47 | + parseTextInputType(widget.control.attrString("keyboardType", "")!); |
| 48 | + DatePickerMode datePickerMode = DatePickerMode.values.firstWhere( |
| 49 | + (a) => |
| 50 | + a.name.toLowerCase() == |
| 51 | + widget.control.attrString("datePickerMode", "")!.toLowerCase(), |
| 52 | + orElse: () => DatePickerMode.day); |
| 53 | + DatePickerEntryMode datePickerEntryMode = DatePickerEntryMode.values |
| 54 | + .firstWhere( |
| 55 | + (a) => |
| 56 | + a.name.toLowerCase() == |
| 57 | + widget.control |
| 58 | + .attrString("datePickerEntryMode", "")! |
| 59 | + .toLowerCase(), |
| 60 | + orElse: () => DatePickerEntryMode.calendar); |
| 61 | + String? fieldHintText = widget.control.attrString("fieldHintText"); |
| 62 | + String? fieldLabelText = widget.control.attrString("fieldLabelText"); |
| 63 | + IconData? switchToCalendarEntryModeIcon = getMaterialIcon( |
| 64 | + widget.control.attrString("switchToCalendarEntryModeIcon", "")!); |
| 65 | + IconData? switchToInputEntryModeIcon = getMaterialIcon( |
| 66 | + widget.control.attrString("switchToInputEntryModeIcon", "")!); |
| 67 | + |
| 68 | + //Locale locale; |
| 69 | + // if (localeString == null) { |
| 70 | + // locale = Localizations.localeOf(context); |
| 71 | + // } else { |
| 72 | + // //locale = Locale(localeString); |
| 73 | + // } |
| 74 | + |
| 75 | + void onClosed(DateTime? dateValue) { |
| 76 | + String stringValue; |
| 77 | + String eventName; |
| 78 | + if (dateValue == null) { |
| 79 | + stringValue = |
| 80 | + value?.toIso8601String() ?? currentDate?.toIso8601String() ?? ""; |
| 81 | + eventName = "dismiss"; |
| 82 | + } else { |
| 83 | + stringValue = dateValue?.toIso8601String() ?? ""; |
| 84 | + eventName = "change"; |
| 85 | + } |
| 86 | + List<Map<String, String>> props = [ |
| 87 | + {"i": widget.control.id, "value": stringValue, "open": "false"} |
| 88 | + ]; |
| 89 | + widget.dispatch( |
| 90 | + UpdateControlPropsAction(UpdateControlPropsPayload(props: props))); |
| 91 | + FletAppServices.of(context).server.updateControlProps(props: props); |
| 92 | + |
| 93 | + FletAppServices.of(context).server.sendPageEvent( |
| 94 | + eventTarget: widget.control.id, |
| 95 | + eventName: eventName, |
| 96 | + eventData: stringValue); |
| 97 | + } |
| 98 | + |
| 99 | + Widget createSelectDateDialog() { |
| 100 | + Widget dialog = DatePickerDialog( |
| 101 | + initialDate: value ?? currentDate ?? DateTime.now(), |
| 102 | + firstDate: firstDate ?? DateTime(1900), |
| 103 | + lastDate: lastDate ?? DateTime(2050), |
| 104 | + currentDate: currentDate ?? DateTime.now(), |
| 105 | + helpText: helpText, |
| 106 | + cancelText: cancelText, |
| 107 | + confirmText: confirmText, |
| 108 | + errorFormatText: errorFormatText, |
| 109 | + errorInvalidText: errorInvalidText, |
| 110 | + keyboardType: keyboardType, |
| 111 | + initialCalendarMode: datePickerMode, |
| 112 | + initialEntryMode: datePickerEntryMode, |
| 113 | + fieldHintText: fieldHintText, |
| 114 | + fieldLabelText: fieldLabelText, |
| 115 | + switchToCalendarEntryModeIcon: switchToCalendarEntryModeIcon != null |
| 116 | + ? Icon(switchToCalendarEntryModeIcon) |
| 117 | + : null, |
| 118 | + switchToInputEntryModeIcon: switchToInputEntryModeIcon != null |
| 119 | + ? Icon(switchToInputEntryModeIcon) |
| 120 | + : null, |
| 121 | + ); |
| 122 | + |
| 123 | + // dialog = Localizations.override( |
| 124 | + // context: context, |
| 125 | + // locale: locale, |
| 126 | + // child: dialog, |
| 127 | + // ); |
| 128 | + |
| 129 | + return dialog; |
| 130 | + } |
| 131 | + |
| 132 | + if (open && !_open) { |
| 133 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 134 | + showDialog<DateTime>( |
| 135 | + context: context, |
| 136 | + builder: (context) => createSelectDateDialog()).then((result) { |
| 137 | + debugPrint("pickDate() completed"); |
| 138 | + onClosed(result); |
| 139 | + }); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + _open = open; |
| 144 | + return const SizedBox.shrink(); |
| 145 | + } |
| 146 | +} |
0 commit comments