Skip to content

Commit

Permalink
Add dynamic events and holidays
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksanderwozniak committed Sep 22, 2019
1 parent 2892056 commit c22b0b7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
6 changes: 2 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
_selectedEvents = _events[_selectedDay] ?? [];

_calendarController = CalendarController();
_calendarController.events = _events;
_calendarController.holidays = _holidays;

_animationController = AnimationController(
vsync: this,
Expand Down Expand Up @@ -125,8 +127,6 @@ class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
Widget _buildTableCalendar() {
return TableCalendar(
calendarController: _calendarController,
events: _events,
holidays: _holidays,
startingDayOfWeek: StartingDayOfWeek.monday,
calendarStyle: CalendarStyle(
selectedColor: Colors.deepOrange[400],
Expand All @@ -151,8 +151,6 @@ class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
return TableCalendar(
locale: 'pl_PL',
calendarController: _calendarController,
events: _events,
holidays: _holidays,
initialCalendarFormat: CalendarFormat.month,
formatAnimation: FormatAnimation.slide,
startingDayOfWeek: StartingDayOfWeek.sunday,
Expand Down
18 changes: 2 additions & 16 deletions lib/src/calendar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ class TableCalendar extends StatefulWidget {
/// If nothing is provided, a default locale will be used.
final dynamic locale;

/// Contains a `List` of objects (eg. events) assigned to particular `DateTime`s.
/// Each `DateTime` inside this `Map` should get its own `List` of above mentioned objects.
final Map<DateTime, List> events;

/// `List`s of holidays associated to particular `DateTime`s.
/// This property allows you to provide custom holiday rules.
final Map<DateTime, List> holidays;

/// Called whenever any day gets tapped.
final OnDaySelected onDaySelected;

Expand Down Expand Up @@ -127,8 +119,6 @@ class TableCalendar extends StatefulWidget {
Key key,
@required this.calendarController,
this.locale,
this.events = const {},
this.holidays = const {},
this.onDaySelected,
this.onUnavailableDaySelected,
this.onVisibleDaysChanged,
Expand Down Expand Up @@ -170,8 +160,6 @@ class _TableCalendarState extends State<TableCalendar> with SingleTickerProvider
super.initState();

widget.calendarController._init(
events: widget.events,
holidays: widget.holidays,
initialDay: widget.initialSelectedDay,
initialFormat: widget.initialCalendarFormat,
availableCalendarFormats: widget.availableCalendarFormats,
Expand Down Expand Up @@ -236,13 +224,11 @@ class _TableCalendarState extends State<TableCalendar> with SingleTickerProvider
}

DateTime _getEventKey(DateTime day) {
return widget.calendarController.visibleEvents.keys
.firstWhere((it) => widget.calendarController._isSameDay(it, day), orElse: () => null);
return widget.calendarController._getEventKey(day);
}

DateTime _getHolidayKey(DateTime day) {
return widget.calendarController.visibleHolidays.keys
.firstWhere((it) => widget.calendarController._isSameDay(it, day), orElse: () => null);
return widget.calendarController._getHolidayKey(day);
}

@override
Expand Down
76 changes: 49 additions & 27 deletions lib/src/calendar_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ typedef void _SelectedDayCallback(DateTime day);
/// }
/// ```
class CalendarController {
/// `Map` of events.
/// Each `DateTime` inside this `Map` should get its own `List` of objects (i.e. events).
Map<DateTime, List> events;

/// `Map` of holidays.
/// This property allows you to provide custom holiday rules.
Map<DateTime, List> holidays;

/// Currently focused day (used to determine which year/month should be visible).
DateTime get focusedDay => _focusedDay;

Expand All @@ -38,34 +46,44 @@ class CalendarController {
List<DateTime> get visibleDays =>
_includeInvisibleDays ? _visibleDays.value : _visibleDays.value.where((day) => !_isExtraDay(day)).toList();

/// Map of currently visible events.
Map<DateTime, List> get visibleEvents => Map.fromEntries(
_events.entries.where((entry) {
for (final day in visibleDays) {
if (_isSameDay(day, entry.key)) {
return true;
}
/// `Map` of currently visible events.
Map<DateTime, List> get visibleEvents {
if (events == null) {
return {};
}

return Map.fromEntries(
events.entries.where((entry) {
for (final day in visibleDays) {
if (_isSameDay(day, entry.key)) {
return true;
}
}

return false;
}),
);
}

return false;
}),
);

/// Map of currently visible holidays.
Map<DateTime, List> get visibleHolidays => Map.fromEntries(
_holidays.entries.where((entry) {
for (final day in visibleDays) {
if (_isSameDay(day, entry.key)) {
return true;
}
/// `Map` of currently visible holidays.
Map<DateTime, List> get visibleHolidays {
if (holidays == null) {
return {};
}

return Map.fromEntries(
holidays.entries.where((entry) {
for (final day in visibleDays) {
if (_isSameDay(day, entry.key)) {
return true;
}
}

return false;
}),
);
return false;
}),
);
}

Map<DateTime, List> _events;
Map<DateTime, List> _holidays;
DateTime _focusedDay;
DateTime _selectedDay;
StartingDayOfWeek _startingDayOfWeek;
Expand All @@ -81,8 +99,6 @@ class CalendarController {
_SelectedDayCallback _selectedDayCallback;

void _init({
@required Map<DateTime, List> events,
@required Map<DateTime, List> holidays,
@required DateTime initialDay,
@required CalendarFormat initialFormat,
@required Map<CalendarFormat, String> availableCalendarFormats,
Expand All @@ -92,8 +108,6 @@ class CalendarController {
@required OnVisibleDaysChanged onVisibleDaysChanged,
@required bool includeInvisibleDays,
}) {
_events = events;
_holidays = holidays;
_availableCalendarFormats = availableCalendarFormats;
_startingDayOfWeek = startingDayOfWeek;
_useNextCalendarFormat = useNextCalendarFormat;
Expand Down Expand Up @@ -403,6 +417,14 @@ class CalendarController {
}
}

DateTime _getEventKey(DateTime day) {
return visibleEvents.keys.firstWhere((it) => _isSameDay(it, day), orElse: () => null);
}

DateTime _getHolidayKey(DateTime day) {
return visibleHolidays.keys.firstWhere((it) => _isSameDay(it, day), orElse: () => null);
}

/// Returns true if `day` is currently selected.
bool isSelected(DateTime day) {
return _isSameDay(day, selectedDay);
Expand Down

0 comments on commit c22b0b7

Please sign in to comment.