From a5ad07a048857efca972cd30c3bc44ee39445980 Mon Sep 17 00:00:00 2001 From: gaoxin Date: Wed, 27 Jul 2022 19:51:02 +0800 Subject: [PATCH] Add pageSnapping option for customizing horizontal scroll --- lib/src/table_calendar.dart | 5 +++++ lib/src/table_calendar_base.dart | 3 +++ lib/src/widgets/calendar_core.dart | 3 +++ 3 files changed, 11 insertions(+) diff --git a/lib/src/table_calendar.dart b/lib/src/table_calendar.dart index 3f083166..19465164 100644 --- a/lib/src/table_calendar.dart +++ b/lib/src/table_calendar.dart @@ -96,6 +96,9 @@ class TableCalendar extends StatefulWidget { /// if the currently visible calendar page is changed. final bool pageAnimationEnabled; + /// Set to false to disable horizontal page snapping, useful for custom scroll behavior. + final bool pageSnapping; + /// When set to true, `CalendarFormat.month` will always display six weeks, /// even if the content would fit in less. final bool sixWeekMonthsEnforced; @@ -225,6 +228,7 @@ class TableCalendar extends StatefulWidget { this.daysOfWeekVisible = true, this.pageJumpingEnabled = false, this.pageAnimationEnabled = true, + this.pageSnapping = true, this.sixWeekMonthsEnforced = false, this.shouldFillViewport = false, this.weekNumbersVisible = false, @@ -502,6 +506,7 @@ class _TableCalendarState extends State> { pageAnimationCurve: widget.pageAnimationCurve, availableCalendarFormats: widget.availableCalendarFormats, simpleSwipeConfig: widget.simpleSwipeConfig, + pageSnapping: widget.pageSnapping, sixWeekMonthsEnforced: widget.sixWeekMonthsEnforced, onVerticalSwipe: _swipeCalendarFormat, onPageChanged: (focusedDay) { diff --git a/lib/src/table_calendar_base.dart b/lib/src/table_calendar_base.dart index 7fe150f1..6222880c 100644 --- a/lib/src/table_calendar_base.dart +++ b/lib/src/table_calendar_base.dart @@ -25,6 +25,7 @@ class TableCalendarBase extends StatefulWidget { final TableBorder? tableBorder; final Duration formatAnimationDuration; final Curve formatAnimationCurve; + final bool pageSnapping; final bool pageAnimationEnabled; final Duration pageAnimationDuration; final Curve pageAnimationCurve; @@ -55,6 +56,7 @@ class TableCalendarBase extends StatefulWidget { this.tableBorder, this.formatAnimationDuration = const Duration(milliseconds: 200), this.formatAnimationCurve = Curves.linear, + this.pageSnapping = true, this.pageAnimationEnabled = true, this.pageAnimationDuration = const Duration(milliseconds: 300), this.pageAnimationCurve = Curves.easeOut, @@ -209,6 +211,7 @@ class _TableCalendarBaseState extends State { scrollPhysics: _canScrollHorizontally ? PageScrollPhysics() : NeverScrollableScrollPhysics(), + pageSnapping: widget.pageSnapping, firstDay: widget.firstDay, lastDay: widget.lastDay, startingDayOfWeek: widget.startingDayOfWeek, diff --git a/lib/src/widgets/calendar_core.dart b/lib/src/widgets/calendar_core.dart index aa7ffd00..88c0db6a 100644 --- a/lib/src/widgets/calendar_core.dart +++ b/lib/src/widgets/calendar_core.dart @@ -31,6 +31,7 @@ class CalendarCore extends StatelessWidget { final PageController? pageController; final ScrollPhysics? scrollPhysics; final _OnCalendarPageChanged onPageChanged; + final bool pageSnapping; const CalendarCore({ Key? key, @@ -55,6 +56,7 @@ class CalendarCore extends StatelessWidget { this.rowDecoration, this.tableBorder, this.scrollPhysics, + this.pageSnapping = true, }) : assert(!dowVisible || (dowHeight != null && dowBuilder != null)), super(key: key); @@ -63,6 +65,7 @@ class CalendarCore extends StatelessWidget { return PageView.builder( controller: pageController, physics: scrollPhysics, + pageSnapping: pageSnapping, itemCount: _getPageCount(calendarFormat, firstDay, lastDay), itemBuilder: (context, index) { final baseDay = _getBaseDay(calendarFormat, index);