Skip to content

Commit

Permalink
fix: 🐛Added a callback for the header title #241.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaiminrana05 committed Nov 1, 2023
1 parent cafbcb1 commit ced2737
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 51 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# [1.0.5] (UnReleased)
# [1.0.5] (UnReleased)(https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/tree/1.0.5)

- Fixed issue related to auto scroll to initial duration for day
view-[#269](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/issues/269)
- Fixed
issue added a callback for the header title
- [#241](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/issues/241)

# [1.0.4 - 9 Aug 2023](https://github.com/SimformSolutionsPvtLtd/flutter_calendar_view/tree/1.0.4)

Expand Down
46 changes: 30 additions & 16 deletions lib/src/day_view/day_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DayView<T extends Object?> extends StatefulWidget {
final DateWidgetBuilder? timeLineBuilder;

/// Builds day title bar.
final DateWidgetBuilder? dayTitleBuilder;
final HeaderBuilder? dayTitleBuilder;

/// Builds custom PressDetector widget
///
Expand Down Expand Up @@ -192,6 +192,12 @@ class DayView<T extends Object?> extends StatefulWidget {
/// By default it will be Duration(hours:0)
final Duration startDuration;

/// Callback for the Header title
final HeaderTitleCallback? onHeaderTitleTap;

/// Page controller to control page actions.
final PageController? pageController;

/// Main widget for day view.
const DayView({
Key? key,
Expand Down Expand Up @@ -232,6 +238,8 @@ class DayView<T extends Object?> extends StatefulWidget {
this.showHalfHours = false,
this.halfHourIndicatorSettings,
this.startDuration = const Duration(hours: 0),
this.onHeaderTitleTap,
this.pageController,
}) : assert(timeLineOffset >= 0,
"timeLineOffset must be greater than or equal to 0"),
assert(width == null || width > 0,
Expand Down Expand Up @@ -274,7 +282,7 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {

late EventTileBuilder<T> _eventTileBuilder;

late DateWidgetBuilder _dayTitleBuilder;
late HeaderBuilder _dayTitleBuilder;

late FullDayEventBuilder<T> _fullDayEventBuilder;

Expand Down Expand Up @@ -305,7 +313,8 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
_scrollController = ScrollController(
initialScrollOffset: widget.scrollOffset ??
widget.startDuration.inMinutes * widget.heightPerMinute);
_pageController = PageController(initialPage: _currentIndex);
_pageController =
widget.pageController ?? PageController(initialPage: _currentIndex);
_eventArranger = widget.eventArranger ?? SideEventArranger<T>();
_assignBuilders();
}
Expand Down Expand Up @@ -381,7 +390,10 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
_dayTitleBuilder(_currentDate),
_dayTitleBuilder(
_currentDate,
onHeaderTitleTap: widget.onHeaderTitleTap,
),
Expanded(
child: DecoratedBox(
decoration: BoxDecoration(color: widget.backgroundColor),
Expand Down Expand Up @@ -632,23 +644,25 @@ class DayViewState<T extends Object?> extends State<DayView<T>> {
/// Default view header builder. This builder will be used if
/// [widget.dayTitleBuilder] is null.
///
Widget _defaultDayBuilder(DateTime date) {
Widget _defaultDayBuilder(DateTime date,
{HeaderTitleCallback? onHeaderTitleTap}) {
return DayPageHeader(
date: _currentDate,
dateStringBuilder: widget.dateStringBuilder,
onNextDay: nextPage,
onPreviousDay: previousPage,
onTitleTapped: () async {
final selectedDate = await showDatePicker(
context: context,
initialDate: date,
firstDate: _minDate,
lastDate: _maxDate,
);

if (selectedDate == null) return;
jumpToDate(selectedDate);
},
onTitleTapped: onHeaderTitleTap ??
() async {
final selectedDate = await showDatePicker(
context: context,
initialDate: date,
firstDate: _minDate,
lastDate: _maxDate,
);

if (selectedDate == null) return;
jumpToDate(selectedDate);
},
headerStyle: widget.headerStyle,
);
}
Expand Down
46 changes: 30 additions & 16 deletions lib/src/month_view/month_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MonthView<T extends Object?> extends StatefulWidget {
/// Builds month page title.
///
/// Used default title builder if null.
final DateWidgetBuilder? headerBuilder;
final HeaderBuilder? headerBuilder;

/// This function will generate DateString in the calendar header.
/// Useful for I18n
Expand Down Expand Up @@ -138,6 +138,12 @@ class MonthView<T extends Object?> extends StatefulWidget {
/// Option for SafeArea.
final SafeAreaOption safeAreaOption;

/// Callback for the Header title
final HeaderTitleCallback? onHeaderTitleTap;

/// Page controller to control page actions.
final PageController? pageController;

/// Main [Widget] to display month view.
const MonthView({
Key? key,
Expand Down Expand Up @@ -166,6 +172,8 @@ class MonthView<T extends Object?> extends StatefulWidget {
this.weekDayStringBuilder,
this.headerStyle = const HeaderStyle(),
this.safeAreaOption = const SafeAreaOption(),
this.onHeaderTitleTap,
this.pageController,
}) : super(key: key);

@override
Expand Down Expand Up @@ -195,7 +203,7 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {

late WeekDayBuilder _weekBuilder;

late DateWidgetBuilder _headerBuilder;
late HeaderBuilder _headerBuilder;

EventController<T>? _controller;

Expand All @@ -215,7 +223,8 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
_regulateCurrentDate();

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

_assignBuilders();
}
Expand Down Expand Up @@ -288,7 +297,10 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
children: [
Container(
width: _width,
child: _headerBuilder(_currentDate),
child: _headerBuilder(
_currentDate,
onHeaderTitleTap: widget.onHeaderTitleTap,
),
),
Expanded(
child: PageView.builder(
Expand Down Expand Up @@ -459,19 +471,21 @@ class MonthViewState<T extends Object?> extends State<MonthView<T>> {
}

/// Default month view header builder
Widget _defaultHeaderBuilder(DateTime date) {
Widget _defaultHeaderBuilder(DateTime date,
{HeaderTitleCallback? onHeaderTitleTap}) {
return MonthPageHeader(
onTitleTapped: () async {
final selectedDate = await showDatePicker(
context: context,
initialDate: date,
firstDate: _minDate,
lastDate: _maxDate,
);

if (selectedDate == null) return;
jumpToMonth(selectedDate);
},
onTitleTapped: onHeaderTitleTap ??
() async {
final selectedDate = await showDatePicker(
context: context,
initialDate: date,
firstDate: _minDate,
lastDate: _maxDate,
);

if (selectedDate == null) return;
jumpToMonth(selectedDate);
},
onPreviousMonth: previousPage,
date: date,
dateStringBuilder: widget.headerStringBuilder,
Expand Down
12 changes: 8 additions & 4 deletions lib/src/typedefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ typedef WeekDayBuilder = Widget Function(
int day,
);

typedef DateWidgetBuilder = Widget Function(
DateTime date,
);
typedef DateWidgetBuilder = Widget Function(DateTime date);

typedef HeaderBuilder = Widget Function(DateTime date,
{HeaderTitleCallback? onHeaderTitleTap});

typedef HeaderTitleCallback = Future<void> Function();

typedef WeekNumberBuilder = Widget? Function(
DateTime firstDayOfWeek,
Expand All @@ -56,7 +59,8 @@ typedef StringProvider = String Function(DateTime date,
{DateTime? secondaryDate});

typedef WeekPageHeaderBuilder = Widget Function(
DateTime startDate, DateTime endDate);
DateTime startDate, DateTime endDate,
{HeaderTitleCallback? onHeaderTitleTap});

typedef TileTapCallback<T extends Object?> = void Function(
CalendarEventData<T> event, DateTime date);
Expand Down
43 changes: 29 additions & 14 deletions lib/src/week_view/week_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ class WeekView<T extends Object?> extends StatefulWidget {
/// Display full day event builder.
final FullDayEventBuilder<T>? fullDayEventBuilder;

/// Callback for the Header title
final HeaderTitleCallback? onHeaderTitleTap;

/// Page controller to control page actions.
final PageController? pageController;

/// Main widget for week view.
const WeekView({
Key? key,
Expand Down Expand Up @@ -224,6 +230,8 @@ class WeekView<T extends Object?> extends StatefulWidget {
this.headerStyle = const HeaderStyle(),
this.safeAreaOption = const SafeAreaOption(),
this.fullDayEventBuilder,
this.onHeaderTitleTap,
this.pageController,
}) : assert((timeLineOffset) >= 0,
"timeLineOffset must be greater than or equal to 0"),
assert(width == null || width > 0,
Expand Down Expand Up @@ -302,7 +310,8 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
_calculateHeights();
_scrollController =
ScrollController(initialScrollOffset: widget.scrollOffset);
_pageController = PageController(initialPage: _currentIndex);
_pageController =
widget.pageController ?? PageController(initialPage: _currentIndex);
_eventArranger = widget.eventArranger ?? SideEventArranger<T>();

_assignBuilders();
Expand Down Expand Up @@ -381,7 +390,11 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
_weekHeaderBuilder(_currentStartDate, _currentEndDate),
_weekHeaderBuilder(
_currentStartDate,
_currentEndDate,
onHeaderTitleTap: widget.onHeaderTitleTap,
),
Expanded(
child: DecoratedBox(
decoration: BoxDecoration(color: widget.backgroundColor),
Expand Down Expand Up @@ -710,23 +723,25 @@ class WeekViewState<T extends Object?> extends State<WeekView<T>> {

/// Default view header builder. This builder will be used if
/// [widget.dayTitleBuilder] is null.
Widget _defaultWeekPageHeaderBuilder(DateTime startDate, DateTime endDate) {
Widget _defaultWeekPageHeaderBuilder(DateTime startDate, DateTime endDate,
{HeaderTitleCallback? onHeaderTitleTap}) {
return WeekPageHeader(
startDate: _currentStartDate,
endDate: _currentEndDate,
onNextDay: nextPage,
onPreviousDay: previousPage,
onTitleTapped: () async {
final selectedDate = await showDatePicker(
context: context,
initialDate: startDate,
firstDate: _minDate,
lastDate: _maxDate,
);

if (selectedDate == null) return;
jumpToWeek(selectedDate);
},
onTitleTapped: onHeaderTitleTap ??
() async {
final selectedDate = await showDatePicker(
context: context,
initialDate: startDate,
firstDate: _minDate,
lastDate: _maxDate,
);

if (selectedDate == null) return;
jumpToWeek(selectedDate);
},
headerStringBuilder: widget.headerStringBuilder,
headerStyle: widget.headerStyle,
);
Expand Down

0 comments on commit ced2737

Please sign in to comment.