From 7fe141177b9fa8fbc823d0cdd3b1788203814603 Mon Sep 17 00:00:00 2001 From: mdmahib10 Date: Mon, 2 Dec 2024 20:13:07 +0600 Subject: [PATCH] Added an exit dialogue in mainwrapper.dart A confirmation dialogue will pop up before exit. It improves the user experience and saves from accidental exit --- lib/mainwrapper.dart | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/mainwrapper.dart b/lib/mainwrapper.dart index 1663005..e0f3323 100644 --- a/lib/mainwrapper.dart +++ b/lib/mainwrapper.dart @@ -19,16 +19,32 @@ class MainWrapperState extends State { ]; Future _systemBackButtonPressed() async { - if (_navigatorKeys[_selectedIndex].currentState?.canPop() == true) { - _navigatorKeys[_selectedIndex] - .currentState - ?.pop(_navigatorKeys[_selectedIndex].currentContext); - return false; - } else { - SystemChannels.platform.invokeMethod('SystemNavigator.pop'); - return true; // Indicate that the back action is handled - } + if (_navigatorKeys[_selectedIndex].currentState?.canPop() == true) { + _navigatorKeys[_selectedIndex] + .currentState + ?.pop(_navigatorKeys[_selectedIndex].currentContext); + return false; + } else { + return await showDialog( + context: context, + builder: (context) => AlertDialog( + title: Text('Exit App'), + content: Text('Do you really want to exit the app?'), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(false), + child: Text('No'), + ), + TextButton( + onPressed: () => SystemChannels.platform.invokeMethod('SystemNavigator.pop'), + child: Text('Yes'), + ), + ], + ), + ) ?? false; } +} + @override Widget build(BuildContext context) {