Flutter showTimePicker with specific 15 minute intervals

Viewed 4578

For the showTimePicker function in Flutter used to select a time, is it possible to set a defined time interval (such as 15 minutes) to prevent users from selecting any other timing?

Currently, this is the code I have for creating a default TimePicker.

showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
    );
2 Answers

You could try using this temporal solution using: time_picker_widget

enter image description here

Copy the following code into this DEMO

showCustomTimePicker(
    context: context,
    // It is a must if you provide selectableTimePredicate
    onFailValidation: (context) =>
        showMessage(context, 'Unavailable selection.'),
    initialTime: TimeOfDay(
        hour: _availableHours.first,
        minute: 0),
    selectableTimePredicate: (time) =>
        _availableHours.indexOf(time.hour) != -1 &&
        time.minute % 15 == 0).then(
    (time) =>
        setState(() => selectedTime = time?.format(context))),
Related