我在我的应用程序中创建了一个注册部分,并尝试执行以下操作:
”。
的任何阶段都按后退按钮。
。
用户按Yes键
然后将用户带回到应用程序的第一个(主要)页面.
。
这对我点击注册后的第一页有效,但是当我到达第二页时,步骤4将我保持在同一个页面上,然后当我再次尝试它时,它就能工作了。但情况不应该是这样。
经过一些调试,我发现了这个问题,但我不知道为什么会发生这种情况。
我在onBackPressed()函数末尾的第二个代码片段中作为注释编写了这个问题,这样就可以清楚地知道它在哪里中断:)。
下面是我从main.dart导航到注册过程页面的代码
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SignUp())
)然后,对于注册过程中的每一页,每当我进入下一页时,我都会执行Navigation.pop(context) to pop当前的注册页面从堆栈中移出,然后立即按下一页。
以下是每个注册页面中back按钮功能的代码:
bool backIsPressed = false;
Tools _tools = new Tools();
@override
void initState() {
super.initState();
BackButtonInterceptor.add(onBackPressed);
}
@override
void dispose() {
BackButtonInterceptor.remove(onBackPressed);
super.dispose();
}
bool onBackPressed(bool stopDefaultButtonEvent) {
this.backIsPressed = !backIsPressed;
if(backIsPressed) {
_tools.yesNoAlert(
context,
"Going Back?",
"Are you sure you want back? Changes made will not be saved.",
() {
this.backIsPressed = false;
Navigator.pop(context, true);
},
() {
this.backIsPressed = false;
Navigator.pop(context, false);
},
).then((res) {
// ---------------- BREAKS HERE -----------------
// "res" returns null the first time YES is pressed
// But Navigation.pop(context, true) should return true according to Flutter's docs
if(res) {
Navigator.pop(context);
}
});
}
else {
Navigator.pop(context);
}
return true;
}最后,yesOrNoAlert()函数在Tools类中。
Future<bool> yesNoAlert(BuildContext context, String title,
String description, Function yesFunction, Function noFunction) {
this._isDialogOpen = true;
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: new Text(title),
content: new Text(description),
actions: <Widget>[
new FlatButton(
child: new Text('Yes'),
onPressed: () {
_isDialogOpen = false;
yesFunction();
},
),
new FlatButton(
child: new Text('No'),
onPressed: () {
_isDialogOpen = false;
noFunction();
},
)
],
);
});
}希望我解释得很清楚。
发布于 2019-10-20 08:56:53
如果您想返回到第一页,而不是pop(),当用户选择“是”时,可以使用popUntil() .不需要通过“区域”
在这里了解更多信息:https://api.flutter.dev/flutter/widgets/Navigator/popUntil.html
https://stackoverflow.com/questions/58471351
复制相似问题