Skip to content

Commit

Permalink
notify user when press back button for exit
Browse files Browse the repository at this point in the history
  • Loading branch information
amitamrutiya committed Jan 15, 2023
1 parent 06fcb87 commit 03a2282
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 54 deletions.
10 changes: 10 additions & 0 deletions lib/Components/toast_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 21,14 @@ class Toasts {
textColor: Colors.white,
fontSize: 16.0);
}

static void showExitWarningToast({required String msg}) {
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.green.withAlpha(130),
textColor: Colors.white,
fontSize: 16);
}
}
126 changes: 72 additions & 54 deletions lib/Pages/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 9,7 @@ import 'package:flood_mobile/Components/add_automatic_torrent.dart';
import 'package:flood_mobile/Components/logout_alert.dart';
import 'package:flood_mobile/Components/nav_drawer_list_tile.dart';
import 'package:flood_mobile/Components/notification_popup_dialogue_container.dart';
import 'package:flood_mobile/Components/toast_component.dart';
import 'package:flood_mobile/Constants/theme_provider.dart';
import 'package:flood_mobile/Pages/about_screen.dart';
import 'package:flood_mobile/Pages/settings_screen.dart';
Expand Down Expand Up @@ -45,6 46,7 @@ class _HomeScreenState extends State<HomeScreen> {
File? _file;
late String base64;
late String directoryDefault;
DateTime timeBackPressed = DateTime.now();

@override
void initState() {
Expand Down Expand Up @@ -110,6 112,19 @@ class _HomeScreenState extends State<HomeScreen> {
}
}

Future<bool> onBackPressed() async {
final differnce = DateTime.now().difference(timeBackPressed);
final isExitWarning = differnce >= Duration(seconds: 2);
timeBackPressed = DateTime.now();

if (isExitWarning) {
Toasts.showExitWarningToast(msg: 'Press back button again to exit');
return false;
} else {
return true;
}
}

@override
Widget build(BuildContext context) {
double wp = MediaQuery.of(context).size.width;
Expand Down Expand Up @@ -137,66 152,69 @@ class _HomeScreenState extends State<HomeScreen> {
break;
}
return Consumer<HomeProvider>(builder: (context, homeModel, child) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(
Icons.menu,
color: ThemeProvider.theme.textTheme.bodyText1?.color,
return WillPopScope(
onWillPop: onBackPressed,
child: Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(
Icons.menu,
color: ThemeProvider.theme.textTheme.bodyText1?.color,
),
onPressed: () {
controller.toggle();
},
),
onPressed: () {
controller.toggle();
},
),
title: Image(
key: Key('Flood Icon'),
image: AssetImage(
'assets/images/icon.png',
title: Image(
key: Key('Flood Icon'),
image: AssetImage(
'assets/images/icon.png',
),
width: 60,
height: 60,
),
width: 60,
height: 60,
),
centerTitle: true,
backgroundColor: Theme.of(context).primaryColor,
elevation: 0,
actions: [
RSSFeedButtonWidget(),
Badge(
showBadge:
homeModel.unreadNotifications == 0 ? false : true,
key: Key('Badge Widget'),
badgeColor: Theme.of(context).accentColor,
badgeContent: Center(
child: Text(
homeModel.unreadNotifications.toString(),
style: TextStyle(color: Colors.white),
centerTitle: true,
backgroundColor: Theme.of(context).primaryColor,
elevation: 0,
actions: [
RSSFeedButtonWidget(),
Badge(
showBadge:
homeModel.unreadNotifications == 0 ? false : true,
key: Key('Badge Widget'),
badgeColor: Theme.of(context).colorScheme.secondary,
badgeContent: Center(
child: Text(
homeModel.unreadNotifications.toString(),
style: TextStyle(color: Colors.white),
),
),
),
position: BadgePosition(top: 0, end: 3),
child: IconButton(
icon: Icon(
Icons.notifications,
position: BadgePosition(top: 0, end: 3),
child: IconButton(
icon: Icon(
Icons.notifications,
),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
key: Key('Notification Alert Dialog'),
elevation: 0,
backgroundColor: Theme.of(context).primaryColor,
content: notificationPopupDialogueContainer(
context: context,
),
);
},
);
},
),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
key: Key('Notification Alert Dialog'),
elevation: 0,
backgroundColor: Theme.of(context).primaryColor,
content: notificationPopupDialogueContainer(
context: context,
),
);
},
);
},
),
),
],
],
),
body: screenCurrent,
),
body: screenCurrent,
);
});
},
Expand Down

0 comments on commit 03a2282

Please sign in to comment.