Flutter DateTimePicker: Disable sunday from weekdays not working

62 Views Asked by At

I'm trying to set Sunday not selectable from DateTimePicker in flutter. Somehow it works for the other 6 days except Sunday.

List<int> weekdays = [0,2,-1,-1,-1,-1,-1];

// ...

DateTimePicker(
  selectableDayPredicate: (DateTime val) => val.weekday == weekdays[0] || val.weekday == weekdays[1] || val.weekday == weekdays[2] || val.weekday == weekdays[3] || val.weekday == weekdays[4] || val.weekday == weekdays[5] || val.weekday == weekdays[6] ? false : true,
  initialValue: DateTime.now().toString(),
  initialDate: DateTime.now(),
  firstDate: DateTime.now(),
  dateLabelText: 'Date',
  type: DateTimePickerType.date,
  lastDate: DateTime.now().add(const Duration(days: 15)),
  onChanged: (date) {
    //
  },
  validator: (date) {
    return null;
  },
),

Output Screenshot: https://prnt.sc/U73nGkLjqZMZ

1

There are 1 best solutions below

5
White Choco On BEST ANSWER

Am I understood correctly that you want to make only Sundays not selectable of the calendar?

If correct, you can try:

DateTimePicker(
  selectableDayPredicate: (DateTime dateTime) => dateTime.weekday != DateTime.sunday,
  // other parameters...
)

then all Sundays will be NOT selectable.

Edit

I got your meaning, sorry for confusing.
You may want to try this one:

  final List<int> disableWeekdays = [
    DateTime.sunday,
    DateTime.tuesday,
    // any weekdays you want to make disabled...
  ];

  @override
  Widget build(BuildContext context) {
    return DateTimePicker(
      selectableDayPredicate: (DateTime dateTime) => !disableWeekdays.contains(dateTime.weekday),
      // other parameters...
    );
  }