tableDayPredicate将DatePicker限制为仅

tableDayPredicate将DatePicker限制为仅

本文介绍了如何使用SelectableDayPredicate将DatePicker限制为仅工作日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Flutter应用程序中实现了DatePicker。我试图将选择器限制为仅允许用户选择工作日。但是,我不确定如何执行此操作。我相信这与SelectableDayPredicate有关。以下是我的代码段:

I have implemented a DatePicker to my Flutter application. I am trying to limit the picker to only allow the users to choose weekdays. However, I am not sure how to do this. I believe it has got to do with the SelectableDayPredicate. Below is a snippet of my code:

Future<Null> _selectDate(BuildContext context) async {
  final DateTime picked = await showDatePicker(
    context: context,
    initialDate: _date,
    firstDate: new DateTime(DateTime.now().year),
    lastDate: new DateTime(DateTime.now().year+1),
    // to do: I am pretty sure the SelectableDayPredicate should go somewhere here.
  );

  if (picked != null && picked != _date) {
    setState(() {
      _date = picked;
    });
  }
}

_selectDate函数在用户点击listTile。

The _selectDate function is called when the user taps on the listTile.

推荐答案

在此示例中,我从选择器中省略了周五和周六,您可以按照此处的逻辑来实现所需的功能:

Here is an example where I omit Fridays and Saturdays from the picker, you can follow the logic here to achieve what you want:

selectableDayPredicate: (DateTime val) =>
            val.weekday == 5 || val.weekday == 6 ? false : true,

这篇关于如何使用SelectableDayPredicate将DatePicker限制为仅工作日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:10