How can i make a time picker in flutter use 24 hr instead of AM/PM

Viewed 27

I have created an application in flutter for linux desktop and web. There is a showTimePicker for a form i used and it works fine in the desktop app and it returns the time in 24 hr format but when i run it in a browser the time picker changes to a 12 hr format.

Is there a setting that i need to change on my google chrome browser?

TimeOfDay _fltEndTime = TimeOfDay.now();
final TimeOfDay? newTime = await showTimePicker(context: context, 
initialTime: _fltEndTime,builder: (BuildContext context, Widget? 
child) {
    return MediaQuery(
        data: 
MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
        child: child!,
    );
  },
);
if (newTime != null) {                                                                            
  setState(() {                                                                              
      fltEndTime = newTime;                                                                              
  });                                                                               
}
1 Answers

Answer:

void inputTimeSelect() async {
    final TimeOfDay picked = await showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
      builder: (BuildContext context, Widget child) {
        return MediaQuery(
          data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
          child: child,
        );
      },
    );
}

It should respect system settings or you can force it by setting alwaysUse24HourFormat: true in MediaQueryData.

Related