Flutter - 只能弹出 AlertDialog 或执行传递的函数 - 两者都需要做
Flutter - Can only pop AlertDialog OR execute passed function - need to do both
TL;DR:尝试将函数调用传递给自定义 AlertDialog。 AlertDialog 需要在函数后弹出 -> 不能使它工作。
我创建了一个自定义 AlertDialog 以在我的应用程序中使用。它看起来像这样:
customAlertDialog({required context, buttonAction}){
showDialog(context: context, builder: (context) => AlertDialog(
title: Text("example title", style: TextStyle(color: AppTheme.colors.white),),
content: const Text("some content"),
actions: [
TextButton(onPressed: () {Navigator.of(context).pop();}, child: Text(
"Abbrechen",
style: TextStyle(
color: AppTheme.colors.white),
),),
TextButton(
child: Text("do something",
style: TextStyle(
color: AppTheme.colors.lightRed),
),
onPressed: buttonAction)
],
),);
}
customAlertDialog 将函数调用作为最后一个 TextButtons onPressed 操作的参数(此处命名为 buttonAction)。当我通过时它工作正常:
buttonAction: () => deleteUser(context)
问题是这不能与 pop 方法结合使用。在下面只会调用 deleteUser:
buttonAction: () => [deleteUser(context), Navigator.of(context).pop()]
这样写也是一样的:
buttonAction: () {deleteUser(context), Navigator.of(context).pop()}
我猜 customAlertDialog 本身的上下文需要弹出。所以我在 customAlertDialog 中尝试了以下内容(buttonAction 包含 () => deleteUser(context):
onPressed: () => [buttonAction, Navigator.of(context).pop()]
现在它只弹出对话框,可能是因为 dart 无法解释 buttonAction。所以我一直在寻找一种方法来只传递应该调用但没有成功的函数。
所以我的问题是:如何在传递函数的同时弹出对话框?
编辑:
正如@SlowDeepCoder 提到的,问题可能是 Navigator.pop() 方法在 deleteUser() 完成之前从堆栈中抛出上下文,因此它不起作用。尝试用以下方法修复它,但没有用:
buttonAction: () async{ await deleteUser(context); Navigator.of(context).pop(); }
您不必总是使用箭头函数来传递自定义函数。
您可以简单地将函数传递为
buttonAction: (){
deleteUser(context);
Navigator.of(context).pop();
}
这是因为您从不正确的 BuildContext
调用 Navigator
上的 pop()
。所以而不是
buttonAction: () {
deleteUser(context);
Navigator.of(context).pop();
}
你必须这样做:
buttonAction: (innerContext) {
deleteUser(innerContext); // not sure if you need the innerContext here. Depends on your other app code
// deleteUser(context); // use this line if the above does not work
Navigator.of(innerContext).pop();
}
连同
TextButton(
child: Text("do something"),
onPressed: () => buttonAction(context),
)
我还建议您为 BuildContext
的机器人(customAlertDialog
和 showDialog
)取不同的名字。
TL;DR:尝试将函数调用传递给自定义 AlertDialog。 AlertDialog 需要在函数后弹出 -> 不能使它工作。
我创建了一个自定义 AlertDialog 以在我的应用程序中使用。它看起来像这样:
customAlertDialog({required context, buttonAction}){
showDialog(context: context, builder: (context) => AlertDialog(
title: Text("example title", style: TextStyle(color: AppTheme.colors.white),),
content: const Text("some content"),
actions: [
TextButton(onPressed: () {Navigator.of(context).pop();}, child: Text(
"Abbrechen",
style: TextStyle(
color: AppTheme.colors.white),
),),
TextButton(
child: Text("do something",
style: TextStyle(
color: AppTheme.colors.lightRed),
),
onPressed: buttonAction)
],
),);
}
customAlertDialog 将函数调用作为最后一个 TextButtons onPressed 操作的参数(此处命名为 buttonAction)。当我通过时它工作正常:
buttonAction: () => deleteUser(context)
问题是这不能与 pop 方法结合使用。在下面只会调用 deleteUser:
buttonAction: () => [deleteUser(context), Navigator.of(context).pop()]
这样写也是一样的:
buttonAction: () {deleteUser(context), Navigator.of(context).pop()}
我猜 customAlertDialog 本身的上下文需要弹出。所以我在 customAlertDialog 中尝试了以下内容(buttonAction 包含 () => deleteUser(context):
onPressed: () => [buttonAction, Navigator.of(context).pop()]
现在它只弹出对话框,可能是因为 dart 无法解释 buttonAction。所以我一直在寻找一种方法来只传递应该调用但没有成功的函数。
所以我的问题是:如何在传递函数的同时弹出对话框?
编辑: 正如@SlowDeepCoder 提到的,问题可能是 Navigator.pop() 方法在 deleteUser() 完成之前从堆栈中抛出上下文,因此它不起作用。尝试用以下方法修复它,但没有用:
buttonAction: () async{ await deleteUser(context); Navigator.of(context).pop(); }
您不必总是使用箭头函数来传递自定义函数。
您可以简单地将函数传递为
buttonAction: (){
deleteUser(context);
Navigator.of(context).pop();
}
这是因为您从不正确的 BuildContext
调用 Navigator
上的 pop()
。所以而不是
buttonAction: () {
deleteUser(context);
Navigator.of(context).pop();
}
你必须这样做:
buttonAction: (innerContext) {
deleteUser(innerContext); // not sure if you need the innerContext here. Depends on your other app code
// deleteUser(context); // use this line if the above does not work
Navigator.of(innerContext).pop();
}
连同
TextButton(
child: Text("do something"),
onPressed: () => buttonAction(context),
)
我还建议您为 BuildContext
的机器人(customAlertDialog
和 showDialog
)取不同的名字。