datesInMonthGrid method
Returns a grid-like list of DateTime
s in a given month, with either 35 or 42 items.
sixBySeven
will add 7 days of padding in case the list only has 35 items. That is,
a "grid" of 6 rows by 7 columns.
startOnMonday
will start the grid on Monday instead of Sunday.
Implementation
static List<DateTime> datesInMonthGrid(DateTime month,
[bool sixBySeven = false, bool startOnMonday = false]) {
DateTime first = firstDayOfMonth(month);
int daysBefore = startOnMonday ? first.weekday - 1 : first.weekday;
DateTime firstToDisplay = first.subtract(new Duration(days: daysBefore));
DateTime last = lastDayOfMonth(month);
int daysAfter = startOnMonday ? 8 - last.weekday : 7 - last.weekday;
// If the last day is sunday (7) the entire week must be rendered
// Otherwise, if we're ending on Sunday leave it as zero
if (daysAfter == 0 && !startOnMonday) daysAfter = 7;
DateTime lastToDisplay = last.add(new Duration(days: daysAfter));
List<DateTime> grid = datesInRange(firstToDisplay, lastToDisplay).toList();
if (sixBySeven && grid.length < 42 && grid.length >= 35) {
lastToDisplay = lastToDisplay.add(new Duration(days: 42 - grid.length));
grid = datesInRange(firstToDisplay, lastToDisplay).toList();
}
return grid;
}