getOccurrenceAppointment method
Appointment?
getOccurrenceAppointment()
inherited
Returns the occurrence appointment for the given pattern appointment at the specified date.
If there is no appointment occurring on the date specified, null is returned.
patternAppointment - required - The pattern appointment is the start appointment in a recurrence series from which the occurrence appointments are cloned with pattern appointment characteristics.
date - required - The date on which the occurrence appointment is requested.
See also:
Appointment
, the object to hold the data for the event in the calendar.getVisibleAppointments
, which allows to get the appointment collection between the given date range.getPatternAppointment
, which allows to get the pattern appointment of the given occurrence appointment in calendar.SfCalendar.getRecurrenceDateTimeCollection
, which used to get the recurrence date time collection for the given recurrence rule.
class MyAppState extends State<MyApp>{
late CalendarController _calendarController;
late _AppointmentDataSource _dataSource;
late Appointment recurrenceApp;
@override
initState(){
_calendarController = CalendarController();
_dataSource = _getCalendarDataSource();
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SfCalendar(
view: CalendarView.month,
controller: _calendarController,
dataSource: _dataSource,
initialDisplayDate: DateTime(2020,11,27,9),
onTap: (CalendarTapDetails details) {
final DateTime? date = details.date;
final Appointment? occurrenceAppointment =
_dataSource.getOccurrenceAppointment(
recurrenceApp, date!, '');
},
),
),
);
}
_AppointmentDataSource _getCalendarDataSource() {
final List<Appointment> appointments = <Appointment>[];
recurrenceApp = Appointment(
startTime: DateTime(2020,11,27,9),
endTime: DateTime(2020,11,27,9).add(const Duration(hours: 2)),
subject: 'Meeting',
color: Colors.cyanAccent,
startTimeZone: '',
endTimeZone: '',
recurrenceRule: 'FREQ=DAILY;INTERVAL=2;COUNT=5',
);
appointments.add(recurrenceApp);
appointments.add(Appointment(
startTime: DateTime(2020,11,28,5),
endTime: DateTime(2020,11,30,7),
subject: 'Discussion',
color: Colors.orangeAccent,
startTimeZone: '',
endTimeZone: '',
isAllDay: true
));
return _AppointmentDataSource(appointments);
}
}
class _AppointmentDataSource extends CalendarDataSource {
_AppointmentDataSource(List<Appointment> source){
appointments = source;
}
}
Implementation
Appointment? getOccurrenceAppointment(
Object? patternAppointment, DateTime date, String calendarTimeZone) {
if (patternAppointment == null) {
return null;
}
final List<dynamic> patternAppointmentColl = <dynamic>[patternAppointment];
final List<CalendarAppointment> patternAppointments =
AppointmentHelper.generateCalendarAppointments(
this, calendarTimeZone, patternAppointmentColl);
final CalendarAppointment patternCalendarAppointment =
patternAppointments[0];
if (patternCalendarAppointment.recurrenceRule == null ||
patternCalendarAppointment.recurrenceRule!.isEmpty) {
return null;
} else if (CalendarViewHelper.isDateInDateCollection(
patternCalendarAppointment.recurrenceExceptionDates, date)) {
final List<CalendarAppointment> dataSourceAppointments =
AppointmentHelper.generateCalendarAppointments(
this, calendarTimeZone);
for (int i = 0; i < dataSourceAppointments.length; i++) {
final CalendarAppointment dataSourceAppointment =
dataSourceAppointments[i];
if (patternCalendarAppointment.id ==
dataSourceAppointment.recurrenceId &&
(isSameDate(dataSourceAppointment.startTime, date))) {
return dataSourceAppointment.convertToCalendarAppointment();
}
}
} else {
final List<CalendarAppointment> occurrenceAppointments =
AppointmentHelper.getVisibleAppointments(
date, date, patternAppointments, calendarTimeZone, false,
canCreateNewAppointment: false);
if (occurrenceAppointments.isEmpty) {
return null;
}
return occurrenceAppointments[0].convertToCalendarAppointment();
}
return null;
}