From d108fd2076cfd1cf3e9daf9fd56140f4f32ef943 Mon Sep 17 00:00:00 2001 From: Shubham Jitiya Date: Wed, 11 Dec 2024 20:23:11 +0530 Subject: [PATCH] fix: Fixes issue #426: Fixed header style icons visibility on min & max dates. --- .../headers/calendar_page_header.dart | 72 +++++++++++-------- lib/src/day_view/day_view.dart | 16 ++++- lib/src/month_view/month_view.dart | 18 ++++- lib/src/style/header_style.dart | 30 ++++++++ lib/src/week_view/week_view.dart | 14 ++++ 5 files changed, 118 insertions(+), 32 deletions(-) diff --git a/lib/src/components/headers/calendar_page_header.dart b/lib/src/components/headers/calendar_page_header.dart index 21086ea8..6eb97dfe 100644 --- a/lib/src/components/headers/calendar_page_header.dart +++ b/lib/src/components/headers/calendar_page_header.dart @@ -105,23 +105,31 @@ class CalendarPageHeader extends StatelessWidget { mainAxisAlignment: headerStyle.mainAxisAlignment, children: [ if (headerStyle.leftIconVisible && headerStyle.leftIconConfig != null) - headerStyle.leftIconConfig!.icon?.call(context) ?? - IconButton( - onPressed: onPreviousDay, - splashColor: Colors.transparent, - focusColor: Colors.transparent, - hoverColor: Colors.transparent, - highlightColor: Colors.transparent, - padding: headerStyle.leftIconPadding ?? - headerStyle.leftIconConfig!.padding, - icon: headerStyle.leftIcon ?? - Icon( - Icons.chevron_left, - size: headerStyle.leftIconConfig!.size, - color: iconColor ?? headerStyle.leftIconConfig!.color, - ), - ), + Expanded( + child: Align( + alignment: Alignment.centerLeft, + child: headerStyle.leftIconConfig!.icon?.call(context) ?? + IconButton( + onPressed: onPreviousDay, + splashColor: Colors.transparent, + focusColor: Colors.transparent, + hoverColor: Colors.transparent, + highlightColor: Colors.transparent, + padding: headerStyle.leftIconPadding ?? + headerStyle.leftIconConfig!.padding, + icon: headerStyle.leftIcon ?? + Icon( + Icons.chevron_left, + size: headerStyle.leftIconConfig!.size, + color: + iconColor ?? headerStyle.leftIconConfig!.color, + ), + ), + ), + ), + if (headerStyle.leftIconConfig == null) Spacer(flex: 1), Expanded( + flex: 3, child: titleBuilder != null ? DefaultTextStyle.merge( style: headerStyle.headerTextStyle, @@ -143,20 +151,26 @@ class CalendarPageHeader extends StatelessWidget { ), if (headerStyle.rightIconVisible && headerStyle.rightIconConfig != null) - IconButton( - onPressed: onNextDay, - splashColor: Colors.transparent, - focusColor: Colors.transparent, - hoverColor: Colors.transparent, - highlightColor: Colors.transparent, - padding: headerStyle.rightIconPadding, - icon: headerStyle.rightIcon ?? - Icon( - Icons.chevron_right, - size: headerStyle.rightIconConfig?.size, - color: iconColor ?? headerStyle.rightIconConfig?.color, - ), + Expanded( + child: Align( + alignment: Alignment.centerRight, + child: IconButton( + onPressed: onNextDay, + splashColor: Colors.transparent, + focusColor: Colors.transparent, + hoverColor: Colors.transparent, + highlightColor: Colors.transparent, + padding: headerStyle.rightIconPadding, + icon: headerStyle.rightIcon ?? + Icon( + Icons.chevron_right, + size: headerStyle.rightIconConfig?.size, + color: iconColor ?? headerStyle.rightIconConfig?.color, + ), + ), + ), ), + if (headerStyle.rightIconConfig == null) Spacer(flex: 1), ], ), ); diff --git a/lib/src/day_view/day_view.dart b/lib/src/day_view/day_view.dart index 35e76bf4..9254f610 100644 --- a/lib/src/day_view/day_view.dart +++ b/lib/src/day_view/day_view.dart @@ -346,6 +346,7 @@ class DayViewState extends State> { ScrollController get scrollController => _scrollController; late VoidCallback _reloadCallback; + late HeaderStyle headerStyle; final _scrollConfiguration = EventScrollConfiguration(); @@ -368,6 +369,7 @@ class DayViewState extends State> { ); _pageController = PageController(initialPage: _currentIndex); _eventArranger = widget.eventArranger ?? SideEventArranger(); + headerStyle = widget.headerStyle; _assignBuilders(); } @@ -701,7 +703,7 @@ class DayViewState extends State> { jumpToDate(selectedDate); } }, - headerStyle: widget.headerStyle, + headerStyle: headerStyle, ); } @@ -756,6 +758,7 @@ class DayViewState extends State> { _currentDate.day + (index - _currentIndex), ); _currentIndex = index; + _updateHeaderIcons(); }); } if (!widget.keepScrollOffset) { @@ -764,6 +767,17 @@ class DayViewState extends State> { widget.onPageChange?.call(_currentDate, _currentIndex); } + // Hide header icons if end dates are reached + void _updateHeaderIcons() { + bool setLeftIconToNull = _currentDate == _minDate; + bool setRightIconToNull = _currentDate == _maxDate; + + headerStyle = widget.headerStyle.copyWith( + setLeftIconConfigToNull: setLeftIconToNull, + setRightIconConfigToNull: setRightIconToNull, + ); + } + /// Animate to next page /// /// Arguments [duration] and [curve] will override default values provided diff --git a/lib/src/month_view/month_view.dart b/lib/src/month_view/month_view.dart index 15cf14d8..925e5ac1 100644 --- a/lib/src/month_view/month_view.dart +++ b/lib/src/month_view/month_view.dart @@ -247,6 +247,8 @@ class MonthViewState extends State> { late VoidCallback _reloadCallback; + late HeaderStyle headerStyle; + @override void initState() { super.initState(); @@ -262,7 +264,7 @@ class MonthViewState extends State> { // Initialize page controller to control page actions. _pageController = PageController(initialPage: _currentIndex); - + headerStyle = widget.headerStyle; _assignBuilders(); } @@ -513,11 +515,23 @@ class MonthViewState extends State> { _currentDate.month + (value - _currentIndex), ); _currentIndex = value; + _updateHeaderIcons(); }); } widget.onPageChange?.call(_currentDate, _currentIndex); } + // Hide header icons if end dates are reached + void _updateHeaderIcons() { + bool setLeftIconToNull = _currentDate == _minDate; + bool setRightIconToNull = _currentDate == _maxDate; + + headerStyle = widget.headerStyle.copyWith( + setLeftIconConfigToNull: setLeftIconToNull, + setRightIconConfigToNull: setRightIconToNull, + ); + } + /// Default month view header builder Widget _defaultHeaderBuilder(DateTime date) { return MonthPageHeader( @@ -540,7 +554,7 @@ class MonthViewState extends State> { date: date, dateStringBuilder: widget.headerStringBuilder, onNextMonth: nextPage, - headerStyle: widget.headerStyle, + headerStyle: headerStyle, ); } diff --git a/lib/src/style/header_style.dart b/lib/src/style/header_style.dart index b7728ff3..ffa5d957 100644 --- a/lib/src/style/header_style.dart +++ b/lib/src/style/header_style.dart @@ -101,6 +101,36 @@ class HeaderStyle { this.rightIconPadding, }); + HeaderStyle copyWith({ + TextStyle? headerTextStyle, + EdgeInsets? headerMargin, + EdgeInsets? headerPadding, + TextAlign? titleAlign, + BoxDecoration? decoration, + MainAxisAlignment? mainAxisAlignment, + IconDataConfig? leftIconConfig, + bool setLeftIconConfigToNull = false, + IconDataConfig? rightIconConfig, + bool setRightIconConfigToNull = false, + MainAxisSize? mainAxisSize, + }) { + return HeaderStyle( + headerTextStyle: headerTextStyle ?? this.headerTextStyle, + headerMargin: headerMargin ?? this.headerMargin, + headerPadding: headerPadding ?? this.headerPadding, + titleAlign: titleAlign ?? this.titleAlign, + decoration: decoration ?? this.decoration, + mainAxisAlignment: mainAxisAlignment ?? this.mainAxisAlignment, + leftIconConfig: setLeftIconConfigToNull + ? null + : (leftIconConfig ?? this.leftIconConfig), + rightIconConfig: setRightIconConfigToNull + ? null + : (rightIconConfig ?? this.rightIconConfig), + mainAxisSize: mainAxisSize ?? this.mainAxisSize, + ); + } + /// Create a `HeaderStyle` of calendar view /// /// Used when you need to use same configs for left and right icons. diff --git a/lib/src/week_view/week_view.dart b/lib/src/week_view/week_view.dart index 81f5d960..4343ee40 100644 --- a/lib/src/week_view/week_view.dart +++ b/lib/src/week_view/week_view.dart @@ -398,6 +398,7 @@ class WeekViewState extends State> { late int _startHour; late int _endHour; + late HeaderStyle headerStyle; final _scrollConfiguration = EventScrollConfiguration(); @@ -430,6 +431,7 @@ class WeekViewState extends State> { _fullDayHeaderTitle = widget.fullDayHeaderTitle; _fullDayHeaderTextConfig = widget.fullDayHeaderTextConfig ?? FullDayHeaderTextConfig(); + headerStyle = widget.headerStyle; } @override @@ -902,11 +904,23 @@ class WeekViewState extends State> { _currentEndDate = _currentStartDate .add(Duration(days: widget.showThreeDaysView ? 2 : 6)); _currentIndex = index; + _updateHeaderIcons(); }); } widget.onPageChange?.call(_currentStartDate, _currentIndex); } + // Hide header icons if end dates are reached + void _updateHeaderIcons() { + bool setLeftIconToNull = _currentStartDate == _minDate; + bool setRightIconToNull = _currentStartDate == _maxDate; + + headerStyle = widget.headerStyle.copyWith( + setLeftIconConfigToNull: setLeftIconToNull, + setRightIconConfigToNull: setRightIconToNull, + ); + } + /// Animate to next page /// /// Arguments [duration] and [curve] will override default values provided