【问题标题】:Flutter AlertDialog Navigator.pop(context) returning a black screenFlutter AlertDialog Navigator.pop(context) 返回黑屏
【发布时间】:2019-08-06 09:35:46
【问题描述】:

我正在使用 NetworkGiffyDialog 自定义 alertdialog 但它会出现黑屏背景,有时应用程序会崩溃 问题在这里:

onOkButtonPressed: () { Navigator.of(context).pop(); 

我调用的函数:

void showAlert(BuildContext context, String txt, String logo) {
      showDialog(
          context: context,
          builder: (_) => NetworkGiffyDialog(
                image: Image.asset("Assets/" + logo + ".gif"),
                title: Text('Produit Radar',
                    textAlign: TextAlign.center,
                    style:
                        TextStyle(fontSize: 22.0, fontWeight: FontWeight.w600)),
                description: Text(
                  txt,
                  textAlign: TextAlign.center,
                ),
                onOkButtonPressed: () {
                  Navigator.of(context).pop();
                },
              ));
    }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您与此代码一起使用的context - Navigator.of(context) - 是您传递给showAlert 函数的那个​​,它可能是您在其上显示对话框的路线的上下文。因此,当您调用pop() 时,您实际上是在弹出路由,而不是对话框。如果这是导航堆栈中的第一条路线,则结果将是您提到的黑屏。

    builder: (_) 替换为builder: (BuildContext context),这样当您调用Navigator.of(context).pop(); 时,您实际上是在使用对话框的上下文,因此对话框将被关闭。

    【讨论】:

    • 我多次调用 thaat fct 的问题,如果用户单击确定,我的对话框将不会关闭,但是当没有执行任何操作并且在该对话框中 diplau=ying rslt 之后(显示示例 3 对话框)用户需要多次按确定才能关闭所有对话框
    猜你喜欢
    • 2019-05-12
    • 2021-07-28
    • 2019-09-19
    • 1970-01-01
    • 2021-07-31
    • 2021-05-18
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    相关资源
    最近更新 更多