Instead of using the method push
, that stack a new screen to your list of screens in the application, try using the pushAndRemoveUntil:
IconButton(
icon: Icon(Icons.exit_to_app),
onPressed: (){
userModel.signOut();
navigator.pushAndRemoveUntil(
MaterialPageRoute(builder: (context)=>LoginScreen()),
(Route<dynamic> route) => false,
);
},
),
This method will remove from the stack (do the pop
) from all screens until the second argument returns true. As I put a function that always returns false, it will remove all, to finally bring the next screen.
If you are sure that only one screen existed on your stack, you can also use as suggested in the comments the function pushReplacement
. As the name implies, she does the push
replacing the position of the current screen:
IconButton(
icon: Icon(Icons.exit_to_app),
onPressed: (){
userModel.signOut();
navigator.pushReplacement(
MaterialPageRoute(builder: (context)=>LoginScreen()));
},
),
Take a look here and see if it solves your problem https://stackoverflow.com/questions/44978216/flutter-remove-back-button-appbar#:~:text=9%20Answers&text=You%20can%20remove%20back,back%20to%20the%20%20route earlier.
– user212376