From 1e413a55d4314d4caf288576cf5ddb7148048f48 Mon Sep 17 00:00:00 2001 From: Parth Baraiya Date: Fri, 24 Nov 2023 16:06:21 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Quick=20fixes=20before=20PR.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/lib/widgets/add_event_form.dart | 5 ++ example/lib/widgets/date_time_selector.dart | 2 - lib/src/calendar_event_data.dart | 46 +++++++++++++++---- lib/src/components/_internal_components.dart | 1 + lib/src/day_view/_internal_day_view_page.dart | 5 +- lib/src/event_controller.dart | 30 ++++++------ .../week_view/_internal_week_view_page.dart | 6 ++- 7 files changed, 67 insertions(+), 28 deletions(-) diff --git a/example/lib/widgets/add_event_form.dart b/example/lib/widgets/add_event_form.dart index 1dcba9d4..a34414ad 100644 --- a/example/lib/widgets/add_event_form.dart +++ b/example/lib/widgets/add_event_form.dart @@ -314,6 +314,11 @@ class _AddOrEditEventFormState extends State { _endDate = DateTime.now().withoutTime; _startTime = null; _endTime = null; + _color = Colors.blue; + + if (mounted) { + setState(() {}); + } } void _displayColorPicker() { diff --git a/example/lib/widgets/date_time_selector.dart b/example/lib/widgets/date_time_selector.dart index a6ee1800..008d5666 100644 --- a/example/lib/widgets/date_time_selector.dart +++ b/example/lib/widgets/date_time_selector.dart @@ -46,8 +46,6 @@ class DateTimeSelectorFormField extends StatefulWidget { } class _DateTimeSelectorFormFieldState extends State { - bool _isSelectorOpen = false; - late var _minimumDate = CalendarConstants.minDate.withoutTime; late var _textEditingController = diff --git a/lib/src/calendar_event_data.dart b/lib/src/calendar_event_data.dart index c4a43ab0..909f53d8 100644 --- a/lib/src/calendar_event_data.dart +++ b/lib/src/calendar_event_data.dart @@ -4,10 +4,12 @@ import 'package:flutter/material.dart'; +import '../calendar_view.dart'; import 'extensions.dart'; -/// Stores all the events on [date] @immutable + +/// {@macro calendar_event_data_doc} class CalendarEventData { /// Specifies date on which all these events are. final DateTime date; @@ -43,13 +45,10 @@ class CalendarEventData { /// Define style of description. final TextStyle? descriptionStyle; - /// Stores all the events on [date]. - /// - /// If [startTime] and [endTime] both are 0 or either of them is null, then - /// event will be considered a full day event. - /// - const CalendarEventData({ + /// {@macro calendar_event_data_doc} + CalendarEventData({ required this.title, + required DateTime date, this.description, this.event, this.color = Colors.blue, @@ -58,8 +57,8 @@ class CalendarEventData { this.titleStyle, this.descriptionStyle, DateTime? endDate, - required this.date, - }) : _endDate = endDate; + }) : _endDate = endDate?.withoutTime, + date = date.withoutTime; DateTime get endDate => _endDate ?? date; @@ -75,6 +74,13 @@ class CalendarEventData { (startTime!.isDayStart && endTime!.isDayStart)); } + bool occursOnDate(DateTime currentDate) { + return currentDate == date || + currentDate == endDate || + (currentDate.isBefore(endDate.withoutTime) && + currentDate.isAfter(date.withoutTime)); + } + Map toJson() => { "date": date, "startTime": startTime, @@ -139,3 +145,25 @@ class CalendarEventData { @override int get hashCode => super.hashCode; } + +/// {@template calendar_event_data_doc} +/// Stores all the events on [date]. +/// +/// If [startTime] and [endTime] both are 0 or either of them is null, then +/// event will be considered a full day event. +/// +/// - [date] and [endDate] are used to define dates only. So, If you +/// are providing any time information with these two arguments, +/// it will be ignored. +/// +/// - [startTime] and [endTime] are used to define the time span of the event. +/// So, If you are providing any day information (year, month, day), it will +/// be ignored. It will also, consider only hour and minutes as time. So, +/// seconds, milliseconds and microseconds will be ignored as well. +/// +/// - [startTime] and [endTime] can no span more then one day. For example, +/// If start time is 11th Nov 11:30 PM and end time is 12th Nov 1:30 AM, it +/// will not be considered as valid time. Events with invalid time will throw +/// [AssertionError] in debug mode and will be ignored in release mode +/// in [DayView] and [WeekView]. +/// {@endtemplate} diff --git a/lib/src/components/_internal_components.dart b/lib/src/components/_internal_components.dart index 7efbb370..a0e4d729 100644 --- a/lib/src/components/_internal_components.dart +++ b/lib/src/components/_internal_components.dart @@ -323,6 +323,7 @@ class EventGenerator extends StatelessWidget { @override Widget build(BuildContext context) { + // TODO: Use SizedBox If possible. return Container( height: height, width: width, diff --git a/lib/src/day_view/_internal_day_view_page.dart b/lib/src/day_view/_internal_day_view_page.dart index 00d32ccf..47c4fbc4 100644 --- a/lib/src/day_view/_internal_day_view_page.dart +++ b/lib/src/day_view/_internal_day_view_page.dart @@ -225,7 +225,10 @@ class InternalDayViewPage extends StatelessWidget { date: date, onTileTap: onTileTap, eventArranger: eventArranger, - events: controller.getEventsOnDay(date), + events: controller.getEventsOnDay( + date, + includeFullDayEvents: false, + ), heightPerMinute: heightPerMinute, eventTileBuilder: eventTileBuilder, scrollNotifier: scrollNotifier, diff --git a/lib/src/event_controller.dart b/lib/src/event_controller.dart index 422b6b2b..a1d40fd5 100644 --- a/lib/src/event_controller.dart +++ b/lib/src/event_controller.dart @@ -124,13 +124,13 @@ class EventController extends ChangeNotifier { //ignore: deprecated_member_use_from_same_package if (_eventFilter != null) return _eventFilter!.call(date, this.events); - return _calendarData.getEventsOnDay(date, - includeFullDays: includeFullDayEvents); + return _calendarData.getEventsOnDay(date.withoutTime, + includeFullDayEvents: includeFullDayEvents); } /// Returns full day events on given day. List> getFullDayEvent(DateTime date) { - return _calendarData.getFullDayEvent(date); + return _calendarData.getFullDayEvent(date.withoutTime); } /// Updates the [eventFilter]. @@ -186,13 +186,18 @@ class CalendarData { UnmodifiableListView> get rangingEventList => UnmodifiableListView(_rangingEventList); - /// Stores all full day events(24hr event) + /// Stores all full day events(24hr event). + /// + /// This includes all full day events that are recurring day events as well. + /// + /// final _fullDayEventList = >[]; UnmodifiableListView> get fullDayEventList => UnmodifiableListView(_fullDayEventList); //#region Data Manipulation Methods void addFullDayEvent(CalendarEventData event) { + // TODO: add separate logic for adding full day event and ranging event. _fullDayEventList.addEventInSortedManner(event); _eventList.add(event); } @@ -203,7 +208,7 @@ class CalendarData { } void addSingleDayEvent(CalendarEventData event) { - final date = event.date.withoutTime; + final date = event.date; if (_singleDayEvents[date] == null) { _singleDayEvents.addAll({ @@ -245,7 +250,7 @@ class CalendarData { } void removeSingleDayEvent(CalendarEventData event) { - if (_singleDayEvents[event.date.withoutTime]?.remove(event) ?? false) { + if (_singleDayEvents[event.date]?.remove(event) ?? false) { _eventList.remove(event); } } @@ -286,7 +291,7 @@ class CalendarData { //#region Data Fetch Methods List> getEventsOnDay(DateTime date, - {bool includeFullDays = true}) { + {bool includeFullDayEvents = true}) { final events = >[]; if (_singleDayEvents[date] != null) { @@ -294,15 +299,12 @@ class CalendarData { } for (final rangingEvent in _rangingEventList) { - if (date == rangingEvent.date || - date == rangingEvent.endDate || - (date.isBefore(rangingEvent.endDate) && - date.isAfter(rangingEvent.date))) { + if (rangingEvent.occursOnDate(date)) { events.add(rangingEvent); } } - if (includeFullDays) { + if (includeFullDayEvents) { events.addAll(getFullDayEvent(date)); } @@ -312,9 +314,9 @@ class CalendarData { /// Returns full day events on given day. List> getFullDayEvent(DateTime date) { final events = >[]; + for (final event in fullDayEventList) { - if (date.difference(event.date).inDays >= 0 && - event.endDate.difference(date).inDays > 0) { + if (event.occursOnDate(date)) { events.add(event); } } diff --git a/lib/src/week_view/_internal_week_view_page.dart b/lib/src/week_view/_internal_week_view_page.dart index 1cd2466e..97287571 100644 --- a/lib/src/week_view/_internal_week_view_page.dart +++ b/lib/src/week_view/_internal_week_view_page.dart @@ -336,8 +336,10 @@ class InternalWeekViewPage extends StatelessWidget { eventArranger: eventArranger, eventTileBuilder: eventTileBuilder, scrollNotifier: scrollConfiguration, - events: controller - .getEventsOnDay(filteredDates[index]), + events: controller.getEventsOnDay( + filteredDates[index], + includeFullDayEvents: false, + ), heightPerMinute: heightPerMinute, ), ],