datesOfWeek method

List<DateTime> datesOfWeek({
  1. WeekDays start = WeekDays.monday,
})

Returns The List of date of Current Week, all of the dates will be without time. Day will start from Monday to Sunday.

ex: if Current Date instance is 8th and day is wednesday then weekDates will return dates 6,7,8,9,10,11,12 Where on 6th there will be monday and on 12th there will be Sunday

Implementation

List<DateTime> datesOfWeek({WeekDays start = WeekDays.monday}) {
  // Here %7 ensure that we do not subtract >6 and <0 days.
  // Initial formula is,
  //    difference = (weekday - startInt)%7
  // where weekday and startInt ranges from 1 to 7.
  // But in WeekDays enum index ranges from 0 to 6 so we are
  // adding 1 in index. So, new formula with WeekDays is,
  //    difference = (weekdays - (start.index + 1))%7
  //
  final startDay =
      DateTime(year, month, day - (weekday - start.index - 1) % 7);

  return [
    startDay,
    DateTime(startDay.year, startDay.month, startDay.day + 1),
    DateTime(startDay.year, startDay.month, startDay.day + 2),
    DateTime(startDay.year, startDay.month, startDay.day + 3),
    DateTime(startDay.year, startDay.month, startDay.day + 4),
    DateTime(startDay.year, startDay.month, startDay.day + 5),
    DateTime(startDay.year, startDay.month, startDay.day + 6),
  ];
}