Refactor notification
This commit is contained in:
		
							parent
							
								
									13fb26be6d
								
							
						
					
					
						commit
						78b23af75a
					
				@ -1,6 +1,8 @@
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
 | 
			
		||||
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
 | 
			
		||||
import 'package:get/get.dart';
 | 
			
		||||
import 'package:sqflite_pust_local_notification/models/task_model.dart';
 | 
			
		||||
import 'package:timezone/data/latest.dart' as tz;
 | 
			
		||||
import 'package:timezone/timezone.dart' as tz;
 | 
			
		||||
 | 
			
		||||
@ -9,7 +11,7 @@ class NotifyHelper {
 | 
			
		||||
      FlutterLocalNotificationsPlugin(); //
 | 
			
		||||
 | 
			
		||||
  initializeNotification() async {
 | 
			
		||||
    tz.initializeTimeZones();
 | 
			
		||||
    _configureLocalTimezone();
 | 
			
		||||
    final IOSInitializationSettings initializationSettingsIOS =
 | 
			
		||||
        IOSInitializationSettings(
 | 
			
		||||
            requestSoundPermission: false,
 | 
			
		||||
@ -48,10 +50,12 @@ class NotifyHelper {
 | 
			
		||||
        channelDescription: '',
 | 
			
		||||
        importance: Importance.max,
 | 
			
		||||
        priority: Priority.high);
 | 
			
		||||
 | 
			
		||||
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
 | 
			
		||||
    var platformChannelSpecifics = new NotificationDetails(
 | 
			
		||||
        android: androidPlatformChannelSpecifics,
 | 
			
		||||
        iOS: iOSPlatformChannelSpecifics);
 | 
			
		||||
 | 
			
		||||
    await flutterLocalNotificationsPlugin.show(
 | 
			
		||||
      0,
 | 
			
		||||
      title,
 | 
			
		||||
@ -61,46 +65,45 @@ class NotifyHelper {
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  scheduledNotification() async {
 | 
			
		||||
  scheduledNotification(int hours, int minutes, TaskModel taskModel) async {
 | 
			
		||||
    print("Call Notification");
 | 
			
		||||
    await flutterLocalNotificationsPlugin.zonedSchedule(
 | 
			
		||||
        0,
 | 
			
		||||
        'scheduled title',
 | 
			
		||||
        'theme changes 5 seconds ago',
 | 
			
		||||
        tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
 | 
			
		||||
        taskModel.id!.toInt(),
 | 
			
		||||
        taskModel.title,
 | 
			
		||||
        taskModel.note,
 | 
			
		||||
        _convertTimeDeration(hours, minutes),
 | 
			
		||||
        //tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
 | 
			
		||||
        const NotificationDetails(
 | 
			
		||||
            android: AndroidNotificationDetails(
 | 
			
		||||
                'your channel id', 'your channel name',
 | 
			
		||||
                channelDescription: '')),
 | 
			
		||||
        androidAllowWhileIdle: true,
 | 
			
		||||
        uiLocalNotificationDateInterpretation:
 | 
			
		||||
            UILocalNotificationDateInterpretation.absoluteTime);
 | 
			
		||||
            UILocalNotificationDateInterpretation.absoluteTime,
 | 
			
		||||
        matchDateTimeComponents: DateTimeComponents.time);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  tz.TZDateTime _convertTimeDeration(int hours, int minutes) {
 | 
			
		||||
    final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
 | 
			
		||||
    print(now);
 | 
			
		||||
    tz.TZDateTime scheduleDate =
 | 
			
		||||
        tz.TZDateTime(tz.local, now.year, now.month, now.day, hours, minutes);
 | 
			
		||||
 | 
			
		||||
    if (scheduleDate.isBefore(now)) {
 | 
			
		||||
      scheduleDate = scheduleDate.add(const Duration(days: 1));
 | 
			
		||||
    }
 | 
			
		||||
    print(scheduleDate);
 | 
			
		||||
    return scheduleDate;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> _configureLocalTimezone() async {
 | 
			
		||||
    tz.initializeTimeZones();
 | 
			
		||||
    final String timeZone = await FlutterNativeTimezone.getLocalTimezone();
 | 
			
		||||
    tz.setLocalLocation(tz.getLocation(timeZone));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future onDidReceiveLocalNotification(
 | 
			
		||||
      int id, String? title, String? body, String? payload) async {
 | 
			
		||||
    // display a dialog with the notification details, tap ok to go to another page
 | 
			
		||||
    // showDialog(
 | 
			
		||||
    //   //context: context,
 | 
			
		||||
    //   builder: (BuildContext context) => CupertinoAlertDialog(
 | 
			
		||||
    //     title: Text(title),
 | 
			
		||||
    //     content: Text(body),
 | 
			
		||||
    //     actions: [
 | 
			
		||||
    //       CupertinoDialogAction(
 | 
			
		||||
    //         isDefaultAction: true,
 | 
			
		||||
    //         child: Text('Ok'),
 | 
			
		||||
    //         onPressed: () async {
 | 
			
		||||
    //           Navigator.of(context, rootNavigator: true).pop();
 | 
			
		||||
    //           await Navigator.push(
 | 
			
		||||
    //             context,
 | 
			
		||||
    //             MaterialPageRoute(
 | 
			
		||||
    //               builder: (context) => SecondScreen(payload),
 | 
			
		||||
    //             ),
 | 
			
		||||
    //           );
 | 
			
		||||
    //         },
 | 
			
		||||
    //       )
 | 
			
		||||
    //     ],
 | 
			
		||||
    //   ),
 | 
			
		||||
    // );
 | 
			
		||||
    Get.dialog(Text("Welcome to Flutter"));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -70,8 +70,6 @@ class _MyHomeScreenState extends State<MyHomeScreen> {
 | 
			
		||||
              body: Get.isDarkMode
 | 
			
		||||
                  ? "Activated Light Mode"
 | 
			
		||||
                  : "Activated Dark Mode");
 | 
			
		||||
 | 
			
		||||
          notifyHelper.scheduledNotification();
 | 
			
		||||
        },
 | 
			
		||||
        child: Icon(
 | 
			
		||||
            Get.isDarkMode ? Icons.wb_sunny_outlined : Icons.nightlight_round,
 | 
			
		||||
@ -136,7 +134,9 @@ class _MyHomeScreenState extends State<MyHomeScreen> {
 | 
			
		||||
        dayTextStyle: datePickerTextStyle,
 | 
			
		||||
        monthTextStyle: datePickerTextStyle,
 | 
			
		||||
        onDateChange: (Date) {
 | 
			
		||||
          setState(() {
 | 
			
		||||
            _selectedDate = Date;
 | 
			
		||||
          });
 | 
			
		||||
        },
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
@ -148,8 +148,30 @@ class _MyHomeScreenState extends State<MyHomeScreen> {
 | 
			
		||||
        return ListView.builder(
 | 
			
		||||
            itemCount: _taskController.taskList.length,
 | 
			
		||||
            itemBuilder: (_, index) {
 | 
			
		||||
              print(
 | 
			
		||||
                  "Task Count :" + _taskController.taskList.length.toString());
 | 
			
		||||
              TaskModel taskModel = _taskController.taskList[index];
 | 
			
		||||
              print(taskModel.toJson());
 | 
			
		||||
              if (taskModel.repeat == "Daily") {
 | 
			
		||||
                DateTime date =
 | 
			
		||||
                    DateFormat.jm().parse(taskModel.startTime.toString());
 | 
			
		||||
                var myTime = DateFormat("HH:mm").format(date);
 | 
			
		||||
                print(myTime);
 | 
			
		||||
                notifyHelper.scheduledNotification(
 | 
			
		||||
                    int.parse(myTime.toString().split(":")[0]),
 | 
			
		||||
                    int.parse(myTime.toString().split(":")[1]),
 | 
			
		||||
                    taskModel);
 | 
			
		||||
                return _setTAskView(index, taskModel);
 | 
			
		||||
              }
 | 
			
		||||
              if (taskModel.date == DateFormat.yMd().format(_selectedDate)) {
 | 
			
		||||
                return _setTAskView(index, taskModel);
 | 
			
		||||
              } else {
 | 
			
		||||
                return Container();
 | 
			
		||||
              }
 | 
			
		||||
            });
 | 
			
		||||
      },
 | 
			
		||||
    ));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  _setTAskView(int index, TaskModel taskModel) {
 | 
			
		||||
    return AnimationConfiguration.staggeredList(
 | 
			
		||||
        position: index,
 | 
			
		||||
        child: SlideAnimation(
 | 
			
		||||
@ -158,17 +180,13 @@ class _MyHomeScreenState extends State<MyHomeScreen> {
 | 
			
		||||
            children: [
 | 
			
		||||
              GestureDetector(
 | 
			
		||||
                onTap: () {
 | 
			
		||||
                            _showBottomSheet(
 | 
			
		||||
                                context, _taskController.taskList[index]);
 | 
			
		||||
                  _showBottomSheet(context, taskModel);
 | 
			
		||||
                },
 | 
			
		||||
                          child: TaskTile(_taskController.taskList[index]),
 | 
			
		||||
                child: TaskTile(taskModel),
 | 
			
		||||
              )
 | 
			
		||||
            ],
 | 
			
		||||
          )),
 | 
			
		||||
        ));
 | 
			
		||||
            });
 | 
			
		||||
      },
 | 
			
		||||
    ));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  _showBottomSheet(BuildContext context, TaskModel taskModel) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user