首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ShowDialog没有弹出

ShowDialog没有弹出
EN

Stack Overflow用户
提问于 2022-12-02 10:22:08
回答 2查看 32关注 0票数 0

我已经创建了调用showDialog的全局方法,每当我调用它时,如果我放置Navigator.pop(上下文),如果删除导航器,它就不会出现。如果没有导航器,我无法关闭错误对话框。我做错什么了吗?下面是我的代码

代码语言:javascript
复制
class GlobalMethod {
  static void showErrorDialog(
      {required String error, required BuildContext ctx}) {
    showDialog(
        context: ctx,
        builder: (context) {
          return AlertDialog(
            title: Row(children: [
              Padding(
                padding: EdgeInsets.all(8.0),
                child: Icon(
                  Icons.logout,
                  color: Colors.grey,
                  size: 35,
                ),
              ),
              Padding(
                padding: EdgeInsets.all(8.0),
                child: Text('Error Occured'),
              ),
            ]),
            content: Text(error,
                style: TextStyle(
                    color: Colors.black,
                    fontSize: 20,
                    fontStyle: FontStyle.italic)),
            actions: [
              TextButton(
                onPressed: () {
                  Navigator.canPop(context) ? Navigator.canPop(context) : null;
                },
                child: Text(
                  'Okay',
                  style: TextStyle(
                    color: Colors.red,
                  ),
                ),
              )
            ],
          );
        });
  }

当我调用该方法时,这就是一个例子。如果我删除Navigator.pop,错误对话框就会弹出,如果我把navigator.pop放出来,什么都不会出来。

代码语言:javascript
复制
else if (balance < price! ){
      GlobalMethod.showErrorDialog(error: "you dont have enough balance , please top up first", ctx: context);
      Navigator.pop(context);
      
    }
EN

回答 2

Stack Overflow用户

发布于 2022-12-02 10:34:05

您需要在showErrorDialog之后删除Navigator:

代码语言:javascript
复制
else if (balance < price! ){
      GlobalMethod.showErrorDialog(error: "you dont have enough balance , please top up first", ctx: context);
      
}

然后在您的showErrorDialog中更改这个

代码语言:javascript
复制
onPressed: () {
    Navigator.canPop(context) ? Navigator.pop(context): null;
},

您正在使用canPop,但是您需要使用pop进行导航,canPop只是返回一个bool结果。

票数 1
EN

Stack Overflow用户

发布于 2022-12-02 10:33:24

对于pop使用Navigator.pop(上下文)

代码语言:javascript
复制
class GlobalMethod {
      static void showErrorDialog(
          {required String error, required BuildContext ctx}) {
        showDialog(
            context: ctx,
            builder: (context) {
              return AlertDialog(
                title: Row(children: [
                  Padding(
                    padding: EdgeInsets.all(8.0),
                    child: Icon(
                      Icons.logout,
                      color: Colors.grey,
                      size: 35,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(8.0),
                    child: Text('Error Occured'),
                  ),
                ]),
                content: Text(error,
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 20,
                        fontStyle: FontStyle.italic)),
                actions: [
                  TextButton(
                    onPressed: () {
                      Navigator.canPop(ctx) ? Navigator.pop(context) : null;
                    },
                    child: Text(
                      'Okay',
                      style: TextStyle(
                        color: Colors.red,
                      ),
                    ),
                  )
                ],
              );
            });
      }
    }

删除pop

代码语言:javascript
复制
else if (balance < price! ){
      GlobalMethod.showErrorDialog(error: "you dont have enough balance , please top up first", ctx: context);
      
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74654180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档