具有特定 15 分钟间隔的 Flutter showTimePicker

Flutter showTimePicker with specific 15 minute intervals

Flutter中的showTimePicker函数用来select一个时间,是否可以设置一个定义的时间间隔(比如15分钟)来防止用户select还有其他时间安排吗?

目前,这是我创建默认 TimePicker 的代码。

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

API暂时没有办法,我提出了这个问题: https://github.com/flutter/flutter/issues/60573

您可以尝试使用此临时解决方案:time_picker_widget

将以下代码复制到此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))),