GaspardMerten / date_field

Flutter DateField and DateFormField
Other
45 stars 32 forks source link

Cannot set the selectedDate in DateTimeFormField for dynamic List of Fields #40

Closed hamuelpalallos closed 1 year ago

hamuelpalallos commented 1 year ago

I'm trying to use DateTimeFormField in a dynamic list where i can add and remove items, however i have encountered an issue with setting the values when the list is updated, since the "initialValue" does set on rebuild, but the DateTimeField "selectedDate" works fine but there's is not "validator" parameter, how can I achieved this?

I have tried the fieldCreator but it just overrides the DateTimeFormField hence no "validator"

DateTimeFormField( decoration: const InputDecoration( isDense: true, errorStyle: TextStyle(color: Colors.redAccent), border: outlineInputBorder, contentPadding: contentPadding, prefixIcon: Icon(Icons.event_note), labelText: 'Birth date * ', ), mode: DateTimeFieldPickerMode.date, onDateSelected: onChangedBirthdate, fieldCreator: ({ DateFormat? dateFormat, TextStyle? dateTextStyle, InputDecoration? decoration, bool? enabled, DateTime? firstDate, DateTime? initialDate, DatePickerMode? initialDatePickerMode, DatePickerEntryMode? initialEntryMode, TimePickerEntryMode? initialTimePickerEntryMode, Key? key, DateTime? lastDate, DateTimeFieldPickerMode? mode, required void Function(DateTime)? onDateSelected, required DateTime? selectedDate, bool? use24hFormat, }) { return DateTimeField( selectedDate: passenger.birthdate, onDateSelected: onChangedBirthdate, ); }, ),

GaspardMerten commented 1 year ago

You can use DateTimeFormField with a global key. Then you can access the state of the field and set its value accordingly.

More details here: https://pub.dev/documentation/date_field/latest/date_field/DateTimeFormField-class.html And some info about global keys: https://api.flutter.dev/flutter/widgets/GlobalKey-class.html

Hope this helps.

hamuelpalallos commented 1 year ago

Thanks!

hamuelpalallos commented 1 year ago

how about when I get the value from the database and i try to add it to the form? and can be updated again to be saved in the database?

GaspardMerten commented 1 year ago

I must admit that I did not understand your issue. Could you provide some code sample illustrating what you wish to do.

Note that this is kind of question seems more suited for StackOverflow as it doesn't appear to be an issue in itself.