Skip to content

Commit

Permalink
feat(calendar): New callback onPrintLog
Browse files Browse the repository at this point in the history
* can be used to pass the log message of the calendar widget to the parent widget so its logger calls can log it
  • Loading branch information
agfeo committed Aug 12, 2024
1 parent 1cce02c commit ecd106d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [0.4.6] - 2024-08-12
* New callback for logging

## [0.4.5] - 2024-08-11
* Two new callback functions
* onListViewStateChanged
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ final ValueChanged<Map<DateTime, List<NeatCleanCalendarEvent>>>?

This callback function gets executed, when the `eventsMap` gets updated. This happens in the `initState` method. This callback function can be used to pass the content of the `eventsMap` to the parent widget. There are some cases, where this is useful.

#### `onPrintLog`

```dart
final ValueChanged<String>? onPrintLog;
```

This callback can be used to pass Strings with logging messages from the widget to its parent widget. By doing so can use your apps logging class to output the messages of the calendar. The only thing you need to do is to implement your allback message and call your logger with the passed String object.

### Options

### `isExpandable`
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MyApp extends StatelessWidget {
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.system,
home: CalendarScreen(),
localizationsDelegates: [GlobalMaterialLocalizations.delegate],
localizationsDelegates: GlobalMaterialLocalizations.delegates,
supportedLocales: [
const Locale('en'),
const Locale('fr'),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.4.3"
version: "0.4.6"
flutter_platform_widgets:
dependency: transitive
description:
Expand Down
39 changes: 30 additions & 9 deletions lib/flutter_neat_and_clean_calendar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Calendar extends StatefulWidget {
final ValueChanged? onListViewStateChanged;
final ValueChanged<Map<DateTime, List<NeatCleanCalendarEvent>>>?
onEventsUpdated;
final ValueChanged<String>? onPrintLog;
final bool isExpandable;
final DayBuilder? dayBuilder;
final EventListBuilder? eventListBuilder;
Expand Down Expand Up @@ -158,6 +159,7 @@ class Calendar extends StatefulWidget {
this.onEventLongPressed,
this.onListViewStateChanged,
this.onEventsUpdated,
this.onPrintLog,
this.isExpandable = false,
this.eventsList,
this.dayBuilder,
Expand Down Expand Up @@ -328,7 +330,9 @@ class _CalendarState extends State<Calendar> {
_selectedDate.year, _selectedDate.month, _selectedDate.day)] ??
[];

print('eventsMap has ${eventsMap?.length} entries');
widget.onPrintLog != null
? widget.onPrintLog!('eventsMap has ${eventsMap?.length} entries')
: print('eventsMap has ${eventsMap?.length} entries');

// If the eventsMap is updated, the eventsUpdated callback is invoked. In some cases it is useful
// to have a copy of the eventsMap in the parent widget. This can be done by providing a callback
Expand Down Expand Up @@ -438,7 +442,10 @@ class _CalendarState extends State<Calendar> {
// _selectedDate is updated. This must be done after the callback methods are invoked,
// otherwise the callback methods will not trigger, if the current date is equal to the
// selected date.
print('Date chosen: ${_selectedDate.toIso8601String()}');
widget.onPrintLog != null
? widget.onPrintLog!(
'Date chosen: ${_selectedDate.toIso8601String()}')
: print('Date chosen: ${_selectedDate.toIso8601String()}');
onJumpToDateSelected(date);
setState(() {
_selectedDate = date;
Expand Down Expand Up @@ -692,7 +699,9 @@ class _CalendarState extends State<Calendar> {
}

Column singleDayTimeWidget(String start, String end) {
print('SingleDayEvent');
widget.onPrintLog != null
? widget.onPrintLog!('SingleDayEvent')
: print('SingleDayEvent');
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.center,
Expand All @@ -704,11 +713,15 @@ class _CalendarState extends State<Calendar> {
}

Column allOrMultiDayDayTimeWidget(NeatCleanCalendarEvent event) {
print('=== Summary: ${event.summary}');
widget.onPrintLog != null
? widget.onPrintLog!('=== Summary: ${event.summary}')
: print('=== Summary: ${event.summary}');
String start = DateFormat('HH:mm').format(event.startTime).toString();
String end = DateFormat('HH:mm').format(event.endTime).toString();
if (event.isAllDay) {
print('AllDayEvent - ${event.summary}');
widget.onPrintLog != null
? widget.onPrintLog!('AllDayEvent - ${event.summary}')
: print('AllDayEvent - ${event.summary}');
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.center,
Expand All @@ -723,16 +736,22 @@ class _CalendarState extends State<Calendar> {
if (event.multiDaySegement == MultiDaySegement.first) {
// The event begins on the selcted day.
// Just show the start time, no end time.
print('MultiDayEvent: start - ${event.summary}');
widget.onPrintLog != null
? widget.onPrintLog!('MultiDayEvent: start - ${event.summary}')
: print('MultiDayEvent: start - ${event.summary}');
end = '';
} else if (event.multiDaySegement == MultiDaySegement.last) {
// The event ends on the selcted day.
// Just show the end time, no start time.
print('MultiDayEvent: end - ${event.summary}');
widget.onPrintLog != null
? widget.onPrintLog!('MultiDayEvent: end - ${event.summary}')
: print('MultiDayEvent: end - ${event.summary}');
start = widget.multiDayEndText;
} else {
// The event spans multiple days.
print('MultiDayEvent: middle - ${event.summary}');
widget.onPrintLog != null
? widget.onPrintLog!('MultiDayEvent: middle - ${event.summary}')
: print('MultiDayEvent: middle - ${event.summary}');
start = widget.allDayEventText;
end = '';
}
Expand Down Expand Up @@ -1144,7 +1163,9 @@ class _CalendarState extends State<Calendar> {
// but typically this method will store the selected date and then call a
// user-defined callback function based on this date.
void handleSelectedDateAndUserCallback(DateTime day) {
print('daySelected: $day');
widget.onPrintLog != null
? widget.onPrintLog!('daySelected: $day')
: print('daySelected: $day');
// Fire onDateSelected callback and onMonthChanged callback.
_launchDateSelectionCallback(day);

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: >-
Simple and clean flutter calendar with ability to slide up/down to show
weekly/monthly calendar. Fork of
https://pub.dev/packages/flutter_clean_calendar
version: 0.4.5
version: 0.4.6
homepage: https://github.com/rwbr/flutter_neat_and_clean_calendar

environment:
Expand Down

0 comments on commit ecd106d

Please sign in to comment.