Skip to content

Commit

Permalink
:fix: Fixes issue #426: Fixed header style icons visibility on min & …
Browse files Browse the repository at this point in the history
…max dates.
  • Loading branch information
shubham-jitiya-simform committed Dec 11, 2024
1 parent b536018 commit 517d5da
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 32 deletions.
73 changes: 44 additions & 29 deletions lib/src/components/headers/calendar_page_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,31 @@ class CalendarPageHeader extends StatelessWidget {
mainAxisAlignment: headerStyle.mainAxisAlignment,
children: [
if (headerStyle.leftIconVisible && headerStyle.leftIconConfig != null)
headerStyle.leftIconConfig!.icon?.call(context) ??
IconButton(
onPressed: onPreviousDay,
splashColor: Colors.transparent,
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
padding: headerStyle.leftIconPadding ??
headerStyle.leftIconConfig!.padding,
icon: headerStyle.leftIcon ??
Icon(
Icons.chevron_left,
size: headerStyle.leftIconConfig!.size,
color: iconColor ?? headerStyle.leftIconConfig!.color,
),
),
Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: headerStyle.leftIconConfig!.icon?.call(context) ??
IconButton(
onPressed: onPreviousDay,
splashColor: Colors.transparent,
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
padding: headerStyle.leftIconPadding ??
headerStyle.leftIconConfig!.padding,
icon: headerStyle.leftIcon ??
Icon(
Icons.chevron_left,
size: headerStyle.leftIconConfig!.size,
color:
iconColor ?? headerStyle.leftIconConfig!.color,
),
),
),
),
if (headerStyle.leftIconConfig == null) Spacer(flex: 1),
Expanded(
flex: 3, // TODO(Shubham): Test overflow
child: titleBuilder != null
? DefaultTextStyle.merge(
style: headerStyle.headerTextStyle,
Expand All @@ -141,22 +149,29 @@ class CalendarPageHeader extends StatelessWidget {
),
),
),
// Spacer(flex: 1),
if (headerStyle.rightIconVisible &&
headerStyle.rightIconConfig != null)
IconButton(
onPressed: onNextDay,
splashColor: Colors.transparent,
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
padding: headerStyle.rightIconPadding,
icon: headerStyle.rightIcon ??
Icon(
Icons.chevron_right,
size: headerStyle.rightIconConfig?.size,
color: iconColor ?? headerStyle.rightIconConfig?.color,
),
Expanded(
child: Align(
alignment: Alignment.centerRight,
child: IconButton(
onPressed: onNextDay,
splashColor: Colors.transparent,
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
padding: headerStyle.rightIconPadding,
icon: headerStyle.rightIcon ??
Icon(
Icons.chevron_right,
size: headerStyle.rightIconConfig?.size,
color: iconColor ?? headerStyle.rightIconConfig?.color,
),
),
),
),
if (headerStyle.rightIconConfig == null) Spacer(flex: 1),
],
),
);
Expand Down
16 changes: 15 additions & 1 deletion lib/src/day_view/day_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
ScrollController get scrollController => _scrollController;

late VoidCallback _reloadCallback;
late HeaderStyle headerStyle;

final _scrollConfiguration = EventScrollConfiguration<T>();

Expand All @@ -368,6 +369,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
);
_pageController = PageController(initialPage: _currentIndex);
_eventArranger = widget.eventArranger ?? SideEventArranger<T>();
headerStyle = widget.headerStyle;
_assignBuilders();
}

Expand Down Expand Up @@ -701,7 +703,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
jumpToDate(selectedDate);
}
},
headerStyle: widget.headerStyle,
headerStyle: headerStyle,
);
}

Expand Down Expand Up @@ -756,6 +758,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
_currentDate.day + (index - _currentIndex),
);
_currentIndex = index;
_updateHeaderIcons();
});
}
if (!widget.keepScrollOffset) {
Expand All @@ -764,6 +767,17 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
widget.onPageChange?.call(_currentDate, _currentIndex);
}

