DateTime addDay(DateTime date, int days) { return date.add(Duration(days: days)); } DateTime subtractDay(DateTime date, int days) { return date.subtract(Duration(days: days)); } DateTime firstDayOfWeek(DateTime date) { if (date.weekday == DateTime.sunday) { return date; } return date.subtract(Duration(days: date.weekday)); } // =================================== 获取一周的数据 ↓ =================================== List getWeekdays(DateTime date, int at) { final first = firstDayOfWeek(date); if (at >= 0) { return _addWeek(first, at); } else { return _subtractWeek(first, -at); } } List _addWeek(DateTime date, int add) { final day = addDay(date, 7 * add); return _getWeekDaysAt(day); } List _subtractWeek(DateTime date, int subtract) { final day = subtractDay(date, 7 * subtract); return _getWeekDaysAt(day); } List _getWeekDaysAt(DateTime date) { return List.generate(7, (index) => index) .map((index) => date.add(Duration(days: index))) .toList(); } // =================================== 获取二周的数据 ↓ =================================== List get2Weekdays(DateTime date, int at) { final first = firstDayOfWeek(date); if (at >= 0) { return _add2Week(first, at); } else { return _subtract2Week(first, -at); } } List _add2Week(DateTime date, int add) { final day = addDay(date, 14 * add); return _get2WeekDaysAt(day); } List _subtract2Week(DateTime date, int subtract) { final day = subtractDay(date, 14 * subtract); return _get2WeekDaysAt(day); } List _get2WeekDaysAt(DateTime date) { return List.generate(14, (index) => index) .map((index) => date.add(Duration(days: index))) .toList(); }