Search code examples
flutteralertnavigator

flutter navigator in alert box not working


in an edit page, once an edit is done I am showing an alert with Ok button. Once Okay button clicked, i wan to move to the previous screen. FOllowing is my alert box code

Future<Null> _showDoneDialog() async {
    return showDialog<Null>(
      context: context,
      //barrierDismissible: true,
      builder: (BuildContext context) {
        return new AlertDialog(
          title: Center(
            child: SingleChildScrollView(
              child: Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                        margin: EdgeInsets.only(top: 12),
                        child: Text(
                          "Profile has been edited successfully",
                          textAlign: TextAlign.start,
                          style: TextStyle(
                              color: Colors.black54,
                              fontSize: 14,
                              fontFamily: 'Oswald',
                              fontWeight: FontWeight.w400),
                        )),
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: GestureDetector(
                            onTap: () {
                              setState(() {
                                Navigator.of(context).pop();
                              });
                            },
                            child: Container(
                                decoration: BoxDecoration(
                                    color: header,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(100))),
                                child: Text(
                                  "OK",
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 17,
                                  ),
                                  textAlign: TextAlign.center,
                                )),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }

How to call the previous screen from alertbox -> Navigator.of(context).pop();


Solution

  • showDialog uses the Navigator to show. So if your navigator stack is

    Screen1>Screen2

    when you show your dialog it becomes

    Screen1>Screen2>FlutterDialog

    And... when you "pop" the navigator, it pops the last route, in this case, it's moving to the previous screen Screen2

    What i would suggest is waiting for the dialog to pop, then "pop" again (To go to Screen 1)

    You can do this in a simple manner.Just wait for the dialog to close itself

    void _showDoneDialog() async {
        await showDialog<Null>(
          context: context,
          //barrierDismissible: true,
          builder: (BuildContext dialogContext) {
            return new AlertDialog(
              title: Center(
                child: SingleChildScrollView(
                  child: Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Container(
                            margin: EdgeInsets.only(top: 12),
                            child: Text(
                              "Profile has been edited successfully",
                              textAlign: TextAlign.start,
                              style: TextStyle(
                                  color: Colors.black54,
                                  fontSize: 14,
                                  fontFamily: 'Oswald',
                                  fontWeight: FontWeight.w400),
                            )),
                        Row(
                          children: <Widget>[
                            Expanded(
                              child: GestureDetector(
                                onTap: () {
                                  setState(() {
                                    Navigator.of(dialogContext).pop();
                                  });
                                },
                                child: Container(
                                    decoration: BoxDecoration(
                                        color: header,
                                        borderRadius:
                                            BorderRadius.all(Radius.circular(100))),
                                    child: Text(
                                      "OK",
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 17,
                                      ),
                                      textAlign: TextAlign.center,
                                    )),
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            );
          },
        );
    
    
        Navigator.of(context).pop();
      }