Home > OS >  ShowDialog did not pop out
ShowDialog did not pop out

Time:12-02

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);
      
    }

CodePudding user response:

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.

CodePudding user response:

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);
      
    }
  • Related