dart - pushReplacement 显示后退箭头颤动
问题描述
我有一个带有自定义底部导航栏的主屏幕,当单击图标时,它将当前索引设置为单击的图标。但是,当单击索引号 2 时,它会执行整页路由到另一个页面以添加“帖子”。提交帖子后,pushReplacement 会返回到主屏幕(以防止返回)。但是在它路由到主屏幕到索引 0 处的默认屏幕后,顶部仍然显示一个后退按钮。单击它后它消失了,但是该屏幕上的数据略有不同。我怎样才能解决这个问题?
我的主屏幕上的部分代码:
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
],
));
}
添加帖子页面:
_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))),
);
}
解决方案
似乎这可能是一个路由问题。Navigator.pushReplacement()
替换您在该路线中所拥有的任何东西,因此您实际上是在设置一条路线:
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.