Search code examples
android-studioflutterlogoutbottomnavigationview

Flutter re-login it does not directs to the intended home page


Flutter Logout directs to login page but after re-login it does not redirects to the intended home page.

I have used the below navigation for login

Navigator.of(context).pushReplacement(PageRouteBuilder(pageBuilder: (_, __, ___) => Home()));

My app is consist of three bottom navigation bar.Home Page is in second navigation bar. I have to logout from third navigation bar.

I have used the below code for logout

Navigator.pushNamedAndRemoveUntil(context, '/Login', (_) => false);

Also i have tried the codes too

 Navigator.popAndPushNamed(context, "/Login");


Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => Login(),), (Route route) => false);

Please help me to find a solution.


Solution

  • Try to use shared preference to store as login key and check if it is true then go to main screen or else redirect to login screen.

    First of all add shared preference dependency in your pubspec.yaml

    dependencies:
      flutter:
        sdk: flutter
    
      shared_preferences: ^0.5.6+2
    

    SplashScreen.dart

    This is first screen where we are check that if isLogin is true then go to HomeScreen or else redirect to LoginScreen

    _autoLogin() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      if (!prefs.containsKey('isLogin')) {
        prefs.setBool('isLogin', false);
      }
      if (prefs.getBool('isLogin')) {
        Navigator.pushReplacement(
            context,
            MaterialPageRoute(
              builder: (context) => HomeScreen(),
            ));
      } else {
        Navigator.pushReplacement(
            context,
            MaterialPageRoute(
              builder: (context) => LoginScreen(),
            ));;
      }
    }
    

    LoginScreen.dart

    _setIsLogin() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setBool('isLogin', true);
      Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => HomeScreen(),
          ));
    }
    
    
    MaterialButton(
      minWidth: MediaQuery.of(context).size.width,
      padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
      onPressed: () {
        _setIsLogin();
      },
      child: Text("LOGIN",
          textAlign: TextAlign.center,
          style: style.copyWith(
              color: Colors.white,
              fontWeight: FontWeight.bold,
              fontSize: 18.0)),
    ),
    

    HomeScreen.dart

    Set isLogin as false when click on Logout

    _logout() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setBool('isLogin', false);
      Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => LoginScreen(),
          ));
    }
    

    I hope this can help you!