Flutter插件 添加日历提醒功能

这边有个需求是在日历中添加事件提醒,所以做了一个轮子。

刚好安卓这边有接口可以实现,

贴一下文档地址。

https://www.android-doc.com/guide/topics/providers/calendar-provider.html

不得不说直接看文档能更直白的了解他。

由于是Flutter这的插件,我这边先设计了一个日历类,类里的变量如下。

//标题参数
  String _title;

  //备注
  String _note;

  //提醒时间,默认提前15分钟提醒
  //可以设置多个提醒时间
  List<int> _alert;

  //事件id,根据此id来更新或删除提醒事件
  //当为-1时表示未赋值。
  //创建提醒事件的时候,eventId在后台是自增的。
  //不受此参数影响
  //具体eventId需要在自己保留
  String _eventId;

  //开始时间,不设置即为默认
  DateTime _beginTime;

  //结束时间,不要觉得默认时间很奇怪,连时间都要默认还要这个插件干嘛。
  DateTime _endTime;

  //全天事件,如果为全天事件设置为1。
  int _allDay;

初始化如下

Calendars calendars = new Calendars(DateTime(2020,5,1,12,23),DateTime(2020,5,1,12,34),'hola','note',[5],'1',1);


    Calendars calendars = new Calendars(DateTime(2020,5,1,12,23),DateTime(2020,5,1,12,34),);
    

calendars.setEventId = '111';
    calendars.setTitle = 'hola2';
    calendars.setAlert = [3,15];
    calendars.setBeginTime = DateTime(2020,5,2,12,34);
    calendars.setEndTime = DateTime(2020,5,2,12,35);
    calendars.setNote = '这里不是备注内容/🐶';

其中起始时间和截止时间是必填参数,其他诸如标题,备注,提醒时间,是否为全天事件均为可填参数。

在日历中添加提醒事件需要日历的读写权限,这边写了CheckReadPermission()和CheckWritePermission()这两个方法用于获取读写权限,当没有权限时他会返回false,并弹出弹窗获取权限。

然后有三个方法用于执行添加,删除以及更新提醒事件。

//添加事件
  static Future<String> createEvent(Calendars calendars) async {
    final String eventId = await _channel.invokeMethod('createEvent', <String, dynamic>{
      'title': calendars.getTitle,
      'beginTime': calendars.getBeginTime.millisecondsSinceEpoch,
      'endTime': calendars.getEndTime.millisecondsSinceEpoch,
      'alert': calendars.getAlert,
      'note': calendars.getNote,
      'allDay':calendars.getAllDay,
    });
    return eventId;
  }

  //删除提醒事件
  static Future<bool> deleteEvent(String eventId) async{
    final bool res = await _channel.invokeMethod('deleteEvent', <String, dynamic>{'eventId': eventId});
    return res;
  }

  //更新提醒事件
  static Future<String> updateEvent(Calendars calendars) async {
    final String resId = await _channel.invokeMethod('updateEvent', <String, dynamic>{
      'eventId': calendars.getEventId,
      'title': calendars.getTitle,
      'beginTime': calendars.getBeginTime.millisecondsSinceEpoch,
      'endTime': calendars.getEndTime.millisecondsSinceEpoch,
      'alert': calendars.getAlert,
      'note': calendars.getNote,
      'allDay':calendars.getAllDay,
    });
    return resId;
  }

增加和更新事件会返回提醒事件的id,删除事件和更新事件时需要提供id。

现在大致的功能实现完成了,但只是安卓方面的,代码我已经传到了github上。如果有ios的大佬愿意来帮助丰富这个插件,那么实在感激不尽。

代码地址:https://github.com/afei1223/AddCalendar

欢迎来fork和star

posted @ 2020-04-28 10:27  阿飞飞啊飞  阅读(3452)  评论(0编辑  收藏  举报