Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

close app on device back button in flutter

The question may be duplicate but I'm unable to found the solution. My app scenario is the same as the almost app. My first screen is Splash screen and holds for two seconds and here login session is checked and upon this condition screen changes like this and below code is run in initState()

 _checkPreference() async {
    PreferencesConnector myprefs= PreferencesConnector();
    id=await myprefs.readString('merchantid');
    if(id==null||id==''){
      Future.delayed(
          const Duration(seconds: 2),
              () => Navigator.pushReplacement(
            context,
            MaterialPageRoute(builder: (context) => Login(),settings: RouteSettings(name: '/login')),
          ));

    }else{
      Future.delayed(
          const Duration(seconds: 2),
              () => Navigator.pushReplacement(
            context,
            MaterialPageRoute(builder: (context) => DashBoard()),
          ));

    }
  }

If the session returns false then it goes to login screen, In Login screen my scaffold inside in WillPopScope widget and that class is stateful class

  return WillPopScope(
        onWillPop: () {

         if (Navigator.canPop(context)) {
            //Navigator.pop(context);
            Navigator.of(context).pop();
          } else {
            SystemNavigator.pop();
          }
        },
        child:Scaffold(
        body: Stack(
          children: <Widget>[

and if LoginApi returns true then it move to dashboard like this

Navigator.pushReplacement(
                context,
                MaterialPageRoute(
                    builder: (context) => DashBoard(),
                    settings: RouteSettings(name: '/dashboard')),
              );

here everything is working fine when the user is already logged-in and we reach to dashboard after the splash and but there is a logOut button on my dashboard when user press logout then there is a dialog appear which asks for logout- if i press yes from dialog button works like this

onPressed:(){

    clearSession();
   // Navigator.of(context).popUntil(ModalRoute.());
   // Navigator.of(context).popUntil('/login', (Route<dynamic> route) => false);
   // Navigator.popUntil(context, ModalRoute.withName("/login"));
   // Navigator.of(context).pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);

      Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login()),);

},

after pressing logout it reaches to login screen where I'm unable to close the app when user press back from the device back in login screen but it redirects to the dashboard and then I pressed back then app closed.

enter image description here

like image 933
Farhana Naaz Ansari Avatar asked Feb 22 '19 12:02

Farhana Naaz Ansari


2 Answers

What you need to do is first clear all path before going to login() screen.

try this:

onPressed:(){
    clearSession();

      //Navigator.popUntil(context, ModalRoute.withName('/'));
      Navigator.pop(context,true);// It worked for me instead of above line
      Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login()),);

},
like image 133
anmol.majhail Avatar answered Sep 20 '22 10:09

anmol.majhail


call this on your logout button

  void _logout() {
      Navigator.popUntil(context, ModalRoute.withName('/login'));
    }

here is the link of official docs

like image 21
End User Avatar answered Sep 18 '22 10:09

End User