datesOfWeek method

List<DateTime> datesOfWeek()

Returns The List of date of Current Week 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() {
  // weekday can very from 1(Monday) to 7(Sunday).
  // If we have a date that has week day of 3 (Wednesday) and
  // we want to find first date of the week that contains this date
  // we need to move 2 days back. Wednesday -> Tuesday -> Monday.
  // That is same as 3 - 1 (weekday - 1).
  //
  final start = subtract(Duration(days: weekday - 1));

  return [
    start,
    start.add(Duration(days: 1)),
    start.add(Duration(days: 2)),
    start.add(Duration(days: 3)),
    start.add(Duration(days: 4)),
    start.add(Duration(days: 5)),
    start.add(Duration(days: 6)),
  ];
}