showIntervalTimePicker function

Future<TimeOfDay?> showIntervalTimePicker({
  1. required BuildContext context,
  2. required TimeOfDay initialTime,
  3. int interval = 1,
  4. VisibleStep visibleStep = VisibleStep.fifths,
  5. TransitionBuilder? builder,
  6. bool useRootNavigator = true,
  7. TimePickerEntryMode initialEntryMode = TimePickerEntryMode.dial,
  8. String? cancelText,
  9. String? confirmText,
  10. String? helpText,
  11. String? errorInvalidText,
  12. String? hourLabelText,
  13. String? minuteLabelText,
  14. RouteSettings? routeSettings,
  15. EntryModeChangeCallback? onEntryModeChanged,
  16. Offset? anchorPoint,
})

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 snippet} Show a dialog with initialTime equal to the current time.

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

{@end-tool}

The context, useRootNavigator and routeSettings arguments are 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.

The initialEntryMode parameter can be used to determine the initial time entry selection of the picker (either a clock dial or text input).

The interval parameter is used for setting the interval used for the TimePicker. The default and minimum value is 1. The maximum is 60.

The visibleStep parameter is used for which minute labels the TimePicker will show.

Optional strings for the helpText, cancelText, errorInvalidText, hourLabelText, minuteLabelText and confirmText can be provided to override the default values.

A DisplayFeature can split the screen into sub-screens. The closest one to anchorPoint is used to render the content.

If no anchorPoint is provided, then Directionality is used:

  • for TextDirection.ltr, anchorPoint is Offset.zero, which will cause the content to appear in the top-left sub-screen.
  • for TextDirection.rtl, anchorPoint is Offset(double.maxFinite, 0), which will cause the content to appear in the top-right sub-screen.

If no anchorPoint is provided, and there is no Directionality ancestor widget in the tree, then the widget asserts during build in debug mode.

By default, the time picker gets its colors from the overall theme's ColorScheme. The time picker can be further customized by providing a TimePickerThemeData to the overall theme.

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

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

{@end-tool}

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

Future<TimeOfDay?> selectedTime24Hour = showIntervalTimePicker(
  context: context,
  initialTime: const 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:

Implementation

Future<TimeOfDay?> showIntervalTimePicker({
  required BuildContext context,
  required TimeOfDay initialTime,
  int interval = 1,
  VisibleStep visibleStep = VisibleStep.fifths,
  TransitionBuilder? builder,
  bool useRootNavigator = true,
  TimePickerEntryMode initialEntryMode = TimePickerEntryMode.dial,
  String? cancelText,
  String? confirmText,
  String? helpText,
  String? errorInvalidText,
  String? hourLabelText,
  String? minuteLabelText,
  RouteSettings? routeSettings,
  EntryModeChangeCallback? onEntryModeChanged,
  Offset? anchorPoint,
}) async {
  assert(interval >= 1 && interval <= 60);
  assert(debugCheckHasMaterialLocalizations(context));

  final Widget dialog = IntervalTimePickerDialog(
    initialTime: initialTime,
    interval: interval,
    visibleStep: visibleStep,
    initialEntryMode: initialEntryMode,
    cancelText: cancelText,
    confirmText: confirmText,
    helpText: helpText,
    errorInvalidText: errorInvalidText,
    hourLabelText: hourLabelText,
    minuteLabelText: minuteLabelText,
    onEntryModeChanged: onEntryModeChanged,
  );
  return showDialog<TimeOfDay>(
    context: context,
    useRootNavigator: useRootNavigator,
    builder: (BuildContext context) {
      return builder == null ? dialog : builder(context, dialog);
    },
    routeSettings: routeSettings,
    anchorPoint: anchorPoint,
  );
}