|
| 1 | +// Copyright 2025 The Flutter Authors. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// ignore_for_file: avoid_dynamic_calls |
| 6 | + |
| 7 | +import 'package:dart_schema_builder/dart_schema_builder.dart'; |
| 8 | +import 'package:flutter/material.dart'; |
| 9 | +import 'package:flutter_genui/flutter_genui.dart'; |
| 10 | +import 'package:intl/intl.dart'; |
| 11 | + |
| 12 | +final _schema = S.object( |
| 13 | + properties: { |
| 14 | + 'value': S.string( |
| 15 | + description: 'The initial date of the date picker in yyyy-mm-dd format.', |
| 16 | + ), |
| 17 | + 'label': S.string(description: 'Label for the date picker.'), |
| 18 | + }, |
| 19 | +); |
| 20 | + |
| 21 | +extension type _DatePickerData.fromMap(JsonMap _json) { |
| 22 | + factory _DatePickerData({String? value, String? label}) => |
| 23 | + _DatePickerData.fromMap({'value': value, 'label': label}); |
| 24 | + |
| 25 | + String? get value => _json['value'] as String?; |
| 26 | + String? get label => _json['label'] as String?; |
| 27 | +} |
| 28 | + |
| 29 | +class _DateInputChip extends StatefulWidget { |
| 30 | + const _DateInputChip({ |
| 31 | + this.initialValue, |
| 32 | + this.label, |
| 33 | + required this.onChanged, |
| 34 | + }); |
| 35 | + |
| 36 | + final String? initialValue; |
| 37 | + final String? label; |
| 38 | + final void Function(String) onChanged; |
| 39 | + |
| 40 | + @override |
| 41 | + State<_DateInputChip> createState() => _DateInputChipState(); |
| 42 | +} |
| 43 | + |
| 44 | +class _DateInputChipState extends State<_DateInputChip> { |
| 45 | + DateTime? _selectedDate; |
| 46 | + |
| 47 | + @override |
| 48 | + void initState() { |
| 49 | + super.initState(); |
| 50 | + if (widget.initialValue != null) { |
| 51 | + _selectedDate = DateTime.tryParse(widget.initialValue!); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @override |
| 56 | + void didUpdateWidget(_DateInputChip oldWidget) { |
| 57 | + super.didUpdateWidget(oldWidget); |
| 58 | + if (widget.initialValue != oldWidget.initialValue) { |
| 59 | + if (widget.initialValue != null) { |
| 60 | + _selectedDate = DateTime.tryParse(widget.initialValue!); |
| 61 | + } else { |
| 62 | + _selectedDate = null; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @override |
| 68 | + Widget build(BuildContext context) { |
| 69 | + final text = _selectedDate == null |
| 70 | + ? widget.label ?? 'Date' |
| 71 | + : '${widget.label}: ${DateFormat.yMMMd().format(_selectedDate!)}'; |
| 72 | + return FilterChip( |
| 73 | + label: Text(text), |
| 74 | + selected: false, |
| 75 | + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)), |
| 76 | + onSelected: (bool selected) { |
| 77 | + showModalBottomSheet<void>( |
| 78 | + context: context, |
| 79 | + builder: (BuildContext context) { |
| 80 | + return SizedBox( |
| 81 | + height: 300, |
| 82 | + child: Column( |
| 83 | + children: [ |
| 84 | + Expanded( |
| 85 | + child: CalendarDatePicker( |
| 86 | + initialDate: _selectedDate ?? DateTime.now(), |
| 87 | + firstDate: DateTime(1700), |
| 88 | + lastDate: DateTime(2101), |
| 89 | + onDateChanged: (newDate) { |
| 90 | + setState(() { |
| 91 | + _selectedDate = newDate; |
| 92 | + }); |
| 93 | + final formattedDate = DateFormat( |
| 94 | + 'yyyy-MM-dd', |
| 95 | + ).format(newDate); |
| 96 | + widget.onChanged(formattedDate); |
| 97 | + Navigator.pop(context); |
| 98 | + }, |
| 99 | + ), |
| 100 | + ), |
| 101 | + ], |
| 102 | + ), |
| 103 | + ); |
| 104 | + }, |
| 105 | + ); |
| 106 | + }, |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +final dateInputChip = CatalogItem( |
| 112 | + name: 'DateInputChip', |
| 113 | + dataSchema: _schema, |
| 114 | + exampleData: [ |
| 115 | + () => { |
| 116 | + 'root': 'date_picker', |
| 117 | + 'widgets': [ |
| 118 | + { |
| 119 | + 'id': 'date_picker', |
| 120 | + 'widget': { |
| 121 | + 'DateInputChip': { |
| 122 | + 'value': '1871-07-22', |
| 123 | + 'label': 'Your birth date', |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + ], |
| 128 | + }, |
| 129 | + ], |
| 130 | + |
| 131 | + widgetBuilder: |
| 132 | + ({ |
| 133 | + required data, |
| 134 | + required id, |
| 135 | + required buildChild, |
| 136 | + required dispatchEvent, |
| 137 | + required context, |
| 138 | + required values, |
| 139 | + }) { |
| 140 | + final datePickerData = _DatePickerData.fromMap(data as JsonMap); |
| 141 | + return _DateInputChip( |
| 142 | + initialValue: datePickerData.value, |
| 143 | + label: datePickerData.label, |
| 144 | + onChanged: (newValue) => values[id] = newValue, |
| 145 | + ); |
| 146 | + }, |
| 147 | +); |
0 commit comments