class ICalendar { final List _events = []; void addEvent( {required String summary, required String description, required DateTime startTime, required DateTime endTime, required List attendees, bool allday = false}) { _events.add( ICalendarEvent( summary: summary, description: description, startTime: startTime, endTime: endTime, attendees: attendees, allday: allday), ); } String serialize() { final dtstamp = DateTime.now(); final buffer = StringBuffer(); buffer.writeln('BEGIN:VCALENDAR'); buffer.writeln('VERSION:2.0'); buffer.writeln('PRODID:-//Your Organization//Your Product//EN'); for (final event in _events) { buffer.writeln('BEGIN:VEVENT'); buffer.writeln( 'UID:${DateTime.now().millisecondsSinceEpoch}-${event.hashCode}'); buffer.writeln('DTSTAMP:${_formatDateTime(dtstamp)}'); buffer.writeln( 'DTSTART:${event.allday ? _formatDate(event.startTime) : _formatDateTime(event.startTime)}'); buffer.writeln( 'DTEND:${event.allday ? _formatDate(event.startTime.add(Duration(days: 1))) : _formatDateTime(event.endTime)}'); buffer.writeln('SUMMARY:${event.summary}'); buffer.writeln('DESCRIPTION:${event.description}'); for (final attendee in event.attendees) { buffer.writeln( 'ATTENDEE;CN="${attendee.toString()}";ROLE=REQ-PARTICIPANT;RSVP=TRUE:mailto:test@me.com'); } buffer.writeln('END:VEVENT'); } buffer.writeln('END:VCALENDAR'); return buffer.toString(); } String _formatDateTime(DateTime dt) { return '${dt.toUtc().toString().replaceAll('-', '').replaceAll(':', '').replaceAll(' ', 'T').split('.')[0]}Z'; } String _formatDate(DateTime dateTime) { // Extract year, month, and day from the DateTime object String year = dateTime.year.toString().padLeft(4, '0'); String month = dateTime.month.toString().padLeft(2, '0'); String day = dateTime.day.toString().padLeft(2, '0'); // Combine into the YYYYMMDD format return '$year$month$day'; } } class ICalendarEvent { final String summary; final String description; final DateTime startTime; final DateTime endTime; final List attendees; final bool allday; ICalendarEvent({ required this.summary, required this.description, required this.startTime, required this.endTime, required this.attendees, this.allday = false, }); }