From ced27373a2f0fd9761be21e71e964ee7f6f6d3e2 Mon Sep 17 00:00:00 2001 From: Jaimin Rana Date: Thu, 10 Aug 2023 11:09:53 +0530 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9BAdded=20a=20callback=20for?= =?UTF-8?q?=20the=20header=20title=20#241.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++- lib/src/day_view/day_view.dart | 46 +++++++++++++++++++----------- lib/src/month_view/month_view.dart | 46 +++++++++++++++++++----------- lib/src/typedefs.dart | 12 +++++--- lib/src/week_view/week_view.dart | 43 +++++++++++++++++++--------- 5 files changed, 101 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3180dfd..5fe18e74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ -# [1.0.5] (UnReleased) +# [1.0.5] (UnReleased)(https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/tree/1.0.5) - Fixed issue related to auto scroll to initial duration for day view-[#269](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/issues/269) +- Fixed + issue added a callback for the header title + - [#241](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/issues/241) # [1.0.4 - 9 Aug 2023](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/tree/1.0.4) diff --git a/lib/src/day_view/day_view.dart b/lib/src/day_view/day_view.dart index 6ae8d333..bd53391c 100644 --- a/lib/src/day_view/day_view.dart +++ b/lib/src/day_view/day_view.dart @@ -43,7 +43,7 @@ class DayView extends StatefulWidget { final DateWidgetBuilder? timeLineBuilder; /// Builds day title bar. - final DateWidgetBuilder? dayTitleBuilder; + final HeaderBuilder? dayTitleBuilder; /// Builds custom PressDetector widget /// @@ -192,6 +192,12 @@ class DayView extends StatefulWidget { /// By default it will be Duration(hours:0) final Duration startDuration; + /// Callback for the Header title + final HeaderTitleCallback? onHeaderTitleTap; + + /// Page controller to control page actions. + final PageController? pageController; + /// Main widget for day view. const DayView({ Key? key, @@ -232,6 +238,8 @@ class DayView extends StatefulWidget { this.showHalfHours = false, this.halfHourIndicatorSettings, this.startDuration = const Duration(hours: 0), + this.onHeaderTitleTap, + this.pageController, }) : assert(timeLineOffset >= 0, "timeLineOffset must be greater than or equal to 0"), assert(width == null || width > 0, @@ -274,7 +282,7 @@ class DayViewState extends State> { late EventTileBuilder _eventTileBuilder; - late DateWidgetBuilder _dayTitleBuilder; + late HeaderBuilder _dayTitleBuilder; late FullDayEventBuilder _fullDayEventBuilder; @@ -305,7 +313,8 @@ class DayViewState extends State> { _scrollController = ScrollController( initialScrollOffset: widget.scrollOffset ?? widget.startDuration.inMinutes * widget.heightPerMinute); - _pageController = PageController(initialPage: _currentIndex); + _pageController = + widget.pageController ?? PageController(initialPage: _currentIndex); _eventArranger = widget.eventArranger ?? SideEventArranger(); _assignBuilders(); } @@ -381,7 +390,10 @@ class DayViewState extends State> { crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ - _dayTitleBuilder(_currentDate), + _dayTitleBuilder( + _currentDate, + onHeaderTitleTap: widget.onHeaderTitleTap, + ), Expanded( child: DecoratedBox( decoration: BoxDecoration(color: widget.backgroundColor), @@ -632,23 +644,25 @@ class DayViewState extends State> { /// Default view header builder. This builder will be used if /// [widget.dayTitleBuilder] is null. /// - Widget _defaultDayBuilder(DateTime date) { + Widget _defaultDayBuilder(DateTime date, + {HeaderTitleCallback? onHeaderTitleTap}) { return DayPageHeader( date: _currentDate, dateStringBuilder: widget.dateStringBuilder, onNextDay: nextPage, onPreviousDay: previousPage, - onTitleTapped: () async { - final selectedDate = await showDatePicker( - context: context, - initialDate: date, - firstDate: _minDate, - lastDate: _maxDate, - ); - - if (selectedDate == null) return; - jumpToDate(selectedDate); - }, + onTitleTapped: onHeaderTitleTap ?? + () async { + final selectedDate = await showDatePicker( + context: context, + initialDate: date, + firstDate: _minDate, + lastDate: _maxDate, + ); + + if (selectedDate == null) return; + jumpToDate(selectedDate); + }, headerStyle: widget.headerStyle, ); } diff --git a/lib/src/month_view/month_view.dart b/lib/src/month_view/month_view.dart index 11a794bd..27363e12 100644 --- a/lib/src/month_view/month_view.dart +++ b/lib/src/month_view/month_view.dart @@ -24,7 +24,7 @@ class MonthView extends StatefulWidget { /// Builds month page title. /// /// Used default title builder if null. - final DateWidgetBuilder? headerBuilder; + final HeaderBuilder? headerBuilder; /// This function will generate DateString in the calendar header. /// Useful for I18n @@ -138,6 +138,12 @@ class MonthView extends StatefulWidget { /// Option for SafeArea. final SafeAreaOption safeAreaOption; + /// Callback for the Header title + final HeaderTitleCallback? onHeaderTitleTap; + + /// Page controller to control page actions. + final PageController? pageController; + /// Main [Widget] to display month view. const MonthView({ Key? key, @@ -166,6 +172,8 @@ class MonthView extends StatefulWidget { this.weekDayStringBuilder, this.headerStyle = const HeaderStyle(), this.safeAreaOption = const SafeAreaOption(), + this.onHeaderTitleTap, + this.pageController, }) : super(key: key); @override @@ -195,7 +203,7 @@ class MonthViewState extends State> { late WeekDayBuilder _weekBuilder; - late DateWidgetBuilder _headerBuilder; + late HeaderBuilder _headerBuilder; EventController? _controller; @@ -215,7 +223,8 @@ class MonthViewState extends State> { _regulateCurrentDate(); // Initialize page controller to control page actions. - _pageController = PageController(initialPage: _currentIndex); + _pageController = + widget.pageController ?? PageController(initialPage: _currentIndex); _assignBuilders(); } @@ -288,7 +297,10 @@ class MonthViewState extends State> { children: [ Container( width: _width, - child: _headerBuilder(_currentDate), + child: _headerBuilder( + _currentDate, + onHeaderTitleTap: widget.onHeaderTitleTap, + ), ), Expanded( child: PageView.builder( @@ -459,19 +471,21 @@ class MonthViewState extends State> { } /// Default month view header builder - Widget _defaultHeaderBuilder(DateTime date) { + Widget _defaultHeaderBuilder(DateTime date, + {HeaderTitleCallback? onHeaderTitleTap}) { return MonthPageHeader( - onTitleTapped: () async { - final selectedDate = await showDatePicker( - context: context, - initialDate: date, - firstDate: _minDate, - lastDate: _maxDate, - ); - - if (selectedDate == null) return; - jumpToMonth(selectedDate); - }, + onTitleTapped: onHeaderTitleTap ?? + () async { + final selectedDate = await showDatePicker( + context: context, + initialDate: date, + firstDate: _minDate, + lastDate: _maxDate, + ); + + if (selectedDate == null) return; + jumpToMonth(selectedDate); + }, onPreviousMonth: previousPage, date: date, dateStringBuilder: widget.headerStringBuilder, diff --git a/lib/src/typedefs.dart b/lib/src/typedefs.dart index 76f0b64d..b495812d 100644 --- a/lib/src/typedefs.dart +++ b/lib/src/typedefs.dart @@ -34,9 +34,12 @@ typedef WeekDayBuilder = Widget Function( int day, ); -typedef DateWidgetBuilder = Widget Function( - DateTime date, -); +typedef DateWidgetBuilder = Widget Function(DateTime date); + +typedef HeaderBuilder = Widget Function(DateTime date, + {HeaderTitleCallback? onHeaderTitleTap}); + +typedef HeaderTitleCallback = Future Function(); typedef WeekNumberBuilder = Widget? Function( DateTime firstDayOfWeek, @@ -56,7 +59,8 @@ typedef StringProvider = String Function(DateTime date, {DateTime? secondaryDate}); typedef WeekPageHeaderBuilder = Widget Function( - DateTime startDate, DateTime endDate); + DateTime startDate, DateTime endDate, + {HeaderTitleCallback? onHeaderTitleTap}); typedef TileTapCallback = void Function( CalendarEventData event, DateTime date); diff --git a/lib/src/week_view/week_view.dart b/lib/src/week_view/week_view.dart index 8357e98c..dd074047 100644 --- a/lib/src/week_view/week_view.dart +++ b/lib/src/week_view/week_view.dart @@ -183,6 +183,12 @@ class WeekView extends StatefulWidget { /// Display full day event builder. final FullDayEventBuilder? fullDayEventBuilder; + /// Callback for the Header title + final HeaderTitleCallback? onHeaderTitleTap; + + /// Page controller to control page actions. + final PageController? pageController; + /// Main widget for week view. const WeekView({ Key? key, @@ -224,6 +230,8 @@ class WeekView extends StatefulWidget { this.headerStyle = const HeaderStyle(), this.safeAreaOption = const SafeAreaOption(), this.fullDayEventBuilder, + this.onHeaderTitleTap, + this.pageController, }) : assert((timeLineOffset) >= 0, "timeLineOffset must be greater than or equal to 0"), assert(width == null || width > 0, @@ -302,7 +310,8 @@ class WeekViewState extends State> { _calculateHeights(); _scrollController = ScrollController(initialScrollOffset: widget.scrollOffset); - _pageController = PageController(initialPage: _currentIndex); + _pageController = + widget.pageController ?? PageController(initialPage: _currentIndex); _eventArranger = widget.eventArranger ?? SideEventArranger(); _assignBuilders(); @@ -381,7 +390,11 @@ class WeekViewState extends State> { crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ - _weekHeaderBuilder(_currentStartDate, _currentEndDate), + _weekHeaderBuilder( + _currentStartDate, + _currentEndDate, + onHeaderTitleTap: widget.onHeaderTitleTap, + ), Expanded( child: DecoratedBox( decoration: BoxDecoration(color: widget.backgroundColor), @@ -710,23 +723,25 @@ class WeekViewState extends State> { /// Default view header builder. This builder will be used if /// [widget.dayTitleBuilder] is null. - Widget _defaultWeekPageHeaderBuilder(DateTime startDate, DateTime endDate) { + Widget _defaultWeekPageHeaderBuilder(DateTime startDate, DateTime endDate, + {HeaderTitleCallback? onHeaderTitleTap}) { return WeekPageHeader( startDate: _currentStartDate, endDate: _currentEndDate, onNextDay: nextPage, onPreviousDay: previousPage, - onTitleTapped: () async { - final selectedDate = await showDatePicker( - context: context, - initialDate: startDate, - firstDate: _minDate, - lastDate: _maxDate, - ); - - if (selectedDate == null) return; - jumpToWeek(selectedDate); - }, + onTitleTapped: onHeaderTitleTap ?? + () async { + final selectedDate = await showDatePicker( + context: context, + initialDate: startDate, + firstDate: _minDate, + lastDate: _maxDate, + ); + + if (selectedDate == null) return; + jumpToWeek(selectedDate); + }, headerStringBuilder: widget.headerStringBuilder, headerStyle: widget.headerStyle, );