44
55import 'package:flutter/material.dart' ;
66
7- import '../calendar_constants.dart' ;
8- import '../calendar_controller_provider.dart' ;
9- import '../calendar_event_data.dart' ;
10- import '../components/components.dart' ;
7+ import '../../calendar_view.dart' ;
118import '../constants.dart' ;
12- import '../enumerations.dart' ;
13- import '../event_controller.dart' ;
14- import '../extensions.dart' ;
15- import '../style/header_style.dart' ;
16- import '../typedefs.dart' ;
179
1810class MonthView <T extends Object ?> extends StatefulWidget {
1911 /// A function that returns a [Widget] that determines appearance of
@@ -64,6 +56,10 @@ class MonthView<T extends Object?> extends StatefulWidget {
6456 /// This method will be called when user double taps on event tile.
6557 final TileTapCallback <T >? onEventDoubleTap;
6658
59+ /// Show weekends or not.
60+ /// Default value is true.
61+ final bool showWeekends;
62+
6763 /// Builds the name of the weeks.
6864 ///
6965 /// Used default week builder if null.
@@ -184,6 +180,7 @@ class MonthView<T extends Object?> extends StatefulWidget {
184180 this .maxMonth,
185181 this .controller,
186182 this .initialMonth,
183+ this .showWeekends = true ,
187184 this .borderSize = 1 ,
188185 this .useAvailableVerticalSpace = false ,
189186 this .cellAspectRatio = 0.55 ,
@@ -342,7 +339,10 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
342339 onPageChanged: _onPageChange,
343340 itemBuilder: (_, index) {
344341 final date = DateTime (_minDate.year, _minDate.month + index);
345- final weekDays = date.datesOfWeek (start: widget.startDay);
342+ final weekDays = date.datesOfWeek (
343+ start: widget.startDay,
344+ showWeekEnds: widget.showWeekends,
345+ );
346346
347347 return Column (
348348 mainAxisSize: MainAxisSize .min,
@@ -352,7 +352,7 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
352352 width: _width,
353353 child: Row (
354354 children: List .generate (
355- 7 ,
355+ widget.showWeekends ? 7 : 5 ,
356356 (index) => Expanded (
357357 child: SizedBox (
358358 width: _cellWidth,
@@ -364,36 +364,45 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
364364 ),
365365 ),
366366 Expanded (
367- child: LayoutBuilder (builder: (context, constraints) {
368- final _cellAspectRatio =
369- widget.useAvailableVerticalSpace
370- ? calculateCellAspectRatio (
371- constraints.maxHeight,
372- )
373- : widget.cellAspectRatio;
374-
375- return SizedBox (
376- height: _height,
377- width: _width,
378- child: _MonthPageBuilder <T >(
379- key: ValueKey (date.toIso8601String ()),
380- onCellTap: widget.onCellTap,
381- onDateLongPress: widget.onDateLongPress,
382- width: _width,
383- height: _height,
384- controller: controller,
385- borderColor: widget.borderColor,
386- borderSize: widget.borderSize,
387- cellBuilder: _cellBuilder,
388- cellRatio: _cellAspectRatio,
389- date: date,
390- showBorder: widget.showBorder,
367+ child: LayoutBuilder (
368+ builder: (context, constraints) {
369+ final dates = date.datesOfMonths (
391370 startDay: widget.startDay,
392- physics: widget.pagePhysics,
393371 hideDaysNotInMonth: widget.hideDaysNotInMonth,
394- ),
395- );
396- }),
372+ showWeekends: widget.showWeekends,
373+ );
374+ final _cellAspectRatio =
375+ widget.useAvailableVerticalSpace
376+ ? calculateCellAspectRatio (
377+ height: constraints.maxHeight,
378+ daysInMonth: dates.length,
379+ )
380+ : widget.cellAspectRatio;
381+
382+ return SizedBox (
383+ height: _height,
384+ width: _width,
385+ child: _MonthPageBuilder <T >(
386+ key: ValueKey (date.toIso8601String ()),
387+ onCellTap: widget.onCellTap,
388+ onDateLongPress: widget.onDateLongPress,
389+ width: _width,
390+ height: _height,
391+ controller: controller,
392+ borderColor: widget.borderColor,
393+ borderSize: widget.borderSize,
394+ cellBuilder: _cellBuilder,
395+ cellRatio: _cellAspectRatio,
396+ date: date,
397+ showBorder: widget.showBorder,
398+ startDay: widget.startDay,
399+ physics: widget.pagePhysics,
400+ hideDaysNotInMonth: widget.hideDaysNotInMonth,
401+ weekDays: widget.showWeekends ? 7 : 5 ,
402+ ),
403+ );
404+ },
405+ ),
397406 ),
398407 ],
399408 );
@@ -432,8 +441,12 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
432441 _height = _cellHeight * 6 ;
433442 }
434443
435- double calculateCellAspectRatio (double height) {
436- final _cellHeight = height / 6 ;
444+ double calculateCellAspectRatio ({
445+ required double height,
446+ required int daysInMonth,
447+ }) {
448+ final rows = daysInMonth / 7 ;
449+ final _cellHeight = height / rows;
437450 return _cellWidth / _cellHeight;
438451 }
439452
@@ -663,6 +676,7 @@ class _MonthPageBuilder<T> extends StatelessWidget {
663676 final WeekDays startDay;
664677 final ScrollPhysics physics;
665678 final bool hideDaysNotInMonth;
679+ final int weekDays;
666680
667681 const _MonthPageBuilder ({
668682 Key ? key,
@@ -680,25 +694,36 @@ class _MonthPageBuilder<T> extends StatelessWidget {
680694 required this .startDay,
681695 required this .physics,
682696 required this .hideDaysNotInMonth,
697+ required this .weekDays,
683698 }) : super (key: key);
684699
685700 @override
686701 Widget build (BuildContext context) {
687- final monthDays = date.datesOfMonths (startDay: startDay);
688- return Container (
702+ final monthDays = date.datesOfMonths (
703+ startDay: startDay,
704+ hideDaysNotInMonth: hideDaysNotInMonth,
705+ showWeekends: weekDays == 7 ,
706+ );
707+
708+ // Highlight tiles which is not in current month
709+ return SizedBox (
689710 width: width,
690711 height: height,
691712 child: GridView .builder (
692713 padding: EdgeInsets .zero,
693714 physics: physics,
694715 gridDelegate: SliverGridDelegateWithFixedCrossAxisCount (
695- crossAxisCount: 7 ,
716+ crossAxisCount: weekDays ,
696717 childAspectRatio: cellRatio,
697718 ),
698- itemCount: 42 ,
719+ itemCount: monthDays.length ,
699720 shrinkWrap: true ,
700721 itemBuilder: (context, index) {
701- final events = controller.getEventsOnDay (monthDays[index]);
722+ // Hide events if `hideDaysNotInMonth` true
723+ final events =
724+ hideDaysNotInMonth & (monthDays[index].month != date.month)
725+ ? < CalendarEventData <T >> []
726+ : controller.getEventsOnDay (monthDays[index]);
702727 return GestureDetector (
703728 onTap: () => onCellTap? .call (events, monthDays[index]),
704729 onLongPress: () => onDateLongPress? .call (monthDays[index]),
0 commit comments