Skip to content

Commit aff99ec

Browse files
author
gopi2401
committed
feat: enhance notification handling and improve theme service implementation
1 parent 3d944a7 commit aff99ec

File tree

2 files changed

+161
-79
lines changed

2 files changed

+161
-79
lines changed

lib/main.dart

Lines changed: 73 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ final StreamController<ReceivedNotification> didReceiveLocalNotificationStream =
3030
StreamController<ReceivedNotification>.broadcast();
3131

3232
final StreamController<NotificationResponse> selectNotificationStream =
33-
StreamController<NotificationResponse>.broadcast();
33+
StreamController<NotificationResponse>.broadcast();
34+
3435
class ReceivedNotification {
3536
ReceivedNotification({
3637
required this.id,
@@ -49,8 +50,10 @@ String? selectedNotificationPayload;
4950

5051
@pragma('vm:entry-point')
5152
void notificationTapBackground(NotificationResponse notificationResponse) {
52-
print('notification(${notificationResponse.id}) action tapped: '
53-
'${notificationResponse.actionId} with payload: ${notificationResponse.payload}');
53+
print(
54+
'notification(${notificationResponse.id}) action tapped: '
55+
'${notificationResponse.actionId} with payload: ${notificationResponse.payload}',
56+
);
5457
}
5558

5659
void main() async {
@@ -92,7 +95,8 @@ void main() async {
9295
// For Android, request notification permission
9396
await flutterLocalNotificationsPlugin
9497
.resolvePlatformSpecificImplementation<
95-
AndroidFlutterLocalNotificationsPlugin>()
98+
AndroidFlutterLocalNotificationsPlugin
99+
>()
96100
?.requestNotificationsPermission();
97101

98102
// Initialize GetX services
@@ -109,10 +113,7 @@ void main() async {
109113
class MyApp extends StatelessWidget {
110114
final AdaptiveThemeMode? savedThemeMode;
111115

112-
const MyApp({
113-
super.key,
114-
this.savedThemeMode,
115-
});
116+
const MyApp({super.key, this.savedThemeMode});
116117

117118
static final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey =
118119
GlobalKey<ScaffoldMessengerState>();
@@ -122,8 +123,8 @@ class MyApp extends StatelessWidget {
122123
final themeService = ThemeService.to;
123124

124125
return AdaptiveTheme(
125-
light: themeService.getLightTheme(),
126-
dark: themeService.getDarkTheme(),
126+
light: themeService.lightTheme,
127+
dark: themeService.darkTheme,
127128
initial: savedThemeMode ?? AdaptiveThemeMode.light,
128129
builder: (lightTheme, darkTheme) => MaterialApp(
129130
title: 'insta',
@@ -133,8 +134,8 @@ class MyApp extends StatelessWidget {
133134
themeMode: savedThemeMode == null
134135
? ThemeMode.system
135136
: savedThemeMode == AdaptiveThemeMode.dark
136-
? ThemeMode.dark
137-
: ThemeMode.light,
137+
? ThemeMode.dark
138+
: ThemeMode.light,
138139
debugShowCheckedModeBanner: false,
139140
home: const MyHomePage(),
140141
),
@@ -171,35 +172,41 @@ class MyHomePageState extends State<MyHomePage> {
171172
var appVersion = await getAppVersion();
172173
var newVersion = await checkUpdate();
173174
if (appVersion != newVersion) {
174-
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
175-
content: RichText(
176-
text: TextSpan(children: [
177-
TextSpan(
178-
text: 'New version download',
179-
style: TextStyle(
180-
fontSize: 18,
181-
color: Theme.of(context).brightness == Brightness.dark
182-
? Colors.white
183-
: Colors.black,
175+
ScaffoldMessenger.of(context).showSnackBar(
176+
SnackBar(
177+
content: RichText(
178+
text: TextSpan(
179+
children: [
180+
TextSpan(
181+
text: 'New version download',
182+
style: TextStyle(
183+
fontSize: 18,
184+
color: Theme.of(context).brightness == Brightness.dark
185+
? Colors.white
186+
: Colors.black,
187+
),
188+
),
189+
TextSpan(
190+
text: 'Link',
191+
style: const TextStyle(fontSize: 20, color: Colors.blue),
192+
recognizer: TapGestureRecognizer()
193+
..onTap = () {
194+
launchUrl(
195+
Uri.parse('https:gopi2401.github.io/download/'),
196+
);
197+
},
198+
),
199+
],
184200
),
185201
),
186-
TextSpan(
187-
text: 'Link',
188-
style: const TextStyle(fontSize: 20, color: Colors.blue),
189-
recognizer: TapGestureRecognizer()
190-
..onTap = () {
191-
launchUrl(Uri.parse('https:gopi2401.github.io/download/'));
192-
})
193-
])),
194-
));
202+
),
203+
);
195204
}
196205
}
197206

198207
Widget toast = Container(
199208
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
200-
decoration: BoxDecoration(
201-
borderRadius: BorderRadius.circular(25.0),
202-
),
209+
decoration: BoxDecoration(borderRadius: BorderRadius.circular(25.0)),
203210
child: const Row(
204211
mainAxisSize: MainAxisSize.min,
205212
children: [
@@ -245,9 +252,7 @@ class MyHomePageState extends State<MyHomePage> {
245252
padding: const EdgeInsets.all(20.0),
246253
child: TextField(
247254
controller: reelController,
248-
decoration: const InputDecoration(
249-
hintText: "Url",
250-
),
255+
decoration: const InputDecoration(hintText: "Url"),
251256
),
252257
),
253258
if (errorMessage != null)
@@ -260,9 +265,7 @@ class MyHomePageState extends State<MyHomePage> {
260265
),
261266
ElevatedButton(
262267
child: downloading
263-
? const CircularProgressIndicator(
264-
color: Colors.white,
265-
)
268+
? const CircularProgressIndicator(color: Colors.white)
266269
: const Text('Download'),
267270
onPressed: () async {
268271
if (!downloading) {
@@ -335,53 +338,44 @@ class DrawerWidget extends StatelessWidget {
335338
child: Column(
336339
children: [
337340
const DrawerHeader(
338-
duration: Duration(seconds: 1),
339-
curve: Curves.easeInCubic,
340-
decoration: BoxDecoration(
341-
image: DecorationImage(
342-
scale: 2.5, image: AssetImage('assets/logo.png'))),
343-
child: Column(
344-
mainAxisAlignment: MainAxisAlignment.end,
345-
children: [
346-
Text(
347-
'Welcome!',
348-
textAlign: TextAlign.center,
349-
style: TextStyle(fontSize: 33),
350-
)
351-
],
352-
)),
341+
duration: Duration(seconds: 1),
342+
curve: Curves.easeInCubic,
343+
decoration: BoxDecoration(
344+
image: DecorationImage(
345+
scale: 2.5,
346+
image: AssetImage('assets/logo.png'),
347+
),
348+
),
349+
child: Column(
350+
mainAxisAlignment: MainAxisAlignment.end,
351+
children: [
352+
Text(
353+
'Welcome!',
354+
textAlign: TextAlign.center,
355+
style: TextStyle(fontSize: 33),
356+
),
357+
],
358+
),
359+
),
353360
Expanded(
354361
child: ListView(
355362
children: [
356363
ListTile(
357-
leading: const Icon(
358-
Icons.home,
359-
),
360-
title: const Text(
361-
'Home',
362-
style: TextStyle(
363-
fontSize: 18,
364-
),
365-
),
364+
leading: const Icon(Icons.home),
365+
title: const Text('Home', style: TextStyle(fontSize: 18)),
366366
onTap: () {
367367
Navigator.of(context).pop();
368368
},
369369
),
370370
ListTile(
371-
leading: const Icon(
372-
Icons.error_outline,
373-
),
374-
title: const Text(
375-
'About',
376-
style: TextStyle(
377-
fontSize: 18,
378-
),
379-
),
371+
leading: const Icon(Icons.error_outline),
372+
title: const Text('About', style: TextStyle(fontSize: 18)),
380373
onTap: () {
381374
Navigator.push(
382375
context,
383376
MaterialPageRoute(
384-
builder: (context) => const AboutPage()),
377+
builder: (context) => const AboutPage(),
378+
),
385379
);
386380
},
387381
),
@@ -397,7 +391,9 @@ class DrawerWidget extends StatelessWidget {
397391
() => recoveryService.deletedFiles.isNotEmpty
398392
? Container(
399393
padding: const EdgeInsets.symmetric(
400-
horizontal: 8, vertical: 4),
394+
horizontal: 8,
395+
vertical: 4,
396+
),
401397
decoration: BoxDecoration(
402398
color: Colors.orange,
403399
borderRadius: BorderRadius.circular(12),
@@ -428,16 +424,14 @@ class DrawerWidget extends StatelessWidget {
428424
ListTile(
429425
leading: Obx(
430426
() => Icon(
431-
themeService.isDarkMode.value
427+
themeService.isDarkMode
432428
? Icons.light_mode
433429
: Icons.dark_mode,
434430
),
435431
),
436432
title: Obx(
437433
() => Text(
438-
themeService.isDarkMode.value
439-
? 'Light Mode'
440-
: 'Dark Mode',
434+
themeService.isDarkMode ? 'Light Mode' : 'Dark Mode',
441435
style: const TextStyle(fontSize: 18),
442436
),
443437
),

lib/services/theme_service.dart

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import 'package:get/get.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:shared_preferences/shared_preferences.dart';
4+
5+
class ThemeService extends GetxService {
6+
static ThemeService get to => Get.find();
7+
8+
final _isDarkMode = false.obs;
9+
10+
bool get isDarkMode => _isDarkMode.value;
11+
12+
late SharedPreferences _prefs;
13+
14+
@override
15+
Future<ThemeService> onInit() async {
16+
super.onInit();
17+
_prefs = await SharedPreferences.getInstance();
18+
_loadThemePreference();
19+
return this;
20+
}
21+
22+
void _loadThemePreference() {
23+
final savedTheme = _prefs.getBool('isDarkMode') ?? false;
24+
_isDarkMode.value = savedTheme;
25+
}
26+
27+
Future<void> toggleTheme() async {
28+
_isDarkMode.toggle();
29+
await _prefs.setBool('isDarkMode', _isDarkMode.value);
30+
Get.changeTheme(_isDarkMode.value ? darkTheme : lightTheme);
31+
}
32+
33+
ThemeData get lightTheme {
34+
return ThemeData(
35+
useMaterial3: true,
36+
brightness: Brightness.light,
37+
primaryColor: Colors.blue,
38+
primarySwatch: Colors.blue,
39+
scaffoldBackgroundColor: Colors.white,
40+
appBarTheme: const AppBarTheme(
41+
backgroundColor: Colors.blue,
42+
foregroundColor: Colors.white,
43+
elevation: 0,
44+
),
45+
cardTheme: CardThemeData(
46+
color: Colors.white,
47+
elevation: 2,
48+
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
49+
),
50+
textTheme: const TextTheme(
51+
bodyMedium: TextStyle(color: Colors.black87),
52+
bodyLarge: TextStyle(color: Colors.black),
53+
),
54+
iconTheme: const IconThemeData(color: Colors.black87),
55+
floatingActionButtonTheme: const FloatingActionButtonThemeData(
56+
backgroundColor: Colors.blue,
57+
),
58+
);
59+
}
60+
61+
ThemeData get darkTheme {
62+
return ThemeData(
63+
useMaterial3: true,
64+
brightness: Brightness.dark,
65+
primaryColor: Colors.blue,
66+
primarySwatch: Colors.blue,
67+
scaffoldBackgroundColor: const Color(0xFF121212),
68+
appBarTheme: const AppBarTheme(
69+
backgroundColor: Color(0xFF1E1E1E),
70+
foregroundColor: Colors.white,
71+
elevation: 0,
72+
),
73+
cardTheme: CardThemeData(
74+
color: const Color(0xFF1E1E1E),
75+
elevation: 2,
76+
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
77+
),
78+
textTheme: const TextTheme(
79+
bodyMedium: TextStyle(color: Colors.white70),
80+
bodyLarge: TextStyle(color: Colors.white),
81+
),
82+
iconTheme: const IconThemeData(color: Colors.white70),
83+
floatingActionButtonTheme: const FloatingActionButtonThemeData(
84+
backgroundColor: Colors.blue,
85+
),
86+
);
87+
}
88+
}

0 commit comments

Comments
 (0)