Skip to content

Commit

Permalink
:feat: Fixes issue #382: Added support for 3-Days week view
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham-jitiya-simform committed Jan 3, 2025
1 parent 501bdb5 commit 60cd573
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
1 change: 1 addition & 0 deletions example/lib/widgets/week_view_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class WeekViewWidget extends StatelessWidget {
width: width,
showWeekends: true,
showLiveTimeLineInAllDays: true,
showThreeDaysView: true,
eventArranger: SideEventArranger(maxWidth: 30),
timeLineWidth: 65,
scrollPhysics: const BouncingScrollPhysics(),
Expand Down
37 changes: 23 additions & 14 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ extension DateTimeExtensions on DateTime {
List<DateTime> datesOfWeek({
WeekDays start = WeekDays.monday,
bool showWeekEnds = true,
bool showThreeDays = false,
}) {
// Here %7 ensure that we do not subtract >6 and <0 days.
// Initial formula is,
Expand All @@ -63,21 +64,29 @@ extension DateTimeExtensions on DateTime {
// 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);
// // Generate weekdays with weekends or without weekends
// final days = List.generate(
// 7,
// (index) => DateTime(startDay.year, startDay.month, startDay.day + index),
// )
// .where(
// (date) =>
// showWeekEnds ||
// (date.weekday != DateTime.saturday &&
// date.weekday != DateTime.sunday),
// )
// .toList();

// Three days view
// final days = showThreeDays ? day : day - (weekday - start.index - 1) % 7;
// final startDay = DateTime(year, month, days);
//
final startDay =
DateTime(year, month, day - (weekday - start.index - 1) % 7);
// Generate weekdays with weekends or without weekends
final days = List.generate(
7,
(index) => DateTime(startDay.year, startDay.month, startDay.day + index),
)
.where(
(date) =>
showWeekEnds ||
(date.weekday != DateTime.saturday &&
date.weekday != DateTime.sunday),
)
.toList();
// return List.generate(showThreeDays ? 3 : 7, (index) {
// return startDay.add(Duration(days: index));
// });
return days;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/src/week_view/_internal_week_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ class InternalWeekViewPage<T extends Object?> extends StatefulWidget {
/// Flag to display quarter hours
final bool showQuarterHours;

/// Enable this flag to show 3-days view default is false.
/// i.e 7 days view
final bool showThreeDaysView;

/// Emulate vertical line offset from hour line starts.
final double emulateVerticalOffsetBy;

Expand Down Expand Up @@ -205,6 +209,7 @@ class InternalWeekViewPage<T extends Object?> extends StatefulWidget {
required this.showWeekDayAtBottom,
required this.showHalfHours,
required this.showQuarterHours,
required this.showThreeDaysView,
required this.emulateVerticalOffsetBy,
required this.onTileDoubleTap,
required this.endHour,
Expand Down
40 changes: 36 additions & 4 deletions lib/src/week_view/week_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ class WeekView<T extends Object?> extends StatefulWidget {
/// saturday and sunday, only monday and tuesday will be visible in week view.
final bool showWeekends;

/// Enable this flag to show 3-days view default is false.
/// i.e 7 days view
final bool showThreeDaysView;

/// Defines which days should be displayed in one week.
///
/// By default all the days will be visible.
Expand Down Expand Up @@ -287,6 +291,7 @@ class WeekView<T extends Object?> extends StatefulWidget {
this.onDateTap,
this.weekDays = WeekDays.values,
this.showWeekends = true,
this.showThreeDaysView = false,
this.startDay = WeekDays.monday,
this.minuteSlotSize = MinuteSlotSize.minutes60,
this.weekDetectorBuilder,
Expand Down Expand Up @@ -333,6 +338,10 @@ class WeekView<T extends Object?> extends StatefulWidget {
endHour <= Constants.hoursADay || endHour < startHour,
"End hour must be less than 24 or startHour must be less than endHour",
),
assert(!(showThreeDaysView && !showWeekends),
"For three days view, showWeekends should be true"),
assert(!(showThreeDaysView && weekDays.length != 7),
"For three days view, weekDays should not be set"),
super(key: key);

@override
Expand Down Expand Up @@ -526,6 +535,9 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
showWeekEnds: widget.showWeekends,
);

// TODO(Shubham): Three days view
// final dates = _getDatesOnWeek(index);

return ValueListenableBuilder(
valueListenable: _scrollConfiguration,
builder: (_, __, ___) => InternalWeekViewPage<T>(
Expand Down Expand Up @@ -573,6 +585,7 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
startHour: _startHour,
showHalfHours: widget.showHalfHours,
showQuarterHours: widget.showQuarterHours,
showThreeDaysView: widget.showThreeDaysView,
emulateVerticalOffsetBy:
widget.emulateVerticalOffsetBy,
showWeekDayAtBottom: widget.showWeekDayAtBottom,
Expand Down Expand Up @@ -631,7 +644,7 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
"Make sure you are providing weekdays in initialization of "
"WeekView. or showWeekends is true if you are providing only "
"saturday or sunday in weekDays.");
_totalDaysInWeek = _weekDays.length;
_totalDaysInWeek = widget.showThreeDaysView ? 3 : _weekDays.length;
}

void _updateViewDimensions() {
Expand Down Expand Up @@ -724,7 +737,6 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
} else if (_currentWeek.isAfter(_maxDate)) {
_currentWeek = _maxDate;
}

_currentStartDate = _currentWeek.firstDayOfWeek(start: widget.startDay);
_currentEndDate = _currentWeek.lastDayOfWeek(start: widget.startDay);
_currentIndex =
Expand Down Expand Up @@ -790,6 +802,9 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {

/// Default builder for week number.
Widget _defaultWeekNumberBuilder(DateTime date) {
if (widget.showThreeDaysView) {
return const SizedBox.shrink();
}
final daysToAdd = DateTime.thursday - date.weekday;
final thursday = daysToAdd > 0
? date.add(Duration(days: daysToAdd))
Expand Down Expand Up @@ -894,9 +909,11 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
_currentStartDate = DateTime(
_currentStartDate.year,
_currentStartDate.month,
_currentStartDate.day + (index - _currentIndex) * 7,
_currentStartDate.day +
(index - _currentIndex) * (widget.showThreeDaysView ? 3 : 7),
);
_currentEndDate = _currentStartDate.add(Duration(days: 6));
_currentEndDate = _currentStartDate
.add(Duration(days: widget.showThreeDaysView ? 2 : 6));
_currentIndex = index;
});
}
Expand Down Expand Up @@ -1036,6 +1053,21 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
void _scrollPageListener(ScrollController controller) {
_lastScrollOffset = controller.offset;
}

List<DateTime> _getDatesOnWeek(int index) {
if (widget.showThreeDaysView) {
return _currentStartDate.datesOfWeek(
start: widget.startDay,
showThreeDays: widget.showThreeDaysView,
);
} else {
return DateTime(
_minDate.year,
_minDate.month,
_minDate.day + (index * DateTime.daysPerWeek),
).datesOfWeek(start: widget.startDay);
}
}
}

class WeekHeader {
Expand Down

0 comments on commit 60cd573

Please sign in to comment.