Skip to content

Commit 03a2282

Browse files
committed
notify user when press back button for exit
1 parent 06fcb87 commit 03a2282

File tree

2 files changed

+82
-54
lines changed

2 files changed

+82
-54
lines changed

lib/Components/toast_component.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,14 @@ class Toasts {
2121
textColor: Colors.white,
2222
fontSize: 16.0);
2323
}
24+
25+
static void showExitWarningToast({required String msg}) {
26+
Fluttertoast.showToast(
27+
msg: msg,
28+
toastLength: Toast.LENGTH_LONG,
29+
gravity: ToastGravity.BOTTOM,
30+
backgroundColor: Colors.green.withAlpha(130),
31+
textColor: Colors.white,
32+
fontSize: 16);
33+
}
2434
}

lib/Pages/home_screen.dart

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flood_mobile/Components/add_automatic_torrent.dart';
99
import 'package:flood_mobile/Components/logout_alert.dart';
1010
import 'package:flood_mobile/Components/nav_drawer_list_tile.dart';
1111
import 'package:flood_mobile/Components/notification_popup_dialogue_container.dart';
12+
import 'package:flood_mobile/Components/toast_component.dart';
1213
import 'package:flood_mobile/Constants/theme_provider.dart';
1314
import 'package:flood_mobile/Pages/about_screen.dart';
1415
import 'package:flood_mobile/Pages/settings_screen.dart';
@@ -45,6 +46,7 @@ class _HomeScreenState extends State<HomeScreen> {
4546
File? _file;
4647
late String base64;
4748
late String directoryDefault;
49+
DateTime timeBackPressed = DateTime.now();
4850

4951
@override
5052
void initState() {
@@ -110,6 +112,19 @@ class _HomeScreenState extends State<HomeScreen> {
110112
}
111113
}
112114

115+
Future<bool> onBackPressed() async {
116+
final differnce = DateTime.now().difference(timeBackPressed);
117+
final isExitWarning = differnce >= Duration(seconds: 2);
118+
timeBackPressed = DateTime.now();
119+
120+
if (isExitWarning) {
121+
Toasts.showExitWarningToast(msg: 'Press back button again to exit');
122+
return false;
123+
} else {
124+
return true;
125+
}
126+
}
127+
113128
@override
114129
Widget build(BuildContext context) {
115130
double wp = MediaQuery.of(context).size.width;
@@ -137,66 +152,69 @@ class _HomeScreenState extends State<HomeScreen> {
137152
break;
138153
}
139154
return Consumer<HomeProvider>(builder: (context, homeModel, child) {
140-
return Scaffold(
141-
appBar: AppBar(
142-
leading: IconButton(
143-
icon: Icon(
144-
Icons.menu,
145-
color: ThemeProvider.theme.textTheme.bodyText1?.color,
155+
return WillPopScope(
156+
onWillPop: onBackPressed,
157+
child: Scaffold(
158+
appBar: AppBar(
159+
leading: IconButton(
160+
icon: Icon(
161+
Icons.menu,
162+
color: ThemeProvider.theme.textTheme.bodyText1?.color,
163+
),
164+
onPressed: () {
165+
controller.toggle();
166+
},
146167
),
147-
onPressed: () {
148-
controller.toggle();
149-
},
150-
),
151-
title: Image(
152-
key: Key('Flood Icon'),
153-
image: AssetImage(
154-
'assets/images/icon.png',
168+
title: Image(
169+
key: Key('Flood Icon'),
170+
image: AssetImage(
171+
'assets/images/icon.png',
172+
),
173+
width: 60,
174+
height: 60,
155175
),
156-
width: 60,
157-
height: 60,
158-
),
159-
centerTitle: true,
160-
backgroundColor: Theme.of(context).primaryColor,
161-
elevation: 0,
162-
actions: [
163-
RSSFeedButtonWidget(),
164-
Badge(
165-
showBadge:
166-
homeModel.unreadNotifications == 0 ? false : true,
167-
key: Key('Badge Widget'),
168-
badgeColor: Theme.of(context).accentColor,
169-
badgeContent: Center(
170-
child: Text(
171-
homeModel.unreadNotifications.toString(),
172-
style: TextStyle(color: Colors.white),
176+
centerTitle: true,
177+
backgroundColor: Theme.of(context).primaryColor,
178+
elevation: 0,
179+
actions: [
180+
RSSFeedButtonWidget(),
181+
Badge(
182+
showBadge:
183+
homeModel.unreadNotifications == 0 ? false : true,
184+
key: Key('Badge Widget'),
185+
badgeColor: Theme.of(context).colorScheme.secondary,
186+
badgeContent: Center(
187+
child: Text(
188+
homeModel.unreadNotifications.toString(),
189+
style: TextStyle(color: Colors.white),
190+
),
173191
),
174-
),
175-
position: BadgePosition(top: 0, end: 3),
176-
child: IconButton(
177-
icon: Icon(
178-
Icons.notifications,
192+
position: BadgePosition(top: 0, end: 3),
193+
child: IconButton(
194+
icon: Icon(
195+
Icons.notifications,
196+
),
197+
onPressed: () {
198+
showDialog(
199+
context: context,
200+
builder: (BuildContext context) {
201+
return AlertDialog(
202+
key: Key('Notification Alert Dialog'),
203+
elevation: 0,
204+
backgroundColor: Theme.of(context).primaryColor,
205+
content: notificationPopupDialogueContainer(
206+
context: context,
207+
),
208+
);
209+
},
210+
);
211+
},
179212
),
180-
onPressed: () {
181-
showDialog(
182-
context: context,
183-
builder: (BuildContext context) {
184-
return AlertDialog(
185-
key: Key('Notification Alert Dialog'),
186-
elevation: 0,
187-
backgroundColor: Theme.of(context).primaryColor,
188-
content: notificationPopupDialogueContainer(
189-
context: context,
190-
),
191-
);
192-
},
193-
);
194-
},
195213
),
196-
),
197-
],
214+
],
215+
),
216+
body: screenCurrent,
198217
),
199-
body: screenCurrent,
200218
);
201219
});
202220
},

0 commit comments

Comments
 (0)