Flutter & AlertDialog : My app doesn't show the alert dialog after loading

1.4k views Asked by At

Flutter & AlertDialog : My app doesn't show the alert dialog after loading. Even the 2 prints before and after the alert dialog was printed, the dialog was skip. Why is that? Please help me with this.

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

                              },

Please have a look on my code and tell me what's wrong. Thank you. I am looking forward to hearing from you.

2

There are 2 answers

1
Dan Gerchcovich On BEST ANSWER

Here is the problem:

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

You are presenting the dialogue, then popping it. Make sure to add the Navigator.pop(context) method, only after you click a button on the alert dialogue. So, rewrite the code like this:

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

The issue lies in the line just after the print('After dialog') line. You are doing a Navigator.pop(context); which is basically removing the dialog from the navigation stack.

In flutter: The showDialog() is used to show the dialog. And the Navigator.pop(context) is used to remove the dialog