I have a main screen with a custom bottom navigation bar, when an icon is clicked it sets the current index to the icon that was clicked. But when index number 2 is clicked it does a full page route to another page to add a "post". After the post is submitted a pushReplacement happens back to the main screen (to prevent going back). But after it routes to the main screen to the default screen at index 0, a back button still shows at the top. It goes away after I click it, but the data on that screen is slightly different. How can I fix this?
Part of the code on my main screen:
Widget customNav() {
return Container(
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
//some icons before
IconButton(
icon: Icon(Icons.add_circle),
onPressed: () => Navigator.push(
context,
FadeRoute(
builder: (_) => BlocProvider<AppBloc>(
builder: (_, bloc) => bloc ?? AppBloc(),
onDispose: (_, bloc) => bloc.dispose(),
child: AddPost(userBloc: widget.userBloc)))),
),
//icons after
],
));
}
Add Post page:
_submitFunc() async {
// sending it to backend
if (everything is good) {
Navigator.pushReplacement(
context,
FadeRoute(
builder: (_) =>
BlocProvider<AppBloc>(
builder: (_, bloc) => bloc ?? AppBloc(),
onDispose: (_, bloc) => bloc.dispose(),
child: MainScreen(userBloc: widget.userBloc))),
);
}
Seems like it might be a routing issue. Navigator.pushReplacement()
replaces whatever you've got at that point in the route, so you're essentially setting up a route that goes:
start: MainScreen
push: MainScreen -> AddScreen
pushReplacement: MainScreen (A) -> MainScreen (B)
This is why the back button goes away after you tap it, it takes you from MainScreen B (which has historical context to go back to) to MainScreen A (which doesn't).
You may want to use Navigator.pop()
to return to the original MainScreen instead of replacing the current view with a new one, or use
Navigator.pushAndRemoveUntil(newRoute, (route)=>false);
to clear all Navigator route history if you want to send the user to a new screen without being able to go back at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With