I want to remove all stacked routes and go back to the Auth page. My home page is like this.
class _HomeScreenState extends State {
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
.
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"),),),
);
}
}