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

Jus*_*son 6 datetime datetimepicker flutter

对于Flutter中用于选择时间的showTimePicker函数,是否可以设置一个定义的时间间隔(例如15分钟)以防止用户选择任何其他时间?

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

showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
    );
Run Code Online (Sandbox Code Playgroud)

Jor*_*eña 5

您可以尝试使用此临时解决方案: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))),
Run Code Online (Sandbox Code Playgroud)


Kay*_*yes 3

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