Skip to content

Commit

Permalink
feat: Fixes issue #385: Add showWeekends flag in month view
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham-jitiya-simform committed Nov 29, 2024
1 parent cc99a0e commit 036b8ab
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
1 change: 1 addition & 0 deletions example/lib/widgets/month_view_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MonthViewWidget extends StatelessWidget {
return MonthView(
key: state,
width: width,
showWeekends: false,
hideDaysNotInMonth: false,
onEventTap: (event, date) {
Navigator.of(context).push(
Expand Down
67 changes: 50 additions & 17 deletions lib/src/month_view/month_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class MonthView<T extends Object?> extends StatefulWidget {
/// This method will be called when user double taps on event tile.
final TileTapCallback<T>? onEventDoubleTap;

/// Show weekends or not
///
/// Default value is true.
final bool showWeekends;

/// Builds the name of the weeks.
///
/// Used default week builder if null.
Expand Down Expand Up @@ -184,6 +189,7 @@ class MonthView<T extends Object?> extends StatefulWidget {
this.maxMonth,
this.controller,
this.initialMonth,
this.showWeekends = true,
this.borderSize = 1,
this.useAvailableVerticalSpace = false,
this.cellAspectRatio = 0.55,
Expand Down Expand Up @@ -352,7 +358,7 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
width: _width,
child: Row(
children: List.generate(
7,
widget.showWeekends ? 7 : 5,
(index) => Expanded(
child: SizedBox(
width: _cellWidth,
Expand Down Expand Up @@ -391,6 +397,7 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
startDay: widget.startDay,
physics: widget.pagePhysics,
hideDaysNotInMonth: widget.hideDaysNotInMonth,
dayCount: widget.showWeekends ? 7 : 5,
),
);
}),
Expand Down Expand Up @@ -663,6 +670,7 @@ class _MonthPageBuilder<T> extends StatelessWidget {
final WeekDays startDay;
final ScrollPhysics physics;
final bool hideDaysNotInMonth;
final int dayCount;

const _MonthPageBuilder({
Key? key,
Expand All @@ -680,30 +688,26 @@ class _MonthPageBuilder<T> extends StatelessWidget {
required this.startDay,
required this.physics,
required this.hideDaysNotInMonth,
required this.dayCount,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final cellsNumber = dayCount == 5 ? 30 : 42;
final monthDays = date.datesOfMonths(startDay: startDay);
return Container(
width: width,
height: height,
child: GridView.builder(
padding: EdgeInsets.zero,
physics: physics,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 7,
childAspectRatio: cellRatio,
),
itemCount: 42,
shrinkWrap: true,
itemBuilder: (context, index) {
final events = controller.getEventsOnDay(monthDays[index]);
return GestureDetector(
final gridViewItems = <Widget>[];
for (var index = 0; index < cellsNumber; index++) {
final events = controller.getEventsOnDay(monthDays[index]);
bool isWeekend =
monthDays[index].weekday == 6 || monthDays[index].weekday == 7;
if (!isWeekend || isWeekend && dayCount == 7) {
gridViewItems.add(
GestureDetector(
onTap: () => onCellTap?.call(events, monthDays[index]),
onLongPress: () => onDateLongPress?.call(monthDays[index]),
child: Container(
decoration: BoxDecoration(
color: Colors.transparent,
border: showBorder
? Border.all(
color: borderColor,
Expand All @@ -719,7 +723,36 @@ class _MonthPageBuilder<T> extends StatelessWidget {
hideDaysNotInMonth,
),
),
);
),
);
}
}
return Container(
width: width,
height: height,
child: GridView.builder(
padding: EdgeInsets.zero,
physics: physics,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: dayCount,
childAspectRatio: cellRatio,
),
itemCount: cellsNumber,
shrinkWrap: true,
itemBuilder: (context, index) {
if (index > gridViewItems.length - 1) {
return Container(
decoration: BoxDecoration(
color: Colors.grey[200],
border: showBorder
? Border.all(
color: borderColor,
width: borderSize,
)
: null,
));
}
return gridViewItems[index];
},
),
);
Expand Down

0 comments on commit 036b8ab

Please sign in to comment.