您如何关闭 AlertDialog 和 运行 一些代码?
How do you dismiss an AlertDialog and run some code?
问题
所以我的 flutter 应用程序中有一个 AlertDialog,它有两个按钮:yes
和 no
。
无论单击哪个按钮,我都希望弹出窗口被关闭,但是如果按下 yes
,我也想 运行 一些代码。
我的代码
showDialog(context: context, builder: (_) => AlertDialog(
title: Text("Delete?"),
content: Text("Do you want to delete this task?"),
actions: [
MaterialButton(child: Text("Yes"),onPressed: () => Navigator.pop(context)),
MaterialButton(child: Text("No"), onPressed: () => Navigator.pop(context)),
],
), barrierDismissible: true);
我将不胜感激任何人的帮助
这是我解决同样问题的方法。我不确定是否有更简洁的方法
bool yesPressed = false;
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text("Delete?"),
content: Text("Do you want to delete this task?"),
actions: [
MaterialButton(
child: Text("Yes"),
onPressed: () {
yesPressed = true;
Navigator.pop(context);
}),
MaterialButton(
child: Text("No"),
onPressed: () => Navigator.pop(context)),
],
),
barrierDismissible: true)
.then((value) {
if (yesPressed) {
// Do something
}
});
您可以在调用 Navigator.pop
时简单地添加一个值,该值将通过 Future
从 showDialog
返回。这是一个代码示例:
showDialog<bool>(
context: context,
builder: (_) => AlertDialog(
title: Text("Delete?"),
content: Text("Do you want to delete this task?"),
actions: [
MaterialButton(child: Text("Yes"),onPressed: () => Navigator.pop(context, true)),
MaterialButton(child: Text("No"), onPressed: () => Navigator.pop(context, false)),
],
),
barrierDismissible: true,
).then((bool hasTappedOnYes) {
if (hasTappedOnYes) {}
else {}
});
试试这个
actions: [
MaterialButton(child: Text("Yes"),onPressed: () {
//do something
Navigator.pop(context))
}),
MaterialButton(child: Text("No"), onPressed: () => Navigator.pop(context)),
],
问题
所以我的 flutter 应用程序中有一个 AlertDialog,它有两个按钮:yes
和 no
。
无论单击哪个按钮,我都希望弹出窗口被关闭,但是如果按下 yes
,我也想 运行 一些代码。
我的代码
showDialog(context: context, builder: (_) => AlertDialog(
title: Text("Delete?"),
content: Text("Do you want to delete this task?"),
actions: [
MaterialButton(child: Text("Yes"),onPressed: () => Navigator.pop(context)),
MaterialButton(child: Text("No"), onPressed: () => Navigator.pop(context)),
],
), barrierDismissible: true);
我将不胜感激任何人的帮助
这是我解决同样问题的方法。我不确定是否有更简洁的方法
bool yesPressed = false;
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text("Delete?"),
content: Text("Do you want to delete this task?"),
actions: [
MaterialButton(
child: Text("Yes"),
onPressed: () {
yesPressed = true;
Navigator.pop(context);
}),
MaterialButton(
child: Text("No"),
onPressed: () => Navigator.pop(context)),
],
),
barrierDismissible: true)
.then((value) {
if (yesPressed) {
// Do something
}
});
您可以在调用 Navigator.pop
时简单地添加一个值,该值将通过 Future
从 showDialog
返回。这是一个代码示例:
showDialog<bool>(
context: context,
builder: (_) => AlertDialog(
title: Text("Delete?"),
content: Text("Do you want to delete this task?"),
actions: [
MaterialButton(child: Text("Yes"),onPressed: () => Navigator.pop(context, true)),
MaterialButton(child: Text("No"), onPressed: () => Navigator.pop(context, false)),
],
),
barrierDismissible: true,
).then((bool hasTappedOnYes) {
if (hasTappedOnYes) {}
else {}
});
试试这个
actions: [
MaterialButton(child: Text("Yes"),onPressed: () {
//do something
Navigator.pop(context))
}),
MaterialButton(child: Text("No"), onPressed: () => Navigator.pop(context)),
],