0

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');
                                }

                              },

请查看我的代码并告诉我出了什么问题。谢谢你。我期待着您的回音。

4

2 回答 2

2

这是问题所在:

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');
                                      }
于 2020-10-16T03:11:46.413 回答
1

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

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

于 2020-10-16T03:12:49.963 回答