addEvent static method

Future<void> addEvent(
  1. BuildContext context, {
  2. required Event event,
  3. Function? onPermissionDenied,
  4. Function? onError,
  5. Function? onSubmitted,
  6. String? dialogBoxTitle,
})

Implementation

static Future<void> addEvent(BuildContext context,
    {required Event event,
      Function? onPermissionDenied,
      Function? onError,
      Function? onSubmitted,
      String? dialogBoxTitle}) async {
  try {
    if (Platform.isAndroid) {
      List calendars = await _channel.invokeMethod('getCalendarList');
      List<DeviceCalendar> modelList = calendars
          .map((map) => DeviceCalendar.fromMap(_convertMap(map)))
          .toList();
      if (modelList.length == 1) {
        String createdEventId = await _addEventToCalendar(
            event: event, calendarId: modelList.first.id);
        if (onSubmitted != null) {
          onSubmitted(createdEventId);
        }
      } else {
        _showCalendarsListDialog(
            context: context,
            calendars: modelList,
            onCalendarSelected: (id) async {
              String createdEventId =
              await _addEventToCalendar(event: event, calendarId: id);
              if (onSubmitted != null) {
                onSubmitted(createdEventId);
              }
            });
      }
    } else {
      String createdEventId =
      await _addEventToCalendar(event: event, calendarId: "0");
      if (onSubmitted != null) {
        onSubmitted(createdEventId);
      }
    }
  } catch (e) {
    debugPrint("Error: $e");
    if (onError != null) {
      onError();
    }
  }
}