這邊有個需求是在日歷中添加事件提醒,所以做了一個輪子。
剛好安卓這邊有接口可以實現,
貼一下文檔地址。
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/libo1223/AddCalendar
歡迎來fork和star