我已经在Flutter应用程序中实现了DatePicker。我试图将选择器限制为仅允许用户选择工作日。但是,我不确定如何执行此操作。我相信这与SelectableDayPredicate有关。以下是我的代码片段:
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; }); } }
当用户点击listTile时,将调用_selectDate函数。
这是一个示例,其中我从选择器中省略了星期五和星期六,您可以按照此处的逻辑来实现所需的目标:
selectableDayPredicate: (DateTime val) => val.weekday == 5 || val.weekday == 6 ? false : true,