Skip to content

Commit 083e19b

Browse files
author
Shubham Jitiya
committed
:feat: Fixes issue #382: 🚧 Added support for 3-Days week view
1 parent 469cc29 commit 083e19b

File tree

4 files changed

+187
-53
lines changed

4 files changed

+187
-53
lines changed

example/lib/widgets/week_view_widget.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ class WeekViewWidget extends StatelessWidget {
1212
@override
1313
Widget build(BuildContext context) {
1414
return WeekView(
15+
minDay: DateTime(2024),
16+
maxDay: DateTime(2026),
1517
key: state,
1618
width: width,
1719
showWeekends: true,
1820
showLiveTimeLineInAllDays: true,
21+
showThreeDaysView: true,
22+
// startDay: WeekDays.tuesday,
1923
eventArranger: SideEventArranger(maxWidth: 30),
2024
timeLineWidth: 65,
2125
scrollPhysics: const BouncingScrollPhysics(),

lib/src/extensions.dart

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,26 @@ extension DateTimeExtensions on DateTime {
3636
.abs();
3737

3838
/// Gets difference of weeks between [date] and calling object.
39-
int getWeekDifference(DateTime date, {WeekDays start = WeekDays.monday}) =>
40-
(firstDayOfWeek(start: start)
41-
.difference(date.firstDayOfWeek(start: start))
42-
.inDays
43-
.abs() /
44-
7)
45-
.ceil();
39+
int getWeekDifference(
40+
DateTime date, {
41+
WeekDays start = WeekDays.monday,
42+
bool isThreeDaysView = false,
43+
}) {
44+
final thisFirstDay = firstDayOfWeek(
45+
start: start,
46+
isThreeDaysView: isThreeDaysView,
47+
);
48+
final otherFirstDay = date.firstDayOfWeek(
49+
start: start,
50+
isThreeDaysView: isThreeDaysView,
51+
);
52+
53+
final daysDifference = thisFirstDay.difference(otherFirstDay).inDays.abs();
54+
55+
// Calculate the difference in weeks or 3-day blocks
56+
final divisor = isThreeDaysView ? 3 : 7;
57+
return (daysDifference / divisor).ceil();
58+
}
4659

4760
/// Returns The List of date of Current Week, all of the dates will be without
4861
/// time.
@@ -55,6 +68,7 @@ extension DateTimeExtensions on DateTime {
5568
List<DateTime> datesOfWeek({
5669
WeekDays start = WeekDays.monday,
5770
bool showWeekEnds = true,
71+
bool showThreeDays = false,
5872
}) {
5973
// Here %7 ensure that we do not subtract >6 and <0 days.
6074
// Initial formula is,
@@ -63,31 +77,49 @@ extension DateTimeExtensions on DateTime {
6377
// But in WeekDays enum index ranges from 0 to 6 so we are
6478
// adding 1 in index. So, new formula with WeekDays is,
6579
// difference = (weekdays - (start.index + 1))%7
66-
//
67-
final startDay =
68-
DateTime(year, month, day - (weekday - start.index - 1) % 7);
80+
6981
// Generate weekdays with weekends or without weekends
70-
final days = List.generate(
71-
7,
72-
(index) => DateTime(startDay.year, startDay.month, startDay.day + index),
73-
)
74-
.where(
75-
(date) =>
76-
showWeekEnds ||
77-
(date.weekday != DateTime.saturday &&
78-
date.weekday != DateTime.sunday),
79-
)
80-
.toList();
81-
return days;
82+
83+
final days = showThreeDays ? day : day - (weekday - start.index - 1) % 7;
84+
final startDay = DateTime(year, month, days);
85+
// TODO(Shubham): Update for hide/show weekends
86+
return List.generate(
87+
showThreeDays ? 3 : 7,
88+
(index) => startDay.add(Duration(days: index)),
89+
);
8290
}
8391

8492
/// Returns the first date of week containing the current date
85-
DateTime firstDayOfWeek({WeekDays start = WeekDays.monday}) =>
86-
DateTime(year, month, day - ((weekday - start.index - 1) % 7));
93+
DateTime firstDayOfWeek({
94+
WeekDays start = WeekDays.monday,
95+
bool isThreeDaysView = false,
96+
}) =>
97+
DateTime(
98+
year,
99+
month,
100+
day - ((weekday - start.index - 1) % (isThreeDaysView ? 3 : 7)),
101+
);
87102

88103
/// Returns the last date of week containing the current date
89-
DateTime lastDayOfWeek({WeekDays start = WeekDays.monday}) =>
90-
DateTime(year, month, day + (6 - (weekday - start.index - 1) % 7));
104+
DateTime lastDayOfWeek({
105+
WeekDays start = WeekDays.monday,
106+
bool isThreeDaysView = false,
107+
}) {
108+
if (isThreeDaysView) {
109+
// Calculate the current weekday offset (based on the start day)
110+
final offset = (weekday - start.index) % 7;
111+
112+
// For a 3-day view, calculate the remaining days to the end of the 3-day block
113+
final daysToAdd =
114+
(3 - offset % 3) % 3; // Ensures wrap-around within 3-day blocks
115+
final lastDay = DateTime(year, month, day + daysToAdd);
116+
debugPrint('Last Day for 3-Day View: $lastDay');
117+
debugPrint('Last Day: ${lastDay}');
118+
return lastDay;
119+
} else {
120+
return DateTime(year, month, day + (6 - (weekday - start.index - 1) % 7));
121+
}
122+
}
91123

92124
/// Returns list of all dates of [month].
93125
/// All the dates are week based that means it will return array of size 42

lib/src/week_view/_internal_week_view_page.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ class InternalWeekViewPage<T extends Object?> extends StatefulWidget {
139139
/// Flag to display quarter hours
140140
final bool showQuarterHours;
141141

142+
/// Enable this flag to show 3-days view default is false.
143+
/// i.e 7 days view
144+
final bool showThreeDaysView;
145+
142146
/// Emulate vertical line offset from hour line starts.
143147
final double emulateVerticalOffsetBy;
144148

@@ -205,6 +209,7 @@ class InternalWeekViewPage<T extends Object?> extends StatefulWidget {
205209
required this.showWeekDayAtBottom,
206210
required this.showHalfHours,
207211
required this.showQuarterHours,
212+
required this.showThreeDaysView,
208213
required this.emulateVerticalOffsetBy,
209214
required this.onTileDoubleTap,
210215
required this.endHour,

0 commit comments

Comments
 (0)