skip to Main Content

I’m creating an app in flutter and i added login to it, but while logout the home bar stays there while also showing the new login page.

So below is the code for logout

class profile extends StatelessWidget {
  profile({super.key});
  final _myBox = Hive.openBox("mybox");

  final loggedModel log = loggedModel();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          height:100,
          width: 100,
          child: ElevatedButton(
              onPressed: ()=> {
              log.out = true,
                Navigator.pop(context),
              Navigator.push(context, MaterialPageRoute<void>(
                builder: (BuildContext context) => const Splash(),
              ),)
        },
              child: Text("LOGOUT")
          ),
        ),
      ),
    );
  }
}

so after login the it returns a widget of homescreen which has cupertinotabscaffold, and in that there’s one page of profile containing the logout button. so when i logout the logout page is loaded but the cupertino tab stays there as shown in pics below

this is the main login page
main login page

and this is the profile with logout button
profile after logging in

and here you can see that after logout the tab is still present and im not able to pop it out or remove it
the login after logout

i tried poping the homescreen or closing the app itself after logging out but they are not working

2

Answers


  1. Push to new screen and remove all previous screen

    Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => Splash(),), (route) => false);
    

    Push to new route and remove all previous routes

    Navigator.pushNamedAndRemoveUntil(context, Splash_route_name, (route) => false);
    

    pushAndRemoveUntil will remove all existing routes and push to new page.

    Login or Signup to reply.
  2. It seems like you’re trying to log out from your application by popping the current route from the navigator stack and then pushing a new route. However, the problem you’re encountering is that the previous routes are still visible.

    To resolve this issue, you should navigate to the login page and clear the entire navigation stack when the user logs out. Here’s how you can do
    it:

      final _myBox = Hive.openBox("mybox");
    
      final loggedModel log = loggedModel();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Container(
              height:100,
              width: 100,
              child: ElevatedButton(
                onPressed: () {
                  // Set the logged out flag
                  log.out = true;
                  // Navigate to the login page and clear the stack
                  Navigator.pushAndRemoveUntil(
                    context,
                    MaterialPageRoute<void>(
                      builder: (BuildContext context) => const Splash(),
                    ),
                    (route) => false, // Remove all routes from the stack
                  );
                },
                child: Text("LOGOUT"),
              ),
            ),
          ),
        );
      }
    }
    

    In this modified version, I’ve replaced Navigator.pop(context) with Navigator.pushAndRemoveUntil() method. This method will push the new login page onto the navigation stack and then remove all routes from the stack except for the new login page, effectively clearing the navigation history.

    This way, when the user logs out, they’ll be taken directly to the login page without any previous routes remaining visible.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search