【问题标题】:Flutter & AlertDialog : My app doesn't show the alert dialog after loadingFlutter & AlertDialog : 我的应用在加载后不显示警报对话框
【发布时间】:2021-01-30 13:37:27
【问题描述】:

Flutter & AlertDialog :我的应用在加载后不显示警报对话框。 即使在打印警报对话框之前和之后的 2 次打印,该对话框也被跳过。这是为什么?请帮我解决这个问题。

onTap: () async {
                                if (_formKey.currentState.validate() &&
                                    _ratingStar > 0) {
                                  setState(() {
                                    
                                    _loading = true;
                                  });
                                  
                                  dynamic result =
                                      await User_DatabaseService().uploadFeedback(
                                    comment: review );
                                  setState(() {
                                    _loading = false;
                                  });
                                  if (result) {
                                    print('Before dialog');
                                    showDialog(
                                      context: context,
                                      builder: (BuildContext context) {
                                        return AlertDialog(
                                          shape: RoundedRectangleBorder(
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(6.0))),
                                          
                                          content: Column(
                                            mainAxisSize: MainAxisSize.min,
                                            children: <Widget>[
                                              Container(
                                                padding: EdgeInsets.symmetric(
                                                    vertical: 60, horizontal: 10),
                                                child: Text(
                                                  //'Please rate with star',
                                                  '평가해 주셔서 감사합니다!',
                                                  style: TextStyle(
                                                    fontSize: 20,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                              ),
                                              InkWell(
                                                onTap: () {
                                                  Navigator.pop(context);
                                                },
                                                child: Container(
                                                  alignment: Alignment.center,
                                                  height: 50,
                                                  //color: primaryColor,
                                                  child: Text(
                                                    AppLocalizations.of(context)
                                                        .translate('OKAY'),
                                                    style: TextStyle(
                                                        color: Colors.white,
                                                        fontWeight:
                                                            FontWeight.bold),
                                                  ),
                                                ),
                                              ),
                                            ],
                                          ),
                                        );
                                      },
                                    );
                                    print('After dialog');
                                    Navigator.pop(context);
                                  } else {
                                    print('Sth wrong');
                                  }
                                } else {
                                  
                                  print('Not submit');
                                }

                              },

请查看我的代码并告诉我有什么问题。谢谢你。我期待您的来信。

【问题讨论】:

    标签: flutter dart exception flutter-alertdialog


    【解决方案1】:

    问题来了:

    if (result) {
                                        print('Before dialog');
                                        showDialog(
                                          context: context,
                                          builder: (BuildContext context) {
                                            return AlertDialog(
                                              shape: RoundedRectangleBorder(
                                                  borderRadius: BorderRadius.all(
                                                      Radius.circular(6.0))),
                                              
                                              content: Column(
                                                mainAxisSize: MainAxisSize.min,
                                                children: <Widget>[
                                                  Container(
                                                    padding: EdgeInsets.symmetric(
                                                        vertical: 60, horizontal: 10),
                                                    child: Text(
                                                      //'Please rate with star',
                                                      '평가해 주셔서 감사합니다!',
                                                      style: TextStyle(
                                                        fontSize: 20,
                                                        fontWeight: FontWeight.bold,
                                                      ),
                                                    ),
                                                  ),
                                                  InkWell(
                                                    onTap: () {
                                                      Navigator.pop(context);
                                                    },
                                                    child: Container(
                                                      alignment: Alignment.center,
                                                      height: 50,
                                                      //color: primaryColor,
                                                      child: Text(
                                                        AppLocalizations.of(context)
                                                            .translate('OKAY'),
                                                        style: TextStyle(
                                                            color: Colors.white,
                                                            fontWeight:
                                                                FontWeight.bold),
                                                      ),
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            );
                                          },
                                        );
                                        print('After dialog');
                                        Navigator.pop(context);
                                      } else {
                                        print('Sth wrong');
                                      }
    

    您正在呈现对话,然后弹出它。确保仅在单击警报对话框上的按钮后添加 Navigator.pop(context) 方法。所以,像这样重写代码:

    if (result) {
                                            print('Before dialog');
                                            showDialog(
                                              context: context,
                                              builder: (BuildContext context) {
                                                return AlertDialog(
                                                  shape: RoundedRectangleBorder(
                                                      borderRadius: BorderRadius.all(
                                                          Radius.circular(6.0))),
                                                  
                                                  content: Column(
                                                    mainAxisSize: MainAxisSize.min,
                                                    children: <Widget>[
                                                      Container(
                                                        padding: EdgeInsets.symmetric(
                                                            vertical: 60, horizontal: 10),
                                                        child: Text(
                                                          //'Please rate with star',
                                                          '평가해 주셔서 감사합니다!',
                                                          style: TextStyle(
                                                            fontSize: 20,
                                                            fontWeight: FontWeight.bold,
                                                          ),
                                                        ),
                                                      ),
                                                      InkWell(
                                                        onTap: () {
                                                          Navigator.pop(context);
                                                        },
                                                        child: Container(
                                                          alignment: Alignment.center,
                                                          height: 50,
                                                          //color: primaryColor,
                                                          child: Text(
                                                            AppLocalizations.of(context)
                                                                .translate('OKAY'),
                                                            style: TextStyle(
                                                                color: Colors.white,
                                                                fontWeight:
                                                                    FontWeight.bold),
                                                          ),
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                );
                                              },
                                            );
                                            print('After dialog');
                                          } else {
                                            print('Sth wrong');
                                          }
    

    【讨论】:

    • 是的,我刚刚注意到了。多么大的错误!哈哈无论如何,非常感谢!
    【解决方案2】:

    问题在于print('After dialog') 行之后的行。您正在执行Navigator.pop(context);,这基本上是从导航堆栈中删除对话框。

    在颤动中: showDialog() 用于显示对话框。而Navigator.pop(context) 用于删除对话框

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 1970-01-01
      • 2021-01-17
      • 2020-01-14
      • 1970-01-01
      • 2021-08-14
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多