getFirstDayOfWeekDate function

dynamic getFirstDayOfWeekDate(
  1. int visibleDatesCount,
  2. dynamic date,
  3. int firstDayOfWeek
)

Calculate first day of week date value based original date with first day of week value.

Implementation

dynamic getFirstDayOfWeekDate(
    int visibleDatesCount, dynamic date, int firstDayOfWeek) {
  if (visibleDatesCount % 7 != 0) {
    return date;
  }

  const int numberOfWeekDays = 7;
  dynamic currentDate = date;
  if (visibleDatesCount == 42) {
    if (currentDate is HijriDateTime) {
      currentDate = HijriDateTime(currentDate.year, currentDate.month, 1);
    } else {
      currentDate = DateTime(currentDate.year, currentDate.month);
    }
  }

  // ignore: avoid_as
  int value = -(currentDate.weekday as int) + firstDayOfWeek - numberOfWeekDays;
  if (value.abs() >= numberOfWeekDays) {
    value += numberOfWeekDays;
  }

  currentDate = addDays(currentDate, value);
  return currentDate;
}