diff --git a/example/lib/widgets/add_event_form.dart b/example/lib/widgets/add_event_form.dart index 4b090919..b8ee0619 100644 --- a/example/lib/widgets/add_event_form.dart +++ b/example/lib/widgets/add_event_form.dart @@ -29,7 +29,7 @@ class _AddOrEditEventFormState extends State { DateTime? _startTime; DateTime? _endTime; RepeatFrequency? _selectedFrequency; - int _selectedIndex = DateTime.now().weekday - 1; + List _selectedDays = List.filled(7, false); Color _color = Colors.blue; @@ -45,6 +45,7 @@ class _AddOrEditEventFormState extends State { super.initState(); _setDefaults(); + _setInitialWeekday(); } @override @@ -298,13 +299,18 @@ class _AddOrEditEventFormState extends State { (int index) { return ChoiceChip( label: Text(AppConstants.weekTitles[index]), - selected: _selectedIndex == index, + showCheckmark: false, + selected: _selectedDays[index], onSelected: (bool selected) { - if (selected) { - _selectedIndex = index; - } else if (_selectedIndex == index) { - _selectedIndex = index; - } + setState(() { + _selectedDays[index] = selected; + if (!_selectedDays.contains(true)) { + _selectedDays[_startDate.weekday - 1] = true; + } + }); + + debugPrint('weekdays: ${_selectedDays}'); + debugPrint('weekdays: ${_selectedIndexes}'); }, shape: CircleBorder(), ); @@ -350,19 +356,38 @@ class _AddOrEditEventFormState extends State { _form.currentState?.save(); final event = CalendarEventData( - date: _startDate, - endTime: _endTime, - startTime: _startTime, - endDate: _endDate, - color: _color, - title: _titleController.text.trim(), - description: _descriptionController.text.trim(), - ); + date: _startDate, + endTime: _endTime, + startTime: _startTime, + endDate: _endDate, + color: _color, + title: _titleController.text.trim(), + description: _descriptionController.text.trim(), + recurrenceSettings: RecurrenceSettings( + _startDate, + frequency: _selectedFrequency ?? RepeatFrequency.daily, + weekdays: _selectedIndexes, + )); widget.onEventAdd?.call(event); _resetForm(); } + List get _selectedIndexes { + List selectedIndexes = []; + for (int i = 0; i < _selectedDays.length; i++) { + if (_selectedDays[i] == true) { + selectedIndexes.add(i); + } + } + return selectedIndexes; + } + + void _setInitialWeekday() { + final currentWeekday = DateTime.now().weekday - 1; + _selectedDays[currentWeekday] = true; + } + void _setDefaults() { if (widget.event == null) return; diff --git a/lib/src/event_controller.dart b/lib/src/event_controller.dart index fabaf3a8..f822c1be 100644 --- a/lib/src/event_controller.dart +++ b/lib/src/event_controller.dart @@ -141,6 +141,7 @@ class EventController extends ChangeNotifier { final events = _calendarData.getEventsOnDay(date.withoutTime, includeFullDayEvents: includeFullDayEvents); events.addAll(getRepeatedEvents(date)); + debugPrint('repeated: ${getRepeatedEvents(date)}'); return events; } @@ -160,7 +161,7 @@ class EventController extends ChangeNotifier { events.add(event); break; case RepeatFrequency.weekly: - if (event.recurrenceSettings!.weekdays.contains(date.weekday)) { + if (event.recurrenceSettings!.weekdays.contains(date.weekday - 1)) { events.add(event); } break; diff --git a/lib/src/extensions.dart b/lib/src/extensions.dart index 36077fd0..3dc355fd 100644 --- a/lib/src/extensions.dart +++ b/lib/src/extensions.dart @@ -132,6 +132,31 @@ extension DateTimeExtensions on DateTime { "This extension is not being used in this package and will be removed " "in next major release. Please use withoutTime instead.") DateTime get dateYMD => DateTime(year, month, day); + + /// Get weekday from day of date + WeekDays get getWeekDays { + switch (this.weekday) { + case 1: + return WeekDays.monday; + case 2: + return WeekDays.tuesday; + case 3: + return WeekDays.wednesday; + case 4: + return WeekDays.thursday; + case 5: + return WeekDays.friday; + case 6: + return WeekDays.saturday; + case 7: + return WeekDays.sunday; + default: + throw Exception(""" + Date $this has unrecognisable weekday expencted [1-7], + but ${this.weekday} was provided. + """); + } + } } extension ColorExtension on Color {