Flutter how to remove bottom navigation with navigation

后端 未结 2 1591
生来不讨喜
生来不讨喜 2020-12-21 10:49

I want to remove all stacked routes and go back to the Auth page. My home page is like this.

class _HomeScreenState extends State {
               


        
相关标签:
2条回答
  • 2020-12-21 11:41

    The code

    Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => new AtuhScreen()));
    

    will be fixed by:

    Navigator.of(context, rootNavigator: true).pushReplacement(MaterialPageRoute(builder: (context) => new AtuhScreen()));
    

    The rootNavigator: true will get the highest root widget Scaffold or MaterialApp and avoid displaying the BottomNavigationBar.

    0 讨论(0)
  • 2020-12-21 11:45

    As You are using FireBase_auth: Then you can Use StreamBuilder to handle the Sign & Sign out Process. E.g:

    final FirebaseAuth _auth = FirebaseAuth.instance;
    void main() {
      runApp(MaterialApp(
        //home: MyLoginPage(),
        home: StreamBuilder(
            stream: _auth.onAuthStateChanged,
            builder: (context, snap) {
              if (snap.connectionState == ConnectionState.waiting) {
                return SplashScreen();
              } else {
                if (snap.hasData) {
                  return HomeScreen(
                    user: snap.data,
                    signOut: signOut,
                  );
                }
                return MyLoginPage();
              }
            }),
        theme: ThemeData(
            primarySwatch: Colors.blue,
            accentColor: Colors.blue),
      ));
    }
    
    void signOut() async {
      await _auth.signOut();
    }
    

    Then you can simply Call SignOut() from any of Page Screen & you Will get a new Screen As here in example is MyLoginPage(). Like:

    class Page3 extends StatelessWidget {
      final Function signOut;
    
      Page3(this.signOut);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Third'),
          ),
          body: Center(child: RaisedButton(onPressed: ()=> signOut,child: Text("Sign-Out"),),),
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题