Search code examples
flutterdartflutter-form-builder

Time validation error on input when setting hour in flutter


Hi everyone I have some troubles with Flutter FormBuilderDateTimePicker

My problem is that when I set the value manually (with a keyboard not using graphical draggable 'arrows' offered by the widget) I get the validation error every time the hour value is between 12 and 00.

For example, if I set 12:30 the value will be accepted, but if I change the hour to 16:30 it will display a validation error message. Below you can find a graphical representation of my case.

enter image description here enter image description here

Here is how I set my FormBuilderTimePicker widget

    FormBuilderDateTimePicker(
         name: 'fieldname',
         initialValue: DateTime.now(),
         initialDate: DateTime.now(),
         initialEntryMode: DatePickerEntryMode.input,
         alwaysUse24HourFormat: true,
         onChanged: (value) => mayValue = ,
         format: DateFormat.yMMMMd('it_IT').add_Hm(),
         timePickerInitialEntryMode: TimePickerEntryMode.input,
       )

The other input type is working fine, but I would like to keep them both enter image description here


Solution

  • On flutter_form_builder: ^7.3.1 source code, alwaysUse24HourFormat has not being used. As matias-de-andrea mention on git issue which is still open.

    To make it work, they override the transitionBuilder in order to provide 24h format on showTimePicker context.

    transitionBuilder: (BuildContext context, Widget? child) {
        return MediaQuery(
          data: MediaQuery.of(context)
              .copyWith(alwaysUse24HourFormat: true),
          child: child!,
        );
      },
    

    You can check How to use 24 hour clock when invoking showTimePicker() in Flutter? and they've discussed more about this.