问题
I'm creating a Sign-Up section in my application using Flutter and I'm trying to do the following:
The user clicks Sign-Up
The user presses the back button at any stage during sign-up
Then an alert dialog pops up asking if the user really wants to leave sign-up process
The user presses Yes
The user is then taken back to the first (main) page of the app.
This works for the first page after I click sign-up, but when I get to the second page, step 4 keeps me on the same page, then when I try it again, it works. But this shouldn't be the case.
After some debugging, I found the issue, but I don't know why it's happening.
I wrote the issue as a comment in my second code snippet, at the end of the onBackPressed()
function so it's clear as to where it's breaking :).
Here is my code for navigating to the sign-up process page from my main.dart:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SignUp())
)
Then for each page during the sign-up process, whenever I go onto the next page I do a Navigation.pop(context)
to pop the current sign-up page off the stack the do a Navigation.push()
right after to push the next page on.
Here is the code for the back button functionality in each sign-up page:
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;
}
And lastly, the yesOrNoAlert()
function in the Tools
class.
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();
},
)
],
);
});
}
Hope I explained this well.
回答1:
if you want go back to the first page, instead of pop(), you can use popUntil() when the user selects yes... no need to pass “res”
Learn more about it here: https://api.flutter.dev/flutter/widgets/Navigator/popUntil.html
来源:https://stackoverflow.com/questions/58471351/navigation-pop-and-navigation-push-acting-strangely