skip to Main Content

i have created global method that call showDialog, whenever i call it, it wont come out if i put Navigator.pop(context) , if i remove the navigator it will come out. I cannot close the errordialog if i dont have the navigator. Did i do something wrong? below is my code

class GlobalMethod {
  static void showErrorDialog(
      {required String error, required BuildContext ctx}) {
    showDialog(
        context: ctx,
        builder: (context) {
          return AlertDialog(
            title: Row(children: [
              Padding(
                padding: EdgeInsets.all(8.0),
                child: Icon(
                  Icons.logout,
                  color: Colors.grey,
                  size: 35,
                ),
              ),
              Padding(
                padding: EdgeInsets.all(8.0),
                child: Text('Error Occured'),
              ),
            ]),
            content: Text(error,
                style: TextStyle(
                    color: Colors.black,
                    fontSize: 20,
                    fontStyle: FontStyle.italic)),
            actions: [
              TextButton(
                onPressed: () {
                  Navigator.canPop(context) ? Navigator.canPop(context) : null;
                },
                child: Text(
                  'Okay',
                  style: TextStyle(
                    color: Colors.red,
                  ),
                ),
              )
            ],
          );
        });
  }

This is example when i call the method. If i remove the Navigator.pop the error dialog will pop out, if i put the navigator.pop nothing will come out

else if (balance < price! ){
      GlobalMethod.showErrorDialog(error: "you dont have enough balance , please top up first", ctx: context);
      Navigator.pop(context);
      
    }

2

Answers


  1. For the pop use Navigator.pop(context)

    class GlobalMethod {
          static void showErrorDialog(
              {required String error, required BuildContext ctx}) {
            showDialog(
                context: ctx,
                builder: (context) {
                  return AlertDialog(
                    title: Row(children: [
                      Padding(
                        padding: EdgeInsets.all(8.0),
                        child: Icon(
                          Icons.logout,
                          color: Colors.grey,
                          size: 35,
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.all(8.0),
                        child: Text('Error Occured'),
                      ),
                    ]),
                    content: Text(error,
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 20,
                            fontStyle: FontStyle.italic)),
                    actions: [
                      TextButton(
                        onPressed: () {
                          Navigator.canPop(ctx) ? Navigator.pop(context) : null;
                        },
                        child: Text(
                          'Okay',
                          style: TextStyle(
                            color: Colors.red,
                          ),
                        ),
                      )
                    ],
                  );
                });
          }
        }
    

    Remove pop

    else if (balance < price! ){
          GlobalMethod.showErrorDialog(error: "you dont have enough balance , please top up first", ctx: context);
          
        }
    
    Login or Signup to reply.
  2. You need to remove Navigator after the showErrorDialog:

    else if (balance < price! ){
          GlobalMethod.showErrorDialog(error: "you dont have enough balance , please top up first", ctx: context);
          
    }
    

    then change this in your showErrorDialog:

    onPressed: () {
        Navigator.canPop(context) ? Navigator.pop(context): null;
    },
    

    you are use canPop but you need to use pop for navigation, canPop just return you a bool result.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search