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 Jan 6, 2025
1 parent 9da90f7 commit 5c631f8
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 124 deletions.
95 changes: 48 additions & 47 deletions lib/src/components/headers/calendar_page_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ class CalendarPageHeader extends StatelessWidget {
/// This will be ignored if right icon is provided in [headerStyle].
final VoidCallback? onNextDay;

// TODO(Shubham): Add comment
final bool showNextIcon;

/// When user taps on left arrow.
///
/// This will be ignored if left icon is provided in [headerStyle].
final VoidCallback? onPreviousDay;

// TODO(Shubham): Add comment
final bool showPreviousIcon;

/// When user taps on title.
///
/// This will be ignored if [titleBuilder] is provided.
Expand Down Expand Up @@ -78,8 +84,10 @@ class CalendarPageHeader extends StatelessWidget {
this.dateStringBuilder,
this.titleBuilder,
this.onNextDay,
this.showNextIcon = true,
this.onTitleTapped,
this.onPreviousDay,
this.showPreviousIcon = true,
this.secondaryDate,
@Deprecated("Use HeaderStyle.decoration to provide background")
this.backgroundColor = Constants.headerBackground,
Expand All @@ -103,32 +111,28 @@ class CalendarPageHeader extends StatelessWidget {
mainAxisSize: headerStyle.mainAxisSize,
mainAxisAlignment: headerStyle.mainAxisAlignment,
children: [
if (headerStyle.leftIconVisible && headerStyle.leftIconConfig != null)
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.leftIconVisible) &&
headerStyle.leftIconConfig != null)
Opacity(
opacity: showPreviousIcon ? 1 : 0,
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) const Spacer(),
Expanded(
flex: 4,
child: titleBuilder != null
? DefaultTextStyle.merge(
style: headerStyle.headerTextStyle,
Expand All @@ -148,30 +152,27 @@ class CalendarPageHeader extends StatelessWidget {
),
),
),
if (headerStyle.rightIconConfig == null) const Spacer(),
if (headerStyle.rightIconVisible &&
if ((headerStyle.rightIconVisible) &&
headerStyle.rightIconConfig != null)
Expanded(
child: Align(
alignment: Alignment.centerRight,
child: headerStyle.rightIconConfig!.icon?.call(context) ??
IconButton(
onPressed: onNextDay,
splashColor: Colors.transparent,
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
padding: headerStyle.rightIconPadding ??
headerStyle.rightIconConfig!.padding,
icon: headerStyle.rightIcon ??
Icon(
Icons.chevron_right,
size: headerStyle.rightIconConfig?.size,
color:
iconColor ?? headerStyle.rightIconConfig?.color,
),
),
),
Opacity(
opacity: showNextIcon ? 1 : 0,
child: headerStyle.rightIconConfig!.icon?.call(context) ??
IconButton(
onPressed: onNextDay,
splashColor: Colors.transparent,
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
padding: headerStyle.rightIconPadding ??
headerStyle.rightIconConfig!.padding,
icon: headerStyle.rightIcon ??
Icon(
Icons.chevron_right,
size: headerStyle.rightIconConfig?.size,
color:
iconColor ?? headerStyle.rightIconConfig?.color,
),
),
),
],
),
Expand Down
5 changes: 5 additions & 0 deletions lib/src/components/headers/day_page_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ class DayPageHeader extends CalendarPageHeader {
const DayPageHeader({
Key? key,
VoidCallback? onNextDay,
bool showNextIcon = true,
AsyncCallback? onTitleTapped,
VoidCallback? onPreviousDay,
bool showPreviousIcon = true,
StringProvider? dateStringBuilder,
required DateTime date,
@Deprecated("Use HeaderStyle to provide icon color") Color? iconColor,
Expand All @@ -31,7 +33,10 @@ class DayPageHeader extends CalendarPageHeader {
backgroundColor: backgroundColor,
iconColor: iconColor,
onNextDay: onNextDay,

showNextIcon: showNextIcon,
onPreviousDay: onPreviousDay,
showPreviousIcon: showPreviousIcon,
onTitleTapped: onTitleTapped,
dateStringBuilder:
dateStringBuilder ?? DayPageHeader._dayStringBuilder,
Expand Down
4 changes: 4 additions & 0 deletions lib/src/components/headers/month_page_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class MonthPageHeader extends CalendarPageHeader {
const MonthPageHeader({
Key? key,
VoidCallback? onNextMonth,
bool showNextIcon = true,
AsyncCallback? onTitleTapped,
VoidCallback? onPreviousMonth,
bool showPreviousIcon = true,
@Deprecated("Use HeaderStyle to provide icon color") Color? iconColor,
@Deprecated("Use HeaderStyle to provide background color")
Color backgroundColor = Constants.headerBackground,
Expand All @@ -27,7 +29,9 @@ class MonthPageHeader extends CalendarPageHeader {
key: key,
date: date,
onNextDay: onNextMonth,
showNextIcon: showNextIcon,
onPreviousDay: onPreviousMonth,
showPreviousIcon: showPreviousIcon,
onTitleTapped: onTitleTapped,
// ignore_for_file: deprecated_member_use_from_same_package
backgroundColor: backgroundColor,
Expand Down
4 changes: 4 additions & 0 deletions lib/src/components/headers/week_page_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class WeekPageHeader extends CalendarPageHeader {
const WeekPageHeader({
Key? key,
VoidCallback? onNextDay,
bool showNextIcon = true,
AsyncCallback? onTitleTapped,
VoidCallback? onPreviousDay,
bool showPreviousIcon = true,
required DateTime startDate,
required DateTime endDate,
@Deprecated("Use HeaderStyle to provide icon color") Color? iconColor,
Expand All @@ -29,7 +31,9 @@ class WeekPageHeader extends CalendarPageHeader {
date: startDate,
secondaryDate: endDate,
onNextDay: onNextDay,
showNextIcon: showNextIcon,
onPreviousDay: onPreviousDay,
showPreviousIcon: showPreviousIcon,
onTitleTapped: onTitleTapped,
// ignore_for_file: deprecated_member_use_from_same_package
iconColor: iconColor,
Expand Down
29 changes: 16 additions & 13 deletions lib/src/day_view/day_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ 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 @@ -369,7 +368,6 @@ 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 @@ -683,10 +681,15 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
/// [widget.dayTitleBuilder] is null.
///
Widget _defaultDayBuilder(DateTime date) {
final showLeftIcon = date != CalendarConstants.epochDate;
final showRightIcon = date != CalendarConstants.maxDate;

return DayPageHeader(
date: _currentDate,
dateStringBuilder: widget.dateStringBuilder,
onNextDay: nextPage,
showPreviousIcon: showLeftIcon,
showNextIcon: showRightIcon,
onPreviousDay: previousPage,
onTitleTapped: () async {
if (widget.onHeaderTitleTap != null) {
Expand All @@ -703,7 +706,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
jumpToDate(selectedDate);
}
},
headerStyle: headerStyle,
headerStyle: widget.headerStyle,
);
}

Expand Down Expand Up @@ -758,7 +761,6 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
_currentDate.day + (index - _currentIndex),
);
_currentIndex = index;
_updateHeaderIcons();
});
}
if (!widget.keepScrollOffset) {
Expand All @@ -768,15 +770,16 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
}

// 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,
);
}
// TODO(Shubham): Not required remove this
// void _updateHeaderIcons() {
// bool setLeftIconToNull = _currentDate == _minDate;
// bool setRightIconToNull = _currentDate == _maxDate;
//
// headerStyle = widget.headerStyle.copyWith(
// setLeftIconConfigToNull: setLeftIconToNull,
// setRightIconConfigToNull: setRightIconToNull,
// );
// }

/// Animate to next page
///
Expand Down
22 changes: 6 additions & 16 deletions lib/src/month_view/month_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {

late VoidCallback _reloadCallback;

late HeaderStyle headerStyle;

@override
void initState() {
super.initState();
Expand All @@ -260,7 +258,6 @@ 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 @@ -516,26 +513,19 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
_currentDate.month + (value - _currentIndex),
);
_currentIndex = value;
_updateHeaderIcons();
});
}
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,
);
}

/// Default month view header builder
Widget _defaultHeaderBuilder(DateTime date) {
final showLeftIcon = date != CalendarConstants.epochDate;
final showRightIcon = date != CalendarConstants.maxDate;

return MonthPageHeader(
showPreviousIcon: showLeftIcon,
showNextIcon: showRightIcon,
onTitleTapped: () async {
if (widget.onHeaderTitleTap != null) {
widget.onHeaderTitleTap!(date);
Expand All @@ -555,7 +545,7 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
date: date,
dateStringBuilder: widget.headerStringBuilder,
onNextMonth: nextPage,
headerStyle: headerStyle,
headerStyle: widget.headerStyle,
);
}

Expand Down
30 changes: 0 additions & 30 deletions lib/src/style/header_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,36 +101,6 @@ 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
Loading

0 comments on commit 5c631f8

Please sign in to comment.