如何在Flutter中限制showTimePicker中的时间。

huangapple go评论117阅读模式
英文:

How to limit the time in showTimePicker Flutter

问题

I need to set Time limit at TimePicker. like i need to set time picker in between 12pm to 6pm and another time i don't want to select from user.

In older version of flutter they provide allowedHours and allowedMinutes but in new version they hide that properties.

Older version code:-

showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
allowedHours: [8,9,10,11,12, 15,16,17],
allowedMinutes: [0,10,20,30,40,50],
displayDisabledValues: true
);

I want to set limit at TimePicker based on requirement. Is there any another way to set it. like any Property or any third party package?

英文:

I need to set Time limit at TimePicker. like i need to set time picker in between 12pm to 6pm and another time i don't want to select from user.

In older version of flutter they provide allowedHours and allowedMinutes but in new version they hide that properties.

Older version code:-

showTimePicker(
    context: context,
    initialTime: TimeOfDay.now(),
    allowedHours: [8,9,10,11,12, 15,16,17],
    allowedMinutes: [0,10,20,30,40,50],
    displayDisabledValues: true

);

I want to set limit at TimePicker based on requirement. Is there any another way to set it. like any Property or any third party package?

答案1

得分: 1

以下是翻译好的部分:

例如,我正在使用12 PM到2 PM。以下代码片段满足您的需求。
使用包名 time_range_picker: ^2.2.0。时间范围起始时间:

TimeOfDay(hour: 12, minute: 00),
end: TimeOfDay(hour: 02, minute: 00)

实现:

return Scaffold(
  body: Container(
    margin: EdgeInsets.only(top: 100),
    child: MaterialButton(
      onPressed: () async {
        TimeRange result = await showTimeRangePicker(
          context: context,
          start: TimeOfDay(hour: 12, minute: 00),
          end: TimeOfDay(hour: 02, minute: 00)
        );
        print("result " + result.toString());
      },
      child: Container(color: Colors.red, child: Text("显示时间选择器")),
    ),
  ),
);

包链接:time_range_picker: ^2.2.0

英文:

For example I am using 12 pm to 2 PM. Below snippet meet with your purpose.
Use a packager name time_range_picker: ^2.2.0. Time range start:

    TimeOfDay(hour: 12, minute: 00),
                      end: TimeOfDay(hour: 02, minute: 00)

Implementation :

   return Scaffold(
          body: Container(
            margin: EdgeInsets.only(top: 100),
            child: MaterialButton(
              onPressed: () async {
                TimeRange result = await showTimeRangePicker(
                  context: context,
                  start: TimeOfDay(hour: 12, minute: 00),
                  end: TimeOfDay(hour: 02, minute: 00)
                );
                print("result " + result.toString());
              },
              child: Container(color: Colors.red, child: Text("Show Time Picker")),
            ),
          ),
        );  

package link : time_range_picker: ^2.2.0

huangapple
  • 本文由 发表于 2023年3月9日 13:17:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定