Flutter showTimePicker with specific 15 minute intervals

3,238

Solution 1

There is currently no way in the API, I have raised this issue: https://github.com/flutter/flutter/issues/60573

Solution 2

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))),
Share:
3,238
JustAPerson
Author by

JustAPerson

Updated on December 22, 2022

Comments

  • JustAPerson
    JustAPerson 11 months

    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(),
        );