diff --git a/example/pubspec.lock b/example/pubspec.lock index 618525e4..22155737 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -75,38 +75,62 @@ packages: url: "https://pub.dev" source: hosted version: "0.19.0" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" + url: "https://pub.dev" + source: hosted + version: "10.0.4" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" + url: "https://pub.dev" + source: hosted + version: "3.0.3" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" + url: "https://pub.dev" + source: hosted + version: "3.0.1" matcher: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e + sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" url: "https://pub.dev" source: hosted - version: "1.10.0" + version: "1.12.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" simple_gesture_detector: dependency: transitive description: @@ -171,10 +195,10 @@ packages: dependency: transitive description: name: test_api - sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" + sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" url: "https://pub.dev" source: hosted - version: "0.6.1" + version: "0.7.0" vector_math: dependency: transitive description: @@ -183,14 +207,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" - web: + vm_service: dependency: transitive description: - name: web - sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152 + name: vm_service + sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" url: "https://pub.dev" source: hosted - version: "0.3.0" + version: "14.2.1" sdks: - dart: ">=3.2.0-194.0.dev <4.0.0" - flutter: ">=1.17.0" + dart: ">=3.3.0 <4.0.0" + flutter: ">=3.18.0-18.0.pre.54" diff --git a/lib/src/table_calendar.dart b/lib/src/table_calendar.dart index bcd52656..983729c9 100644 --- a/lib/src/table_calendar.dart +++ b/lib/src/table_calendar.dart @@ -30,6 +30,8 @@ enum RangeSelectionMode { disabled, toggledOff, toggledOn, enforced } /// Highly customizable, feature-packed Flutter calendar with gestures, animations and multiple formats. class TableCalendar extends StatefulWidget { + + final bool onlyWeekdays; /// Locale to format `TableCalendar` dates with, for example: `'en_US'`. /// /// If nothing is provided, a default locale will be used. @@ -259,7 +261,7 @@ class TableCalendar extends StatefulWidget { this.onHeaderLongPressed, this.onPageChanged, this.onFormatChanged, - this.onCalendarCreated, + this.onCalendarCreated, this.onlyWeekdays = false, }) : assert(availableCalendarFormats.keys.contains(calendarFormat)), assert(availableCalendarFormats.length <= CalendarFormat.values.length), assert(weekendDays.isNotEmpty @@ -556,13 +558,16 @@ class _TableCalendarState extends State> { return dowCell; }, dayBuilder: (context, day, focusedMonth) { - return GestureDetector( + return day.weekday == 7 ? Container(width: 0,) : GestureDetector( behavior: widget.dayHitTestBehavior, onTap: () => _onDayTapped(day), onLongPress: () => _onDayLongPressed(day), child: _buildCell(day, focusedMonth), ); }, + + onlyWeekDays: widget.onlyWeekdays, + weekendDays: widget.weekendDays, ), ), ], diff --git a/lib/src/table_calendar_base.dart b/lib/src/table_calendar_base.dart index 659a43a7..f4944a09 100644 --- a/lib/src/table_calendar_base.dart +++ b/lib/src/table_calendar_base.dart @@ -36,6 +36,8 @@ class TableCalendarBase extends StatefulWidget { final SwipeCallback? onVerticalSwipe; final void Function(DateTime focusedDay)? onPageChanged; final void Function(PageController pageController)? onCalendarCreated; + final List weekendDays; + final bool onlyWeekDays; TableCalendarBase({ Key? key, @@ -73,7 +75,7 @@ class TableCalendarBase extends StatefulWidget { }, this.onVerticalSwipe, this.onPageChanged, - this.onCalendarCreated, + this.onCalendarCreated, required this.weekendDays, required this.onlyWeekDays, }) : assert(!dowVisible || (dowHeight != null && dowBuilder != null)), assert(isSameDay(focusedDay, firstDay) || focusedDay.isAfter(firstDay)), assert(isSameDay(focusedDay, lastDay) || focusedDay.isBefore(lastDay)), @@ -251,6 +253,8 @@ class _TableCalendarBaseState extends State { }, dowBuilder: widget.dowBuilder, dayBuilder: widget.dayBuilder, + weekendDays: widget.weekendDays, + onlyWeekDays: widget.onlyWeekDays, ), ), ); diff --git a/lib/src/widgets/calendar_core.dart b/lib/src/widgets/calendar_core.dart index d196296f..2e6a4dea 100644 --- a/lib/src/widgets/calendar_core.dart +++ b/lib/src/widgets/calendar_core.dart @@ -32,6 +32,8 @@ class CalendarCore extends StatelessWidget { final PageController? pageController; final ScrollPhysics? scrollPhysics; final _OnCalendarPageChanged onPageChanged; + final bool onlyWeekDays; + final List weekendDays; const CalendarCore({ Key? key, @@ -56,7 +58,7 @@ class CalendarCore extends StatelessWidget { this.rowDecoration, this.tableBorder, this.tablePadding, - this.scrollPhysics, + this.scrollPhysics, this.onlyWeekDays = false, required this.weekendDays, }) : assert(!dowVisible || (dowHeight != null && dowBuilder != null)), super(key: key); @@ -112,7 +114,8 @@ class CalendarCore extends StatelessWidget { height: constrainedRowHeight ?? rowHeight, child: weekNumberBuilder?.call(context, day), ); - }, + }, + daysInWeek: onlyWeekDays == false ? 7 : 5, ); }, onPageChanged: (index) { @@ -261,10 +264,16 @@ class CalendarCore extends StatelessWidget { List _daysInRange(DateTime first, DateTime last) { final dayCount = last.difference(first).inDays + 1; - return List.generate( + List weekdays = List.generate( dayCount, (index) => DateTime.utc(first.year, first.month, first.day + index), ); + + if (onlyWeekDays) { + weekdays.removeWhere((element) => weekendDays.contains(element.weekday)); + } + + return weekdays; } DateTime _firstDayOfWeek(DateTime week) { diff --git a/lib/src/widgets/calendar_page.dart b/lib/src/widgets/calendar_page.dart index a13cb62b..172dd60d 100644 --- a/lib/src/widgets/calendar_page.dart +++ b/lib/src/widgets/calendar_page.dart @@ -15,6 +15,7 @@ class CalendarPage extends StatelessWidget { final bool dowVisible; final bool weekNumberVisible; final double? dowHeight; + final int daysInWeek; const CalendarPage({ Key? key, @@ -28,7 +29,7 @@ class CalendarPage extends StatelessWidget { this.tablePadding, this.dowVisible = true, this.weekNumberVisible = false, - this.dowHeight, + this.dowHeight, required this.daysInWeek, }) : assert(!dowVisible || (dowHeight != null && dowBuilder != null)), assert(!weekNumberVisible || weekNumberBuilder != null), super(key: key); @@ -56,12 +57,12 @@ class CalendarPage extends StatelessWidget { } Widget _buildWeekNumbers(BuildContext context) { - final rowAmount = visibleDays.length ~/ 7; + final rowAmount = visibleDays.length ~/ daysInWeek; return Column( children: [ if (dowVisible) SizedBox(height: dowHeight ?? 0), - ...List.generate(rowAmount, (index) => index * 7) + ...List.generate(rowAmount, (index) => index * daysInWeek) .map((index) => Expanded( child: weekNumberBuilder!(context, visibleDays[index]), )) @@ -74,20 +75,20 @@ class CalendarPage extends StatelessWidget { return TableRow( decoration: dowDecoration, children: List.generate( - 7, + daysInWeek, (index) => dowBuilder!(context, visibleDays[index]), ).toList(), ); } List _buildCalendarDays(BuildContext context) { - final rowAmount = visibleDays.length ~/ 7; + final rowAmount = visibleDays.length ~/ daysInWeek; - return List.generate(rowAmount, (index) => index * 7) + return List.generate(rowAmount, (index) => index * daysInWeek) .map((index) => TableRow( decoration: rowDecoration, children: List.generate( - 7, + daysInWeek, (id) => dayBuilder(context, visibleDays[index + id]), ), )) diff --git a/pubspec.lock b/pubspec.lock index 115e68f9..19725456 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -67,38 +67,62 @@ packages: url: "https://pub.dev" source: hosted version: "0.19.0" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" + url: "https://pub.dev" + source: hosted + version: "10.0.4" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" + url: "https://pub.dev" + source: hosted + version: "3.0.3" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" + url: "https://pub.dev" + source: hosted + version: "3.0.1" matcher: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e + sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" url: "https://pub.dev" source: hosted - version: "1.10.0" + version: "1.12.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" simple_gesture_detector: dependency: "direct main" description: @@ -156,10 +180,10 @@ packages: dependency: transitive description: name: test_api - sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" + sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" url: "https://pub.dev" source: hosted - version: "0.6.1" + version: "0.7.0" vector_math: dependency: transitive description: @@ -168,14 +192,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" - web: + vm_service: dependency: transitive description: - name: web - sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152 + name: vm_service + sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" url: "https://pub.dev" source: hosted - version: "0.3.0" + version: "14.2.1" sdks: - dart: ">=3.2.0-194.0.dev <4.0.0" - flutter: ">=1.17.0" + dart: ">=3.3.0 <4.0.0" + flutter: ">=3.18.0-18.0.pre.54" diff --git a/test/calendar_page_test.dart b/test/calendar_page_test.dart index f9d15020..c2da2148 100644 --- a/test/calendar_page_test.dart +++ b/test/calendar_page_test.dart @@ -38,6 +38,7 @@ void main() { return Text('${day.day}'); }, dowVisible: false, + daysInWeek: 7, ), ), ); @@ -62,6 +63,7 @@ void main() { return Text('${day.weekday}'); }, dowHeight: 5, + daysInWeek: 7, ), ), ); @@ -88,6 +90,7 @@ void main() { return Text('${day.day}'); }, dowVisible: true, + daysInWeek: 7, ), ), ); @@ -110,6 +113,7 @@ void main() { return Text('${day.weekday}'); }, dowHeight: 5, + daysInWeek: 7, ), ), ); @@ -139,6 +143,7 @@ void main() { weekNumberBuilder: (BuildContext context, DateTime day) { return Text(day.weekday.toString()); }, + daysInWeek: 7, ), )); diff --git a/test/table_calendar_base_test.dart b/test/table_calendar_base_test.dart index 4076a540..586a94a2 100644 --- a/test/table_calendar_base_test.dart +++ b/test/table_calendar_base_test.dart @@ -39,6 +39,8 @@ void main() { dowVisible: false, calendarFormat: CalendarFormat.month, startingDayOfWeek: StartingDayOfWeek.sunday, + weekendDays: [DateTime.saturday, DateTime.sunday], + onlyWeekDays: false, ), ), ); @@ -85,6 +87,8 @@ void main() { dowVisible: false, calendarFormat: CalendarFormat.twoWeeks, startingDayOfWeek: StartingDayOfWeek.sunday, + weekendDays: [DateTime.saturday, DateTime.sunday], + onlyWeekDays: false, ), ), ); @@ -131,6 +135,8 @@ void main() { dowVisible: false, calendarFormat: CalendarFormat.week, startingDayOfWeek: StartingDayOfWeek.sunday, + weekendDays: [DateTime.saturday, DateTime.sunday], + onlyWeekDays: false, ), ), ); @@ -177,6 +183,8 @@ void main() { dowVisible: false, calendarFormat: CalendarFormat.month, startingDayOfWeek: StartingDayOfWeek.monday, + weekendDays: [DateTime.saturday, DateTime.sunday], + onlyWeekDays: false, ), ), ); @@ -223,6 +231,8 @@ void main() { dowVisible: false, calendarFormat: CalendarFormat.twoWeeks, startingDayOfWeek: StartingDayOfWeek.monday, + weekendDays: [DateTime.saturday, DateTime.sunday], + onlyWeekDays: false, ), ), ); @@ -269,6 +279,8 @@ void main() { dowVisible: false, calendarFormat: CalendarFormat.week, startingDayOfWeek: StartingDayOfWeek.monday, + weekendDays: [DateTime.saturday, DateTime.sunday], + onlyWeekDays: false, ), ), ); @@ -327,6 +339,8 @@ void main() { }, rowHeight: 52, dowVisible: false, + weekendDays: [DateTime.saturday, DateTime.sunday], + onlyWeekDays: false, ), ), ); @@ -372,6 +386,8 @@ void main() { dowBuilder: (context, day) { return Text('${day.weekday}'); }, + weekendDays: [DateTime.saturday, DateTime.sunday], + onlyWeekDays: false, ), ), );