Search code examples
flutterpopupnavigatorsnackbar

Flutter: Pop from AlertBuilder and then show SnackBar


I am new in Flutter as well as App Development, What I want to achieve is Show SnackBar after poping from alertbuilder. I think following code will be useful for understanding my intentions

onSubmitted: (value) async {
                            try {
                              await FirebaseAuth.instance
                                  .sendPasswordResetEmail(
                                      email: _loginEmail);
                            } catch (e) {
                              SnackBar(
                                backgroundColor: Colors.pink,
                                content: Text(e.toString()),
                                duration: Duration(seconds: 5),
                              );
                              Navigator.of(context).pop();
                            }
                            SnackBar(
                              backgroundColor: Colors.pink,
                              content: Text('Email sent with a link!'),
                              duration: Duration(seconds: 5),
                            );
                            Navigator.of(context).pop();
                          },

Solution

  • I hope it'll solve your problem

    1, You should await your showDialog at your onpressed event like this

    var result = await showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return AlertDialog(
           title: Text("your title")
           content: Text("your content")
           actions: <Widget>[
            FlatButton(
              onPressed: () {
                Navigator.pop(context, 'success'); // here you can make a callback
              },
              child: Text('Okay')),]
        );
      },
    );
    

    2, so you can write

    if(result=="success"){}
    

    3, Then, show your SnackBar after that. Before showing SnackBar you have to add your snackbar prop like I write below

    final snackbar = SnackBar(content: Text('Your context message'));
    

    and then you can show the snackbar with your context like this

    Scaffold.of(context).showSnackBar(snackbar);