GaspardMerten / date_field

Flutter DateField and DateFormField
Other
45 stars 32 forks source link

DatePickerFormField no updates #7

Closed fregate closed 3 years ago

fregate commented 3 years ago

I have a form with to fields to select range with week between (at least)

DateTime _start = DateTime.now(); DateTime _finish = _start.add(Duration(days: 7));

         child: Form(
            key: _formKey,
            child: Wrap(
              children: <Widget>[
                DateTimeFormField(
                  mode: DateFieldPickerMode.date,
                  label: "Start Date",
                  initialValue: _start,
                  firstDate: DateTime.now(),
                  lastDate: DateTime(2101),
                  onDateSelected: (picked) {
                    if (picked != null && picked != _start)
                      setState(() {
                        _start = picked;
                        final addWeek = _start.add(Duration(days: 7));
                        if (_end.isBefore(addWeek)) {
                          _end = addWeek;
                        }
                      });
                  },
                ),
                DateTimeFormField(
                  mode: DateFieldPickerMode.date,
                  label: "Finish Date",
                  initialValue: _end,
                  firstDate: _start.add(Duration(days: 7)),
                  lastDate: DateTime(2101),
                  onDateSelected: (picked) {
                    if (picked != null && picked != _end)
                      setState(() {
                        _end = picked;
                      });
                  },
                  validator: (value) {
                    if (value.millisecondsSinceEpoch < _start.add(Duration(days: 7)).millisecondsSinceEpoch)
                      return "Duration must be at least one week";
                    else
                      return null;
                  },
                ),
              ],
            ),
          )

After select a new date in Start Date field (first) and run setState() with new dates - second field has no updates in text, but when creates it has all the right values. Except FormFieldState. When field builds its date field text it gets from FormFieldState.value - and there's old value.

In result - if I want to select Finish Date after set Start Date after initial _end, it throws error (initialValue isBefore than firstDate)

If just change selection of the second field - everything ok (new date renders correctly).

How I can fix this?