add_2_calendar_plus 1.0.0
add_2_calendar_plus: ^1.0.0 copied to clipboard
A Flutter plugin to add, update, retrieve, and delete events on each platform's default calendar.
import 'package:flutter/material.dart';
import 'package:add_2_calendar_plus/add_2_calendar_plus.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Add 2 Calendar Plus',
home: CalendarDemoPage(),
);
}
}
class CalendarDemoPage extends StatefulWidget {
const CalendarDemoPage({super.key});
@override
State<CalendarDemoPage> createState() => _CalendarDemoPageState();
}
class _CalendarDemoPageState extends State<CalendarDemoPage> {
Event? _savedEvent;
String _status = 'No event saved yet.';
Event _buildEvent({String? id, String title = 'Test Event'}) {
return Event(
id: id,
title: title,
description: 'Created by add_2_calendar_plus example',
location: 'Flutter app',
startDate: DateTime.now().add(const Duration(hours: 1)),
endDate: DateTime.now().add(const Duration(hours: 2)),
allDay: false,
iosParams: const IOSParams(
reminder: Duration(minutes: 30),
url: 'http://example.com',
),
androidParams: const AndroidParams(
emailInvites: ['test@example.com'],
),
);
}
void _showSnack(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message)),
);
}
Future<void> _addEvent() async {
final event = await Add2Calendar.addEvent2Cal(_buildEvent());
if (event != null) {
setState(() {
_savedEvent = event;
_status = 'Added event. ID: ${event.id}';
});
_showSnack('Event added! ID: ${event.id}');
} else {
_showSnack('Failed to add event.');
}
}
Future<void> _getEvent() async {
final id = _savedEvent?.id;
if (id == null) {
_showSnack('Add an event first.');
return;
}
final event = await Add2Calendar.getEvent(id);
if (event != null) {
setState(() => _status = 'Got event: "${event.title}" (${event.startDate})');
_showSnack('Got event: "${event.title}"');
} else {
setState(() => _status = 'Event not found (ID: $id).');
_showSnack('Event not found.');
}
}
Future<void> _updateEvent() async {
final id = _savedEvent?.id;
if (id == null) {
_showSnack('Add an event first.');
return;
}
final updated = _buildEvent(id: id, title: 'Updated Test Event');
final result = await Add2Calendar.updateEvent(updated);
if (result != null) {
setState(() {
_savedEvent = result;
_status = 'Updated event. Title: "${result.title}"';
});
_showSnack('Event updated!');
} else {
_showSnack('Failed to update event.');
}
}
Future<void> _deleteEvent() async {
final id = _savedEvent?.id;
if (id == null) {
_showSnack('Add an event first.');
return;
}
final success = await Add2Calendar.deleteEvent(id);
if (success) {
setState(() {
_savedEvent = null;
_status = 'Event deleted (was ID: $id).';
});
_showSnack('Event deleted!');
} else {
_showSnack('Failed to delete event.');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Add 2 Calendar Plus')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Text(
_status,
style: Theme.of(context).textTheme.bodyMedium,
textAlign: TextAlign.center,
),
),
const Divider(),
Expanded(
child: ListView(
children: [
ListTile(
leading: const Icon(Icons.add_circle_outline),
title: const Text('Add event'),
subtitle: const Text('Adds a new event to the calendar'),
onTap: _addEvent,
),
const Divider(),
ListTile(
leading: const Icon(Icons.search),
title: const Text('Get event'),
subtitle: const Text('Retrieves the saved event by ID'),
enabled: _savedEvent != null,
onTap: _getEvent,
),
const Divider(),
ListTile(
leading: const Icon(Icons.edit_outlined),
title: const Text('Update event'),
subtitle: const Text('Updates the saved event title'),
enabled: _savedEvent != null,
onTap: _updateEvent,
),
const Divider(),
ListTile(
leading: const Icon(Icons.delete_outline),
title: const Text('Delete event'),
subtitle: const Text('Deletes the saved event from the calendar'),
enabled: _savedEvent != null,
onTap: _deleteEvent,
),
const Divider(),
],
),
),
],
),
);
}
}