Flutter Pop 最佳实践
Flutter pop best practice
我有以下流程屏幕 1 -> 屏幕 2 -> 对话框(在单独的小部件中)。
屏幕 2 显示一个对话框(关闭?是或否)。如果有人按是,我想 return 到屏幕 1,如果他们按否,只需关闭对话框并 return 到屏幕 2。我现在做的是当点击是时,我做Navigator.pop(上下文) 两次。这是一个好习惯吗?有没有办法将屏幕 2 的上下文传递到我的对话框小部件,以便我可以直接弹出那个?
就我个人而言,我认为将对话框的响应传递回页面,让页面处理其余部分会更好。
你可以这样做:
//I'm using a raised button just to call the alert as an example...
RaisedButton(
child: Text('Press me'),
//This part here is the important part
onPressed: () async {
//You can return anything when you use Navigator.pop
//In this case I'm returning a bool indicating if the page should close or not.
//You have to await this because it depends on user input.
bool shouldPopResult = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
//The content of your dialog
actions: <Widget>[
// The value you pass here in Navigator.of(context).pop
// is the value that will be stored in shouldPopResult,
// so if "Yes" is pressed, true will return...
// and if "No", false is returned.
FlatButton(
child: Text('Yes'),
onPressed: () => Navigator.of(context).pop(true),
),
FlatButton(
child: Text('No'),
onPressed: () => Navigator.of(context).pop(false),
)
],
),
);
// This is for if the user dismisses the dialog without pressing a button
// In that case shouldPopResult would be null, so I'm setting it to false.
// You can prevent the user from dismissing the dialog
// setting barrierDismissible to false in the showDialog method.
if (shouldPopResult == null) shouldPopResult = false;
// And finally with the dialog already dismissed, you can decide
// to go back or not.
if (shouldPopResult) Navigator.of(context).pop();
});
像往常一样,您可以将对话框提取为 Widget,或者完全提取处理对话框响应的功能或其他任何功能。
在flutter文档中可以看到页面返回数据的例子here。
我有以下流程屏幕 1 -> 屏幕 2 -> 对话框(在单独的小部件中)。
屏幕 2 显示一个对话框(关闭?是或否)。如果有人按是,我想 return 到屏幕 1,如果他们按否,只需关闭对话框并 return 到屏幕 2。我现在做的是当点击是时,我做Navigator.pop(上下文) 两次。这是一个好习惯吗?有没有办法将屏幕 2 的上下文传递到我的对话框小部件,以便我可以直接弹出那个?
就我个人而言,我认为将对话框的响应传递回页面,让页面处理其余部分会更好。
你可以这样做:
//I'm using a raised button just to call the alert as an example...
RaisedButton(
child: Text('Press me'),
//This part here is the important part
onPressed: () async {
//You can return anything when you use Navigator.pop
//In this case I'm returning a bool indicating if the page should close or not.
//You have to await this because it depends on user input.
bool shouldPopResult = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
//The content of your dialog
actions: <Widget>[
// The value you pass here in Navigator.of(context).pop
// is the value that will be stored in shouldPopResult,
// so if "Yes" is pressed, true will return...
// and if "No", false is returned.
FlatButton(
child: Text('Yes'),
onPressed: () => Navigator.of(context).pop(true),
),
FlatButton(
child: Text('No'),
onPressed: () => Navigator.of(context).pop(false),
)
],
),
);
// This is for if the user dismisses the dialog without pressing a button
// In that case shouldPopResult would be null, so I'm setting it to false.
// You can prevent the user from dismissing the dialog
// setting barrierDismissible to false in the showDialog method.
if (shouldPopResult == null) shouldPopResult = false;
// And finally with the dialog already dismissed, you can decide
// to go back or not.
if (shouldPopResult) Navigator.of(context).pop();
});
像往常一样,您可以将对话框提取为 Widget,或者完全提取处理对话框响应的功能或其他任何功能。
在flutter文档中可以看到页面返回数据的例子here。