0

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.

asked Oct 16, 2020 at 2:48

2 Answers 2

3

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');
 }
answered Oct 16, 2020 at 3:11
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I just noticed that. What a dump mistake! haha Anyway, thank you a lot!
1

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

answered Oct 16, 2020 at 3:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.