I currently try to learn doing the flutter scheduled local notification. I selecting the time from DatePicker.showDateTimePicker to alert and pop up the notification however when the time is reached nothing is happening, anyone know what is happening, another thing is it seems unable to recognize the import 'package:timezone/data/latest.dart' as tz;and mark is as unused import. I need help, thank you.
#Main.dart
import 'package:flutter/material.dart';
import 'package:success_auto/views/home.dart';
import 'package:timezone/data/latest.dart' as tz;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
NotificationWidget.init();
tz.initializeTimeZones();
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
static GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
@override
Widget build(BuildContext context) {
const secondaryColor = Color(0xff6D28D9);
return MaterialApp(
debugShowCheckedModeBanner: false,
navigatorKey: navigatorKey,
title: 'Awesome Notification Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
shadowColor: secondaryColor,
),
home: const MyHomePage(title: 'Notification',),
);
}
}
#MyHomePage.dart
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker_plus/flutter_datetime_picker_plus.dart';
import 'package:success_auto/notify_setting.dart';
DateTime scheduleTime = DateTime.now();
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DatePickerTxt(),
ScheduleBtn(),
],
),
),
);
}
}
class DatePickerTxt extends StatefulWidget {
const DatePickerTxt({
super.key,
});
@override
State<DatePickerTxt> createState() => _DatePickerTxtState();
}
class _DatePickerTxtState extends State<DatePickerTxt> {
@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () {
DatePicker.showDateTimePicker(
context,
showTitleActions: true,
onChanged: (date) => scheduleTime = date,
onConfirm: (date) {},
);
},
child: const Text(
'Select Date Time',
style: TextStyle(color: Colors.blue),
),
);
}
}
class ScheduleBtn extends StatelessWidget {
const ScheduleBtn({
super.key,
});
@override
Widget build(BuildContext context) {
return ElevatedButton(
child: const Text('Schedule notifications'),
onPressed: () {
debugPrint('Notification Scheduled for $scheduleTime');
NotificationWidget().scheduleNotification(
title: 'Scheduled Notification',
body: '$scheduleTime',
scheduledNotificationDateTime: scheduleTime);
},
);
}
}
#NotificationSetting.dart
// ignore_for_file: deprecated_member_use
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
class NotificationWidget {
static final _notification = FlutterLocalNotificationsPlugin();
static Future init({bool scheduled = false}) async {
var initAndroidSettings =
const AndroidInitializationSettings('mipmap/ic_launcher');
var ios = const DarwinInitializationSettings();
final settings =
InitializationSettings(android: initAndroidSettings, iOS: ios);
await _notification.initialize(settings);
}
static Future showNotification(
{var id = 0, var title, var body, var payload}) async =>
_notification.show(id, title, body, await notificationDetails());
static notificationDetails() {
return const NotificationDetails(
android: AndroidNotificationDetails('channel id', 'channel name',
importance: Importance.max),
);
}
Future scheduleNotification(
{int id = 0,
String? title,
String? body,
String? payload,
required DateTime scheduledNotificationDateTime}) async {
return _notification.zonedSchedule(
id,
title,
body,
tz.TZDateTime.from(scheduledNotificationDateTime, tz.local),
await notificationDetails(),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
}
}