2

I have a problem. I would like to pop the screen (of main context) when alert dialog is closed. So here how it goes:

  1. User does some staff.
  2. Alert dialog is open
  3. It is open for 3 seconds.
  4. Alert dialog is closed
  5. We move user to the main screen (EmailPage for me)

Whole method:

Future<void> _sendMessageToSupport() async {
    final body = {
      'email': emailController.text, //
      'topic': topicController.text, //
      'message': contentController.text,
    };
    final jsonString = json.encode(body);
    final uri =
        Uri.http(AppConstants.BASE_URL, AppConstants.SUPPORT_CONTACT_ENDPOINT);
    final headers = {HttpHeaders.contentTypeHeader: 'application/json'};
    _setRefreshProgressIndicator(true);
    await http.post(uri, headers: headers, body: jsonString);
    _setRefreshProgressIndicator(false);
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (context) {
        Future.delayed(
          Duration(seconds: 3),
          () {
            Navigator.of(context).pop(true);
            /*Navigator.push(context,
                new MaterialPageRoute(builder: (context) => EmailPage()));*/ //navigates to EmailPage rather than pop to it
          },
        );
        return AlertDialog(
          title: Text(
            'blabla',
            style: TextStyle(
              fontSize: 18,
              color: Color(0xff000000),
            ),
            textAlign: TextAlign.center,
          ),
        );
      },
    );
  }

We move to this screen (let's say A screen) from the EmailPage. User does staff and we should automatically back to the EmailPage. I have made it like this:

Future.delayed(
          Duration(seconds: 3),
          () {
            Navigator.of(context).pop(true);
            /*Navigator.push(context,
                new MaterialPageRoute(builder: (context) => EmailPage()));*/ //navigates to EmailPage rather than pop
          },
        );

but the problem is we move user to EmailPage by pushing new screen and then when he clicks on the back arrow on the appbar he is moved to screen A again.

Could you tell me how can I do it? I mean, I know that I need to pop from context of build method, but how? When I did something like this (pop on the end of the method):

builder: (context) {
        Future.delayed(
          Duration(seconds: 3),
          () {
            Navigator.of(context).pop(true);
            /*Navigator.push(context,
                new MaterialPageRoute(builder: (context) => EmailPage()));*/ //navigates to EmailPage rather than pop
          },
        );
        return AlertDialog(
          title: Text(
            'Wysłano zapytanie do supportu. Postaramy się odpowiedzieć jak najszybciej!',
            style: TextStyle(
              fontSize: 18,
              color: Color(0xff000000),
            ),
            textAlign: TextAlign.center,
          ),
        );
      },
    );
    Navigator.of(context).pop(true);
  }

then popup was not even shown.

2 Answers 2

2

enter image description here

Let me know if this is not what you're looking for.

class EmailPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('EmailPage')),
      body: Center(
        child: ElevatedButton(
          onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (_) => PageA())),
          child: Text('Go to Page A'),
        ),
      ),
    );
  }
}

class PageA extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('PageA')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Timer(Duration(seconds: 3), () {
              Navigator.pop(context); // Dismisses dialog
              Navigator.pop(context); // Navigates back to previous screen
            });

            showDialog(
              context: context,
              builder: (_) => AlertDialog(
                title: Text('Dialog'),
                content: Text('Dismissing in 3s'),
              ),
            );
          },
          child: Text('Show Dialog'),
        ),
      ),
    );
  }
}
0

The Alert Dialog returns a future that runs when the Dialog is Closed so you can subscribe to that future and use your pop code inside that future. / Here is a simple example to showcase

RaisedButton(
        onPressed: () {
          showDialog(
              context: context,
              builder: (context) => AlertDialog(
                    title: Text('Are you sure?'),
                    content: Text('Do you want to remove item?'),
                    actions: <Widget>[
                      FlatButton(
                          onPressed: () => Navigator.of(context).pop('Success'),
                          child: Text('NO')),
                      FlatButton(
                          onPressed: () => Navigator.of(context).pop('Failure'),
                          child: Text('YES'))
                    ],
                  )).then((value) =>
              print('Result: ' + value.toString()));
        },
        child: Text('Show Alert Dialog'),
      ),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.