Skip to content

Commit

Permalink
fix(events): Possibility to update the events shown #37
Browse files Browse the repository at this point in the history
* Closes #37
* The internal eventsMap got filled only in initState before
* Now every time the widget re-renders the update of the internal events map happens
* Allows the user to update the list of events displayed by the calendar widget

#37
  • Loading branch information
agfeo-rw committed Jun 3, 2022
1 parent a19b646 commit c5c7e3c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [0.3.6+19] - 2022-06-03
* Issue #37 closed

## [0.3.5+18] - 2022-05-04
* Option to change color of selected day when same date as today

Expand Down
26 changes: 18 additions & 8 deletions lib/flutter_neat_and_clean_calendar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ class _CalendarState extends State<Calendar> {
super.initState();
isExpanded = widget.isExpanded;

_selectedDate = widget.initialDate ?? DateTime.now();
initializeDateFormatting(widget.locale, null).then((_) => setState(() {
var monthFormat =
DateFormat('MMMM yyyy', widget.locale).format(_selectedDate);
displayMonth =
'${monthFormat[0].toUpperCase()}${monthFormat.substring(1)}';
}));
}

/// The method [_updateEventsMap] has the purpose to update the eventsMap, when the calendar widget
/// renders its view. When this method executes, it fills the eventsMap with the contents of the
/// given eventsList. This can be used to update the events shown by the calendar.
void _updateEventsMap() {
eventsMap = widget.events ?? {};
// If the user provided a list of events, then convert it to a map, but only if there
// was no map of events provided. To provide the events in form of a map is the way,
Expand Down Expand Up @@ -260,21 +273,16 @@ class _CalendarState extends State<Calendar> {
}
});
}

_selectedDate = widget.initialDate ?? DateTime.now();
selectedMonthsDays = _daysInMonth(_selectedDate);
selectedWeekDays = Utils.daysInRange(
_firstDayOfWeek(_selectedDate), _lastDayOfWeek(_selectedDate))
.toList();
initializeDateFormatting(widget.locale, null).then((_) => setState(() {
var monthFormat =
DateFormat('MMMM yyyy', widget.locale).format(_selectedDate);
displayMonth =
'${monthFormat[0].toUpperCase()}${monthFormat.substring(1)}';
}));

_selectedEvents = eventsMap?[DateTime(
_selectedDate.year, _selectedDate.month, _selectedDate.day)] ??
[];

print('eventsMap has ${eventsMap?.length} entries');
}

Widget get nameAndIconRow {
Expand Down Expand Up @@ -714,6 +722,8 @@ class _CalendarState extends State<Calendar> {

@override
Widget build(BuildContext context) {
_updateEventsMap();

// If _selectedEvents is not null, then we sort the events by isAllDay propeerty, so that
// all day events are displayed at the top of the list.
// Slightly inexxficient, to do this sort each time, the widget builds.
Expand Down
23 changes: 8 additions & 15 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -80,7 +80,7 @@ packages:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
version: "0.1.4"
meta:
dependency: transitive
description:
Expand All @@ -94,7 +94,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -106,7 +106,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -141,20 +141,13 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.8"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "0.4.9"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
version: "2.1.2"
sdks:
dart: ">=2.14.0 <3.0.0"
dart: ">=2.17.0-0 <3.0.0"
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.3.5+18
version: 0.3.6+19
homepage: https://github.com/rwbr/flutter_neat_and_clean_calendar

environment:
Expand Down

0 comments on commit c5c7e3c

Please sign in to comment.