如何在Flutter上禁用特定日期列表 [英] How to disable list of specific date on flutter

查看:169
本文介绍了如何在Flutter上禁用特定日期列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用showDatePicker的代码,我的问题是如何禁用特定日期,一个,两个或多个json日期的列表?
我尝试在禁用周六和周日的情况下尝试selectableDayPredicate,但是如何使用api中的日期的json列表禁用该功能?

I have a code to call showDatePicker, my question is how to disable list of specific date, one, two or more json dates? I try selectableDayPredicate with Saturday and Sunday disabled, but how to do disable with json list of date from api ?

                onTap: () {
                            showDatePicker(
                              context: context,
                              initialDate:
                                  DateTime.now().add(Duration(days: 2)),
                              firstDate:
                                  DateTime.now().add(Duration(days: 1)),
                              lastDate:
                                  DateTime.now().add(Duration(days: 730)),
                              selectableDayPredicate: (DateTime val) =>
                                  val.weekday == 5 || val.weekday == 6
                                      ? false
                                      : true,
                            );
                          }


推荐答案

这在很大程度上取决于您从API获取的数据。

This depends a lot on the data you're getting from your API. That I can't help you with, but the predicate I can.

假设您已经从服务器中检索了一个包含一串字符串的json列表,则对它们进行了解析。放入DateTime对象(可能使用类似DateTime.parse的东西),则相对简单。

Assuming you've retrieved a json list from your server that contains a bunch of strings, parsed them into DateTime objects (probably using something like DateTime.parse), then it's relatively simple.

基本上,您需要检查每个日期(使用selectableDayPredicate)并返回true

Basically, you need to check for each date (using the selectableDayPredicate) and return true if it is not in your list.

首先,您需要清理DateTime对象,因为它们可能包含小时,秒,毫秒等。并且您需要它们转换成可以快速搜索的格式-因此可能不是DateTime。从技术上讲,DateTimes可以实现,因为它们确实具有hashCode实现,但是您必须创建无论如何都仅包括年,月和日的新DateTime对象(因为从技术上讲,具有+1毫秒的日期时间与没有它的日期时间是不同的值)。如果您确实想这样做,可以更改下面的 sanitizeDateTime 函数以返回 DateTime getDateSet 返回 Set< DateTime> ,但是我建议将它们变成String,因为它们在set和很简单。

First, you'll need to sanitize the DateTime objects as they may contain hours, seconds, milliseconds etc. And you need to make them into a format that can be searched quickly - so probably not DateTime. DateTimes would technically work as they do have a hashCode implementation, but you'd have to create new DateTime objects that only include year, month and day anyways (as a datetime with +1 millisecond is technically a different value than one without it). If you really wanted to do it that way you could change the sanitizeDateTime function below to return a DateTime and getDateSet to return a Set<DateTime>, but what I'd recommend is making them into Strings as they work well in sets and are simple.

所以您需要一个类似以下的函数:

So you'll need a function something like:

String sanitizeDateTime(DateTime dateTime)=> $ {dateTime.year}-$ {dateTime.month}-$ {dateTime.day};

无论您到哪里

Set< String> getDateSet(List< DateTime>日期)=> date.map(sanitizeDateTime).toSet();

重要的是您要有一组字符串而不是一个List,因为selectableDayPredicate将运行很多时候,如果您每次都遍历整个不可选择的日期列表(这是List的包含所做的事情),它可能会变慢 O(n * m)其中, n 是要检查的日期数, m 是无法选择的日期数-而如果使用集合,则包含使用更有效的哈希计算,然后进行查找,结果为 O(n)。可以将其解释为过早的优化,但在这种情况下,我认为这是一个值得的优化。

It's important that you have a Set of strings rather than a List as selectableDayPredicate will run a bunch of times, and if you were iterating through an entire list of unselectable dates each time you did that (which is what List's contains does), it could get slow as it's O(n*m) where n is the number of dates being checked and m is the number of unselectable dates - whereas if you use a set, contains uses a more efficient hash computation and then lookup which results in O(n). This could be construed as premature optimization, but in this case I think it's a worthwhile one.

一旦您拥有一组无法选择的日期,您只需对它们进行检查

Once you have the set of unselectable dates, you simply have to check against them in the predicate.

class .... {
  Set<String> unselectableDates; // assuming this is set somewhere

  ....
    onTap: () {
      showDatePicker(
        ...
        selectableDayPredicate: (DateTime val) {
          String sanitized = sanitizeDateTime(val);
          return !unselectableDates.contains(sanitized);
        }
    }
}

这篇关于如何在Flutter上禁用特定日期列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