-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
204 lines (186 loc) · 5.62 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:task_manager/provider/task_provider.dart';
import 'package:task_manager/provider/theme_provider.dart';
import 'package:task_manager/screens/homepage.dart';
import 'package:task_manager/theme/dark_theme.dart';
import 'notification_services/local_notification_service.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
late Directory dir;
late File file;
late SharedPreferences sp;
final imagePicker = ImagePicker();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
dir = await getApplicationDocumentsDirectory();
file = File("${dir.path}/userImage1");
sp = await SharedPreferences.getInstance();
await Future.wait([
LocalNotificationService.init(),
]);
runApp(
ChangeNotifierProvider(
create: (context) => ThemeProvider(),
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [ChangeNotifierProvider(create: (context) => TaskProvider())],
child: MaterialApp(
theme: Provider.of<ThemeProvider>(context).themeData,
darkTheme: dartTheme,
debugShowCheckedModeBanner: false,
home: const HomePage(),
),
);
}
}
/*
*
* import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_local_notification/local_notification_service.dart';
import 'package:flutter_local_notification/notification_details.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Future.wait([
LocalNotificationService.init(),
WorkManagerService().init(),
]);
// await LocalNotificationService.init(); //2
// await WorkManagerService().init(); //4
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Local Notification Tutorial',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.amberAccent),
// useMaterial3: true,
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
void initState() {
super.initState();
listenToNotificationStream();
}
void listenToNotificationStream() {
LocalNotificationService.streamController.stream.listen(
(notificationResponse) {
log(notificationResponse.id!.toString());
log(notificationResponse.payload!.toString());
//logic to get product from database.
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => NotificationDetailsScreen(
response: notificationResponse,
),
),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.amberAccent,
leading: const Icon(Icons.notifications),
titleSpacing: 0.0,
title: const Text('Flutter Local Notification Tutorial'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//basic
ListTile(
onTap: () {
LocalNotificationService.showBasicNotification();
},
leading: const Icon(Icons.notifications),
title: const Text('Basic Notification'),
subtitle: const Text('with custom sound'),
trailing: IconButton(
onPressed: () {
LocalNotificationService.cancelNotification(0);
},
icon: const Icon(
Icons.cancel,
color: Colors.red,
),
),
),
//repeated
ListTile(
onTap: () {
LocalNotificationService.showRepeatedNotification();
},
leading: const Icon(Icons.notifications),
title: const Text('Repeated Notification'),
subtitle: const Text('every minute'),
trailing: IconButton(
onPressed: () {
LocalNotificationService.cancelNotification(1);
},
icon: const Icon(
Icons.cancel,
color: Colors.red,
),
),
),
//Schduled
ListTile(
onTap: () {
LocalNotificationService.showSchduledNotification();
},
leading: const Icon(Icons.notifications),
title: const Text('Schduled Notification'),
subtitle: const Text('after 10 seconds from now'),
trailing: IconButton(
onPressed: () {
LocalNotificationService.cancelNotification(2);
},
icon: const Icon(
Icons.cancel,
color: Colors.red,
),
),
),
//cancel All
ElevatedButton(
onPressed: () {
LocalNotificationService.flutterLocalNotificationsPlugin
.cancelAll();
},
child: const Text('Cancel All'),
)
],
)),
);
}
}
* */