calendar_events 1.0.0 copy "calendar_events: ^1.0.0" to clipboard
calendar_events: ^1.0.0 copied to clipboard

A simple plugin for flutter to play with calendar events. You can easily fetch calendar accounts and add events.

example/lib/main.dart

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:calendar_events/calendar_events.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _calenderEventsPlugin = CalendarEvents();
  CalendarPermission? havePermission;
  List<CalendarAccount>? accounts;
  int? selectedCalenderId;
  final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey =
      GlobalKey<ScaffoldMessengerState>();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ScaffoldMessenger(
        key: scaffoldMessengerKey,
        child: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: SingleChildScrollView(
            child: Column(
              children: [
                if (havePermission == null ||
                    havePermission != CalendarPermission.allowed)
                  ElevatedButton(
                      onPressed: () {
                        _requestPermission();
                      },
                      child: const Text('Request permission')),
                ElevatedButton(
                    onPressed: () {
                      _checkPermission();
                    },
                    child: Text(havePermission == null
                        ? 'Check Permission'
                        : 'You have ${havePermission == null ? "no " : ''} calender permission : $havePermission')),
                if (havePermission != null &&
                    havePermission == CalendarPermission.allowed)
                  ElevatedButton(
                      onPressed: () {
                        _listAccounts();
                      },
                      child: Text(accounts == null
                          ? 'Get accounts'
                          : 'You have got calender accounts')),
                if (accounts != null)
                  for (CalendarAccount account in accounts!)
                    Column(
                      children: [
                        InkWell(
                          onTap: () {
                            _showDialog(account);
                          },
                          child: Container(
                            width: double.infinity,
                            padding: const EdgeInsets.all(10),
                            child: Text(
                                '1. CalendarId: ${account.calenderId}\n2. accountName: ${account.accountName}\n3. ${account.accountType}'),
                          ),
                        ),
                        const Divider(
                          height: 2,
                          thickness: 2,
                        )
                      ],
                    ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  _requestPermission() async {
    if (Platform.isIOS) {
      _calenderEventsPlugin.requestPermission();
      return;
    }
    if (!(await Permission.calendarFullAccess.isGranted)) {
      if (await Permission.calendarFullAccess.request().isGranted) {
        _checkPermission();
      }
    }
  }

  _checkPermission() async {
    havePermission = await _calenderEventsPlugin.checkCalendarPermission();
    setState(() {});
  }

  _listAccounts() async {
    accounts = await _calenderEventsPlugin.getCalendarAccounts();
    setState(() {});
  }

  void _showDialog(CalendarAccount account) {
    showDialog(
        context: context,
        builder: (context) {
          return Container(
            color: Theme.of(context).primaryColor,
            child: Center(
              child: ElevatedButton(
                onPressed: () {
                  _addEvent(account);
                  Navigator.of(context).pop();
                },
                child: const Text('Add Event'),
              ),
            ),
          );
        });
  }

  void _addEvent(CalendarAccount account) async {
    final event = CalendarEvent(
        calendarId: account.calenderId,
        title: 'Sample Event',
        location: 'Location',
        description: 'desc',
        start: DateTime.now().add(const Duration(hours: 1)),
        end: DateTime.now().add(const Duration(hours: 2)),
        recurrence:
            EventRecurrence(frequency: EventFrequency.daily, interval: 2));

    var bool = await _calenderEventsPlugin.addEvent(event);
    var showSnackBar = scaffoldMessengerKey.currentState?.showSnackBar(SnackBar(
        content: Text('Calender added ${bool ? 'success' : 'failed'}')));

    bool = await _calenderEventsPlugin.requestSync(account);
    if (kDebugMode) {
      print('Syncing : $bool');
    }
  }
}
4
likes
0
points
36
downloads

Publisher

unverified uploader

Weekly Downloads

A simple plugin for flutter to play with calendar events. You can easily fetch calendar accounts and add events.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on calendar_events