Home > Enterprise >  How to close severals showDialogs in flutter
How to close severals showDialogs in flutter

Time:12-15

How can I to close all the showDialogs in my aplication? in this case _mostrarDialogConfirmacion is the dialog where i request to the user a confirmation to make a query, cargandoDialog is another dialog where i show a loading message while the query is executing, when the query finish, i want to close the two dialogs and only see the _mostrarDialogMensaje dialog

_mostrarDialogConfirmacion(
  mensaje, 
  BuildContext context, 
  codLink, 
  motivo,
) {
  return showDialog(
    context: context, 
    builder: (context){
      return AlertDialog(
        title: const Text(
          'Informacion', 
          textAlign: TextAlign.center,
        ),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget> [
            Text(mensaje, textAlign: TextAlign.center),
          ],
        ),
        actions: <Widget> [
          TextButton(
            onPressed: () async {
              Navigator.of(context).pop();
              cargandoDialog(context);
              List<dynamic> ingresarReclamo1 = await ingresarReclamo
                  .ingresarReclamo(codLink, titular, motivo);
              // ignore: use_build_context_synchronously
              Navigator.of(context).pop();
              // ignore: use_build_context_synchronously
              _mostrarDialogMensaje(
                ingresarReclamo1[0].observaciones,
                ingresarReclamo1[0].validado, 
                context,
              );
            }, 
            child: const Text('Si')
          ),
          TextButton(
            onPressed: ()=> Navigator.of(context).pop(),
            child: const Text('No')
          ),
        ],
      );
    }
  );
}

CodePudding user response:

you will dismiss first dialog and then you can use whenComplete to dismiss the second dialog

showDialog(
  .....
).whenComplete(() => Navigator.of(context).pop())

CodePudding user response:

You can return bool while .pop(boolValue) to check the tap button. Also, the showDialog is a future method, you can use await until it finished and then processed to next dialog. Navigator.of(context).pop() will be used to close recent dialog on this case. As for my understanding about the question, try this example code.

 _mostrarDialogConfirmacion(mensaje, BuildContext context, codLink, motivo) {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) {
          return AlertDialog(
            title: const Text('Informacion', textAlign: TextAlign.center),
            content: Column(
              mainAxisSize: MainAxisSize.min,
            ),
            actions: <Widget>[
              TextButton(
                  onPressed: () async {
                    // Navigator.of(context)
                    //     .pop(); //if you like to close previous one before showing the next dialog
                    final bool isSi = await showDialog(
                        barrierDismissible: false,
                        builder: (context) => AlertDialog(
                              content: Text("Second Dialog"),
                              actions: [
                                TextButton(
                                    onPressed: () {
                                      Navigator.of(context).pop(true);
                                    },
                                    child: const Text('Si')),
                                TextButton(
                                    onPressed: () =>
                                        Navigator.of(context).pop(false),
                                    child: const Text('No')),
                              ],
                            ),
                        context: context);
                    if (mounted && isSi) {
                      Navigator.of(context).pop();
                      await showDialog(
                          builder: (context) => AlertDialog(
                              content: Text("_mostrarDialogMensaje")),
                          context: context);
                    }
                  },
                  child: const Text('Si')),
              TextButton(
                  onPressed: () => Navigator.of(context).pop(),
                  child: const Text('No')),
            ],
          );
        });
  }

  • Related