diff --git a/example/lib/widgets/week_view_widget.dart b/example/lib/widgets/week_view_widget.dart index 765e8fcb..209f8a0e 100644 --- a/example/lib/widgets/week_view_widget.dart +++ b/example/lib/widgets/week_view_widget.dart @@ -15,6 +15,7 @@ class WeekViewWidget extends StatelessWidget { key: state, width: width, showLiveTimeLineInAllDays: true, + showThreeDaysView: true, eventArranger: SideEventArranger(maxWidth: 30), timeLineWidth: 65, scrollPhysics: const BouncingScrollPhysics(), diff --git a/lib/src/components/headers/week_page_header.dart b/lib/src/components/headers/week_page_header.dart index c7efaa3e..3c2506f9 100644 --- a/lib/src/components/headers/week_page_header.dart +++ b/lib/src/components/headers/week_page_header.dart @@ -40,6 +40,7 @@ class WeekPageHeader extends CalendarPageHeader { headerStyle: headerStyle, ); + // TODO(Shubham): Update this for 3-days view static String _weekStringBuilder(DateTime date, {DateTime? secondaryDate}) => "${date.day} / ${date.month} / ${date.year} to " "${secondaryDate != null ? "${secondaryDate.day} / " diff --git a/lib/src/extensions.dart b/lib/src/extensions.dart index ef54a08e..a3be0a4f 100644 --- a/lib/src/extensions.dart +++ b/lib/src/extensions.dart @@ -52,7 +52,10 @@ extension DateTimeExtensions on DateTime { /// will return dates /// [6,7,8,9,10,11,12] /// Where on 6th there will be monday and on 12th there will be Sunday - List datesOfWeek({WeekDays start = WeekDays.monday}) { + List datesOfWeek({ + bool showThreeDays = false, + WeekDays start = WeekDays.monday, + }) { // Here %7 ensure that we do not subtract >6 and <0 days. // Initial formula is, // difference = (weekday - startInt)%7 @@ -61,18 +64,12 @@ extension DateTimeExtensions on DateTime { // 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), - ]; + final days = showThreeDays ? day : day - (weekday - start.index - 1) % 7; + final startDay = DateTime(year, month, days); + + return List.generate(showThreeDays ? 3 : 7, (index) { + return startDay.add(Duration(days: index)); + }); } /// Returns the first date of week containing the current date diff --git a/lib/src/week_view/_internal_week_view_page.dart b/lib/src/week_view/_internal_week_view_page.dart index 85d36b54..fda7e824 100644 --- a/lib/src/week_view/_internal_week_view_page.dart +++ b/lib/src/week_view/_internal_week_view_page.dart @@ -139,6 +139,10 @@ class InternalWeekViewPage 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; @@ -205,6 +209,7 @@ class InternalWeekViewPage extends StatefulWidget { required this.showWeekDayAtBottom, required this.showHalfHours, required this.showQuarterHours, + required this.showThreeDaysView, required this.emulateVerticalOffsetBy, required this.onTileDoubleTap, required this.endHour, @@ -513,7 +518,6 @@ class _InternalWeekViewPageState output.add(date); } } - return output; } } diff --git a/lib/src/week_view/week_view.dart b/lib/src/week_view/week_view.dart index 6b0001d0..6649e0dd 100644 --- a/lib/src/week_view/week_view.dart +++ b/lib/src/week_view/week_view.dart @@ -172,6 +172,10 @@ class WeekView 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. @@ -287,6 +291,7 @@ class WeekView extends StatefulWidget { this.onDateTap, this.weekDays = WeekDays.values, this.showWeekends = true, + this.showThreeDaysView = false, this.startDay = WeekDays.monday, this.minuteSlotSize = MinuteSlotSize.minutes60, this.weekDetectorBuilder, @@ -333,6 +338,10 @@ class WeekView 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 @@ -514,9 +523,7 @@ class WeekViewState extends State> { physics: widget.pageViewPhysics, onPageChanged: _onPageChange, itemBuilder: (_, index) { - final dates = DateTime(_minDate.year, _minDate.month, - _minDate.day + (index * DateTime.daysPerWeek)) - .datesOfWeek(start: widget.startDay); + final dates = getDatesOnWeek(index); return ValueListenableBuilder( valueListenable: _scrollConfiguration, @@ -565,6 +572,7 @@ class WeekViewState extends State> { startHour: _startHour, showHalfHours: widget.showHalfHours, showQuarterHours: widget.showQuarterHours, + showThreeDaysView: widget.showThreeDaysView, emulateVerticalOffsetBy: widget.emulateVerticalOffsetBy, showWeekDayAtBottom: widget.showWeekDayAtBottom, @@ -623,7 +631,8 @@ class WeekViewState extends State> { "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; + // TODO(Shubham): Update weekdays to 3 + _totalDaysInWeek = widget.showThreeDaysView ? 3 : _weekDays.length; } void _updateViewDimensions() { @@ -648,6 +657,7 @@ class WeekViewState extends State> { assert(_hourIndicatorSettings.height < _hourHeight, "hourIndicator height must be less than minuteHeight * 60"); + // TODO(Shubham): Update total days in week _weekTitleWidth = (_width - _timeLineWidth - _hourIndicatorSettings.offset) / _totalDaysInWeek; @@ -716,9 +726,8 @@ class WeekViewState extends State> { } else if (_currentWeek.isAfter(_maxDate)) { _currentWeek = _maxDate; } - _currentStartDate = _currentWeek.firstDayOfWeek(start: widget.startDay); - _currentEndDate = _currentWeek.lastDayOfWeek(start: widget.startDay); + _currentEndDate = _currentWeek.add(Duration(days: 2)); _currentIndex = _minDate.getWeekDifference(_currentEndDate, start: widget.startDay); } @@ -728,7 +737,6 @@ class WeekViewState extends State> { _minDate = (widget.minDay ?? CalendarConstants.epochDate) .firstDayOfWeek(start: widget.startDay) .withoutTime; - _maxDate = (widget.maxDay ?? CalendarConstants.maxDate) .lastDayOfWeek(start: widget.startDay) .withoutTime; @@ -781,7 +789,11 @@ class WeekViewState extends State> { } /// Default builder for week number. + // TODO(Shubham): Update for 3 day view Widget _defaultWeekNumberBuilder(DateTime date) { + if (widget.showThreeDaysView) { + return SizedBox.shrink(); + } final daysToAdd = DateTime.thursday - date.weekday; final thursday = daysToAdd > 0 ? date.add(Duration(days: daysToAdd)) @@ -886,9 +898,9 @@ class WeekViewState extends State> { _currentStartDate = DateTime( _currentStartDate.year, _currentStartDate.month, - _currentStartDate.day + (index - _currentIndex) * 7, + _currentStartDate.day + (index - _currentIndex) * 3, ); - _currentEndDate = _currentStartDate.add(Duration(days: 6)); + _currentEndDate = _currentStartDate.add(Duration(days: 2)); _currentIndex = index; }); } @@ -1028,6 +1040,22 @@ class WeekViewState extends State> { void _scrollPageListener(ScrollController controller) { _lastScrollOffset = controller.offset; } + + List getDatesOnWeek(int index) { + if (widget.showThreeDaysView) { + final dates = _currentStartDate.datesOfWeek( + start: widget.startDay, + showThreeDays: widget.showThreeDaysView, + ); + return dates; + } else { + return DateTime( + _minDate.year, + _minDate.month, + _minDate.day + (index * DateTime.daysPerWeek), + ).datesOfWeek(start: widget.startDay); + } + } } class WeekHeader {