// Hide header icons if end dates are reached
void _updateHeaderIcons() {
bool setLeftIconToNull = _currentDate == _minDate;
bool setRightIconToNull = _currentDate == _maxDate;

headerStyle = widget.headerStyle.copyWith(
setLeftIconConfigToNull: setLeftIconToNull,
setRightIconConfigToNull: setRightIconToNull,
);
}

/// Animate to next page
///
/// Arguments [duration] and [curve] will override default values provided
Expand Down
24 changes: 22 additions & 2 deletions lib/src/month_view/month_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {

late VoidCallback _reloadCallback;

late HeaderStyle headerStyle;

@override
void initState() {
super.initState();
Expand All @@ -262,7 +264,7 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {

// Initialize page controller to control page actions.
_pageController = PageController(initialPage: _currentIndex);

headerStyle = widget.headerStyle;
_assignBuilders();
}

Expand Down Expand Up @@ -508,18 +510,36 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
void _onPageChange(int value) {
if (mounted) {
setState(() {
// TODO(Shubham): Once call in init as when opening it can be edge dates
// debugPrint(
// 'CurrentDate: ${DateTime(_currentDate.year - 1, _currentDate.month - 1, _currentDate.day)}');
// widget.headerStyle.leftIconConfig = null;
_currentDate = DateTime(
_currentDate.year,
_currentDate.month + (value - _currentIndex),
);
_currentIndex = value;
_updateHeaderIcons();
});
}
widget.onPageChange?.call(_currentDate, _currentIndex);
}

// Hide header icons if end dates are reached
// TODO(Shubham): Check if custom header is provided
void _updateHeaderIcons() {
bool setLeftIconToNull = _currentDate == _minDate;
bool setRightIconToNull = _currentDate == _maxDate;

headerStyle = widget.headerStyle.copyWith(
setLeftIconConfigToNull: setLeftIconToNull,
setRightIconConfigToNull: setRightIconToNull,
);
}

/// Default month view header builder
Widget _defaultHeaderBuilder(DateTime date) {
debugPrint('Rebuild header => ${_maxDate}');
return MonthPageHeader(
onTitleTapped: () async {
if (widget.onHeaderTitleTap != null) {
Expand All @@ -540,7 +560,7 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
date: date,
dateStringBuilder: widget.headerStringBuilder,
onNextMonth: nextPage,
headerStyle: widget.headerStyle,
headerStyle: headerStyle,
);
}

Expand Down
30 changes: 30 additions & 0 deletions lib/src/style/header_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,36 @@ class HeaderStyle {
this.rightIconPadding,
});

HeaderStyle copyWith({
TextStyle? headerTextStyle,
EdgeInsets? headerMargin,
EdgeInsets? headerPadding,
TextAlign? titleAlign,
BoxDecoration? decoration,
MainAxisAlignment? mainAxisAlignment,
IconDataConfig? leftIconConfig,
bool setLeftIconConfigToNull = false,
IconDataConfig? rightIconConfig,
bool setRightIconConfigToNull = false,
MainAxisSize? mainAxisSize,
}) {
return HeaderStyle(
headerTextStyle: headerTextStyle ?? this.headerTextStyle,
headerMargin: headerMargin ?? this.headerMargin,
headerPadding: headerPadding ?? this.headerPadding,
titleAlign: titleAlign ?? this.titleAlign,
decoration: decoration ?? this.decoration,
mainAxisAlignment: mainAxisAlignment ?? this.mainAxisAlignment,
leftIconConfig: setLeftIconConfigToNull
? null
: (leftIconConfig ?? this.leftIconConfig),
rightIconConfig: setRightIconConfigToNull
? null
: (rightIconConfig ?? this.rightIconConfig),
mainAxisSize: mainAxisSize ?? this.mainAxisSize,
);
}

/// Create a `HeaderStyle` of calendar view
///
/// Used when you need to use same configs for left and right icons.
Expand Down

0 comments on commit 517d5da

Please sign in to comment.