Flutter/Dart Disable Multiple Week Days using enabledDayPredicate of Calendar API / TableCalendar package

Viewed 847

At this point, I'm spinning my wheels, but going nowhere and could use some help. I have an array/List of weekdays that I want to disable on my calendar using the enabledPredicateDay function. for example ['sunday', 'monday', 'tuesday'].

return TableCalendar(
  calendarBuilders: CalendarBuilders(dowBuilder: (context, day) {
    if (day.weekday == DateTime.sunday) {
      final text = DateFormat.E().format(day);
      return Center(
        child: Text(
          text,
          style: TextStyle(color: Colors.red),
        ),
      );
    }
  }),
  firstDay: kFirstDay,
  lastDay: kLastDay,
  focusedDay: _focusedDay,
  calendarFormat: _calendarFormat,
  enabledDayPredicate: (date) {
    // this is where we disable any days that the user is not available OR is booked up

    var disabledDays = "";
    unavailableDays.asMap().forEach((index, value) {
      if (index == 0) {
        disabledDays = disabledDays + "date.weekday != DateTime.$value";
      } else {
        disabledDays = disabledDays + " && date.weekday != DateTime.$value";
      }
    });
    
    date.weekday != disabledDays; // here I'm trying to get some sort of boolean back.

    return date.weekday != disabledDays;

  }

The problem I'm running into is that I can either only return one day programmatically, let's say in a 'for in' loop, or I need to hardcode it. This is because the return value needs to be a boolean.

I tried combining everything into a string and then converting it to a method so that each day I need disabled can be the return statement like this:

return date.weekday != DateTime.$value && date.weekday != DateTime.$value && date.weekday != DateTime.$value

Everything works fine if I hardcode it like this:

return date.weekday != DateTime.sunday && date.weekday != DateTime.monday && date.weekday != DateTime.tuesday

But I haven't been able to get to this point because I'm getting a string back, but I need a bool.

How do I return multiple days to this particular function?

1 Answers

I figured it out...seems very hacky and maybe this can be improved. What I did here was instantiated a variable for each day. And then, as I go through the weekdays to check which is unavailable with conditional statements, I assign the weekday to the variable if and only if it's unavailable.

var unavailableDay1;
var unavailableDay2;
var unavailableDay3;
var unavailableDay4;
var unavailableDay5;
var unavailableDay6;
var unavailableDay7;

for (var day in daysOfWeek) {
  // print(widget.data[day]);
  if (widget.data[day] == null) {
    unavailableDays.add(day);
  }
}
for (var day in unavailableDays) {
  if (day == "monday") {
    unavailableDay1 = 1;
  } else if (day == "tuesday") {
    unavailableDay2 = 2;
  } else if (day == "wednesday") {
    unavailableDay3 = 3;
  } else if (day == "thursday") {
    unavailableDay4 = 4;
  } else if (day == "friday") {
    unavailableDay5 = 5;
  } else if (day == "saturday") {
    unavailableDay6 = 6;
  } else if (day == "sunday") {
    unavailableDay7 = 7;
  }
}

Finally, I check if each of the 7 days has a value, if not null is returned. Thus, it gives me back only days that are enabled! I hope this helps someone! Please help me improve this!

enabledDayPredicate: (date) {
    // this is where we disable any days that the user is not available OR is booked up
    return date.weekday != unavailableDay1 &&
        date.weekday != unavailableDay2 &&
        date.weekday != unavailableDay3 &&
        date.weekday != unavailableDay4 &&
        date.weekday != unavailableDay5 &&
        date.weekday != unavailableDay6 &&
        date.weekday != unavailableDay7;
  },
Related