showRoundedTimePicker function

Future<TimeOfDay?> showRoundedTimePicker({
  1. required BuildContext context,
  2. required TimeOfDay initialTime,
  3. ThemeData? theme,
  4. Locale? locale,
  5. double borderRadius = 16.0,
  6. ImageProvider<Object>? imageHeader,
  7. String? fontFamily,
  8. bool barrierDismissible = false,
  9. Color background = Colors.transparent,
  10. String? negativeBtn,
  11. String? positiveBtn,
  12. String? leftBtn,
  13. VoidCallback? onLeftBtn,
  14. MaterialRoundedDatePickerStyle? style,
})

Shows a dialog containing a material design time picker.

The returned Future resolves to the time selected by the user when the user closes the dialog. If the user cancels the dialog, null is returned.

{@tool sample} Show a dialog with initialTime equal to the current time.

Future<TimeOfDay> selectedTime = showTimePicker(
  initialTime: TimeOfDay.now(),
  context: context,
);

{@end-tool}

The context argument is passed to showDialog, the documentation for which discusses how it is used.

The builder parameter can be used to wrap the dialog widget to add inherited widgets like Localizations.override, Directionality, or MediaQuery.

{@tool sample} Show a dialog with the text direction overridden to be TextDirection.rtl.

Future<TimeOfDay> selectedTimeRTL = showTimePicker(
  context: context,
  initialTime: TimeOfDay.now(),
  builder: (BuildContext context, Widget child) {
    return Directionality(
      textDirection: TextDirection.rtl,
      child: child,
    );
  },
);

{@end-tool}

{@tool sample} Show a dialog with time unconditionally displayed in 24 hour format.

Future<TimeOfDay> selectedTime24Hour = showTimePicker(
  context: context,
  initialTime: TimeOfDay(hour: 10, minute: 47),
  builder: (BuildContext context, Widget child) {
    return MediaQuery(
      data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
      child: child,
    );
  },
);

{@end-tool}

See also:

  • showDatePicker, which shows a dialog that contains a material design date picker.

Implementation

Future<TimeOfDay?> showRoundedTimePicker({
  required BuildContext context,
  required TimeOfDay initialTime,
  ThemeData? theme,
  Locale? locale,
  double borderRadius = 16.0,
  ImageProvider? imageHeader,
  String? fontFamily,
  bool barrierDismissible = false,
  Color background = Colors.transparent,
  String? negativeBtn,
  String? positiveBtn,
  String? leftBtn,
  VoidCallback? onLeftBtn,
  MaterialRoundedDatePickerStyle? style,
}) async {
  theme ??= ThemeData();

  assert(
    (leftBtn == null) == (onLeftBtn == null),
    "If you provide leftBtn, you must provide onLeftBtn",
  );
  assert(debugCheckHasMaterialLocalizations(context));

  final Widget dialog = _TimePickerDialog(
    initialTime: initialTime,
    borderRadius: borderRadius,
    imageHeader: imageHeader,
    fontFamily: fontFamily,
    negativeBtn: negativeBtn,
    positiveBtn: positiveBtn,
    leftBtn: leftBtn,
    onLeftBtn: onLeftBtn,
    style: style,
  );

  Widget child = GestureDetector(
    onTap: () {
      if (!barrierDismissible) Navigator.pop(context);
    },
    child: Container(
      color: background,
      child: GestureDetector(
        onTap: () {},
        child: dialog,
      ),
    ),
  );

  if (locale != null) {
    child = Localizations.override(
      context: context,
      locale: locale,
      child: dialog,
    );
  }

  return await showDialog<TimeOfDay>(
    context: context,
    builder: (_) => Theme(data: theme!, child: child),
  );
}