Home > Enterprise >  Flutter: Hide first alert dialog when other is shown
Flutter: Hide first alert dialog when other is shown

Time:07-31

I want to hide the first alert dialog when I call showDialog() from it. After that when I close the second dialog I want the first dialog was visible again. How I can achieve this? enter image description here

CodePudding user response:

Before you call second dialog, use Navigator.of(context).pop() to close first dialog. Then, in the second one, you have functions then((value) {...}) or whenComplete(() {...}), inside that you can use it to re-open first dialog.

That's strange that you want to close first one, why don't you just leave it alone and let the second lies on it?

CodePudding user response:

You can create common dialog to show data. if its already showing then just update data only.

CodePudding user response:

showDialog return a future and you can pass data from dialog. The concept is here passing some flag to open the second dialog.

onPressed: () async {
  final data = await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          content: ElevatedButton(
            onPressed: () {
              Navigator.of(context)
                  .pop(true); // true for to show second dialog
            },
            child: Text("open Second dialog"),
          ),
        );
      });

  if (data == true) {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text("Second dialog"),
          );
        });
  }
},
  • Related