how to update parent widget after showDialog is closed in Flutter?

Viewed 863

I have a widget with an icon when I click on it a dialog widget is shown, here is the call for the dialog :

// this icon is in widget parent
           IconButton(
              icon: Icon(
                Icons.info_outline,
                size: mobileWidth * 0.07,
              ),
              tooltip: 'information',
              color: Colors.blueGrey,
              onPressed: () {
                showAlertInfo(context, code);
                setState(() {});
              },
            ),

here is my dialog :

showAlertInfo(BuildContext context, String code) {
  showDialog(
    context: context,
    builder: (context) {
      return StatefulBuilder(
        builder: (context, setState) {
          return AlertDialog(
            title: Text(
              "Information sur le client $code", ),
            content: SingleChildScrollView(
               child: Container( ..... 
               /* ...
               this dialog has some operations that changes info 
               values of the widget that called this in first place
               it is a big code to put here*/
             // here I have a close button
                    actions: [
            FlatButton(
            child: Text(
              "Fermer",
              style: TextStyle(
                color: Colors.red,
                fontSize: mobileWidth * 0.035,
                fontWeight: FontWeight.bold,
              ),
            ),
            onPressed: () {
              Navigator.of(context).pop(); // dismiss dialog
            },
          ),
        ],
      );  

what I'm trying to achieve is when the dialog is dismissed I want the parent widget to be updated, so how I call setState of the parent widget when the dialog widget is closed ?

1 Answers

For Async function

Add await before showDialog() and call setState((){}) on the next line.

await showDialog(
    context: context,
    builder: (context) {
       return yourWidget;
    }
);
setState((){});

For Sync Function

Use the .then() callback, and call setState((){}) in it.


showDialog(
    context: context,
    builder(context) {
        return yourWidget;
    }).then((_){
        setState((){});
    }
);

Related