popup_dialog.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_local_notifications/flutter_local_notifications.dart';
  4. import 'package:flutter_native_timezone/flutter_native_timezone.dart';
  5. import 'package:slide_popup_dialog/slide_popup_dialog.dart' as slideDialog;
  6. import 'package:trpp/data/data.dart';
  7. import 'package:timezone/data/latest.dart' as tz;
  8. import 'package:timezone/timezone.dart' as tz;
  9. import 'package:trpp/screens/note.dart';
  10. /// Slider for scheduling a notification
  11. class AddNotificationDialog {
  12. BuildContext _context;
  13. bool _isSwitched = false;
  14. Function getResult;
  15. DateTime _chosenTime;
  16. bool _chosenDelete;
  17. AddNoteScreenState parent;
  18. AddNotificationDialog(BuildContext context, Function getResult, NotesModel nm,
  19. AddNoteScreenState parent) {
  20. _context = context;
  21. this.getResult = getResult;
  22. this.parent = parent;
  23. _chosenTime = DateTime.now();
  24. _chosenDelete = _isSwitched;
  25. initTimeZone();
  26. }
  27. /// Initializes user timezone
  28. void initTimeZone() async {
  29. tz.initializeTimeZones();
  30. tz.setLocalLocation(
  31. tz.getLocation(await FlutterNativeTimezone.getLocalTimezone()));
  32. }
  33. /// Opens slider
  34. void showDialog() {
  35. slideDialog.showSlideDialog(
  36. context: _context,
  37. child: _widget(),
  38. );
  39. }
  40. Widget _widget() {
  41. // Main structure of the slider
  42. return SafeArea(
  43. child: Column(
  44. children: [
  45. Text('Notification options',
  46. style: TextStyle(
  47. fontSize: 24, color: Theme.of(_context).primaryColor)),
  48. _picker2(),
  49. Stack(
  50. children: <Widget>[
  51. Align(
  52. alignment: Alignment.bottomLeft,
  53. child: _removeBtn(),
  54. ),
  55. Align(
  56. alignment: Alignment.bottomRight,
  57. child: _okBtn(),
  58. ),
  59. ],
  60. )
  61. ],
  62. ),
  63. );
  64. }
  65. Widget _picker2() {
  66. // DataTime picker widget
  67. return Container(
  68. child: CupertinoTheme(
  69. data: CupertinoThemeData(
  70. textTheme: CupertinoTextThemeData(
  71. dateTimePickerTextStyle: TextStyle(
  72. fontSize: 18,
  73. color: Theme.of(_context).primaryColor,
  74. ),
  75. ),
  76. ),
  77. child: CupertinoDatePicker(
  78. initialDateTime: getInitialDateTime(),
  79. onDateTimeChanged: (dateTime) {
  80. _chosenTime = dateTime;
  81. },
  82. use24hFormat: true,
  83. ),
  84. ),
  85. width: 400,
  86. height: 300,
  87. );
  88. }
  89. /// Returns time that was scheduled if user already created a notification
  90. DateTime getInitialDateTime() {
  91. if (parent.notificationModel != null)
  92. return DateTime.parse(parent.notificationModel.date1);
  93. else
  94. return DateTime.now();
  95. }
  96. Widget _okBtn() {
  97. return Padding(
  98. padding: EdgeInsets.only(right: 24),
  99. child: OutlineButton(
  100. onPressed: () {
  101. Navigator.of(_context).pop();
  102. saveNotification();
  103. getResult(_chosenTime, _chosenDelete);
  104. },
  105. highlightedBorderColor: Theme.of(_context).accentColor,
  106. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
  107. child: Text(
  108. 'OK',
  109. style: TextStyle(
  110. fontWeight: FontWeight.w500,
  111. fontSize: 14,
  112. color: Theme.of(_context).accentColor),
  113. ),
  114. ),
  115. );
  116. }
  117. Widget _removeBtn() {
  118. return Padding(
  119. padding: EdgeInsets.only(left: 24),
  120. child: OutlineButton(
  121. onPressed: () {
  122. Navigator.of(_context).pop();
  123. cancelScheduledNotification();
  124. },
  125. highlightedBorderColor: Theme.of(_context).accentColor,
  126. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
  127. child: Text(
  128. 'Cancel',
  129. style: TextStyle(fontSize: 14, color: Theme.of(_context).errorColor),
  130. ),
  131. ),
  132. );
  133. }
  134. /// Schedules notification
  135. scheduleNotification() async {
  136. if (parent.nm != null) {
  137. FlutterLocalNotificationsPlugin plugin =
  138. FlutterLocalNotificationsPlugin();
  139. const AndroidInitializationSettings initializationSettingsAndroid =
  140. AndroidInitializationSettings('ic_launcher');
  141. final IOSInitializationSettings initializationSettingsIOS =
  142. IOSInitializationSettings(
  143. onDidReceiveLocalNotification: onDidReceiveLocalNotification);
  144. final MacOSInitializationSettings initializationSettingsMacOS =
  145. MacOSInitializationSettings();
  146. final InitializationSettings initializationSettings =
  147. InitializationSettings(
  148. android: initializationSettingsAndroid,
  149. iOS: initializationSettingsIOS,
  150. macOS: initializationSettingsMacOS);
  151. await plugin.initialize(initializationSettings,
  152. onSelectNotification: selectNotification);
  153. if (getTime().isAfter(DateTime.now())) {
  154. print("scheduled to:" + parent.notificationModel.date1);
  155. await plugin.zonedSchedule(
  156. parent.notificationModel.note,
  157. parent.nm.getTitleFromModel(16),
  158. parent.nm.getShortDesc(24),
  159. getTime(),
  160. const NotificationDetails(
  161. android: AndroidNotificationDetails('your channel id',
  162. 'Notifications', 'Channel for your alarms')),
  163. androidAllowWhileIdle: true,
  164. uiLocalNotificationDateInterpretation:
  165. UILocalNotificationDateInterpretation.absoluteTime);
  166. }
  167. }
  168. }
  169. /// Cancels notifications
  170. void cancelScheduledNotification() {
  171. print("Cancel");
  172. if (parent.notificationModel != null) {
  173. if (parent.notificationModel.id != null) {
  174. FlutterLocalNotificationsPlugin plugin =
  175. FlutterLocalNotificationsPlugin();
  176. plugin.cancel(parent.notificationModel.note);
  177. NotesDatabaseService.db
  178. .deleteNotificationInDB(parent.notificationModel);
  179. parent.notificationModel = null;
  180. }
  181. }
  182. }
  183. /// Gets time from DateTimePicker
  184. tz.TZDateTime getTime() {
  185. tz.TZDateTime a = tz.TZDateTime.from(_chosenTime, tz.local);
  186. return a;
  187. }
  188. /// Saves notification in db
  189. saveNotification() async {
  190. cancelScheduledNotification();
  191. parent.notificationModel = new NotificationModel();
  192. parent.notificationModel.makeData(_chosenTime);
  193. if (parent.nm != null) parent.notificationModel.note = parent.nm.id;
  194. parent.notificationModel = await NotesDatabaseService.db
  195. .addNotificationInDB(parent.notificationModel);
  196. if (parent.notificationModel.id != null) scheduleNotification();
  197. }
  198. /// DummyFunction
  199. Future onDidReceiveLocalNotification(
  200. int id, String title, String body, String payload) async{return;}
  201. /// DummyFunction
  202. Future selectNotification(String payload) async{
  203. if (payload != null) {
  204. debugPrint('notification payload: $payload');
  205. return;
  206. }
  207. }
  208. }