Skip to content

Commit d9b4272

Browse files
authored
Merge pull request #153 from amitamrutiya2210/conformation-on-exit
notify user when press back button for exit
2 parents 2f783c2 + 47d9239 commit d9b4272

File tree

2 files changed

+84
-54
lines changed

2 files changed

+84
-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: 74 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';
@@ -20,6 +21,7 @@ import 'package:flood_mobile/Route/routes.dart';
2021
import 'package:flutter/material.dart';
2122
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
2223
import 'package:flutter_svg/svg.dart';
24+
import 'package:fluttertoast/fluttertoast.dart';
2325
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
2426
import 'package:hidden_drawer_menu/controllers/simple_hidden_drawer_controller.dart';
2527
import 'package:hidden_drawer_menu/simple_hidden_drawer/simple_hidden_drawer.dart';
@@ -45,6 +47,7 @@ class _HomeScreenState extends State<HomeScreen> {
4547
File? _file;
4648
late String base64;
4749
late String directoryDefault;
50+
DateTime timeBackPressed = DateTime.now();
4851

4952
@override
5053
void initState() {
@@ -110,6 +113,20 @@ class _HomeScreenState extends State<HomeScreen> {
110113
}
111114
}
112115

116+
Future<bool> onBackPressed() async {
117+
final differnce = DateTime.now().difference(timeBackPressed);
118+
final isExitWarning = differnce >= Duration(seconds: 2);
119+
timeBackPressed = DateTime.now();
120+
121+
if (isExitWarning) {
122+
Toasts.showExitWarningToast(msg: 'Press back button again to exit');
123+
return false;
124+
} else {
125+
Fluttertoast.cancel();
126+
return true;
127+
}
128+
}
129+
113130
@override
114131
Widget build(BuildContext context) {
115132
double wp = MediaQuery.of(context).size.width;
@@ -137,66 +154,69 @@ class _HomeScreenState extends State<HomeScreen> {
137154
break;
138155
}
139156
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,
157+
return WillPopScope(
158+
onWillPop: onBackPressed,
159+
child: Scaffold(
160+
appBar: AppBar(
161+
leading: IconButton(
162+
icon: Icon(
163+
Icons.menu,
164+
color: ThemeProvider.theme.textTheme.bodyText1?.color,
165+
),
166+
onPressed: () {
167+
controller.toggle();
168+
},
146169
),
147-
onPressed: () {
148-
controller.toggle();
149-
},
150-
),
151-
title: Image(
152-
key: Key('Flood Icon'),
153-
image: AssetImage(
154-
'assets/images/icon.png',
170+
title: Image(
171+
key: Key('Flood Icon'),
172+
image: AssetImage(
173+
'assets/images/icon.png',
174+
),
175+
width: 60,
176+
height: 60,
155177
),
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),
178+
centerTitle: true,
179+
backgroundColor: Theme.of(context).primaryColor,
180+
elevation: 0,
181+
actions: [
182+
RSSFeedButtonWidget(),
183+
Badge(
184+
showBadge:
185+
homeModel.unreadNotifications == 0 ? false : true,
186+
key: Key('Badge Widget'),
187+
badgeColor: Theme.of(context).colorScheme.secondary,
188+
badgeContent: Center(
189+
child: Text(
190+
homeModel.unreadNotifications.toString(),
191+
style: TextStyle(color: Colors.white),
192+
),
173193
),
174-
),
175-
position: BadgePosition(top: 0, end: 3),
176-
child: IconButton(
177-
icon: Icon(
178-
Icons.notifications,
194+
position: BadgePosition(top: 0, end: 3),
195+
child: IconButton(
196+
icon: Icon(
197+
Icons.notifications,
198+
),
199+
onPressed: () {
200+
showDialog(
201+
context: context,
202+
builder: (BuildContext context) {
203+
return AlertDialog(
204+
key: Key('Notification Alert Dialog'),
205+
elevation: 0,
206+
backgroundColor: Theme.of(context).primaryColor,
207+
content: notificationPopupDialogueContainer(
208+
context: context,
209+
),
210+
);
211+
},
212+
);
213+
},
179214
),
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-
},
195215
),
196-
),
197-
],
216+
],
217+
),
218+
body: screenCurrent,
198219
),
199-
body: screenCurrent,
200220
);
201221
});
202222
},

0 commit comments

Comments
 (0)