场景
我正在导航用户从应用程序中的不同屏幕(例如destinationScreen,attractionScreens,reviewScreen等)登录到LoginScreen / signUpScreen,并成功登录后,我想将他从导航到loginScreen /的位置弹出回到同一屏幕signupScreen。
因此,我要弹出注册/登录过程中涉及的所有屏幕,例如 3个屏幕。
我已经尝试过的内容
我已经尝试过 navigator.popUntil ,但是只有在您希望每次这样都弹出回到单个屏幕时,它才能起作用:
推路线:
Navigator.push(
context,
MaterialPageRoute(
settings: RouteSettings(name: '/loginRedirect'),
builder: (context) => AttractionScreen(
attractionData: att,
)),
);
在signInScreen中弹出路线:
Navigator.popUntil(context, ModalRoute.withName('/loginRedirect'));
但是这种情况仅在您必须路由到单个特定屏幕时才有效,但就我而言,我必须根据将用户定向到登录屏幕的位置来路由到其他屏幕。
我现在想做什么
现在,我想拥有一个实现,在该实现中,成功登录后,我从导航器堆栈中弹出前三个屏幕,我在其中进行了大量搜索,但找不到任何解决方案。 那么,如何从导航器堆栈中弹出最后一条路线?
答案 0 :(得分:2)
我刚刚得到了一个不确定的解决方案。
我只需要打三遍电话:
Navigator.pop(context)
像这样:
if(needBackRedirect){
Navigator.pop(context);
Navigator.pop(context);
Navigator.pop(context);
}
答案 1 :(得分:2)
如果要在导航堆栈中弹出3个屏幕,请选择一个变量并跟踪已删除的路线。然后检查该计数是否达到3,然后停止弹出路由。
List<Route> arrayRoutes = [];
Navigator.popUntil(context, (Route<dynamic> route){
arrayRoutes.add(route);
if (arrayRoutes.length != 3) {
print('continue popping if length is not 3');
return true;
} else {
print('stop to pop when count is 3');
return false;
}
});