Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the flutter timepicker in 12 hours format with AM PM selector

I want to use the Flutter timepicker in 12 hour format with AM/PM selector, but Flutter only shows me the 24 hours format.

I want to get this format: enter image description here

But flutter only shows me this format: enter image description here

This is my code:

_selectTime(BuildContext context) async {
  TimeOfDay picked = await showTimePicker(
    context: context,
    initialTime: TimeOfDay(hour: 12, minute: 00),
    builder: (BuildContext context, Widget child) {
      return MediaQuery(
        data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: false),
        child: child,
      );
    },
  );
}
like image 404
Darien Romero Leiva Avatar asked Apr 10 '20 18:04

Darien Romero Leiva


2 Answers

This should show you what you want

RaisedButton(
  child: Text('Show Time Picker'),
  onPressed: () async => await showTimePicker(
    context: context,
    builder: (BuildContext context, Widget child) {
      return MediaQuery(
        data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: false),
        child: child,
      );
    },
  ),
),
like image 135
wcyankees424 Avatar answered Oct 11 '22 18:10

wcyankees424


just use format() method to get AM,PM what you required.

final TimeOfDay picked = await showTimePicker(
  context: context,
  initialTime: timeStart,
);
print(picked.format(context));    // 8:15 PM
like image 20
Abdul Qadir Avatar answered Oct 11 '22 17:10

Abdul Qadir