Wie aktualisiere ich das übergeordnete Widget, nachdem showDialog in Flutter geschlossen wurde?

Nov 29 2020

Ich habe ein Widget mit einem Symbol, wenn ich darauf klicke, wird ein Dialog-Widget angezeigt. Hier ist der Aufruf für den 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(() {});
              },
            ),

Hier ist mein 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
            },
          ),
        ],
      );  

Was ich erreichen möchte, ist, wenn das Dialogfeld geschlossen wird. Ich möchte, dass das übergeordnete Widget aktualisiert wird. Wie rufe ich setStatedas übergeordnete Widget auf, wenn das Dialogfeld-Widget geschlossen wird?

Antworten

2 TahaMalik Nov 29 2020 at 06:58

Für die Async-Funktion

awaitVorher hinzufügen showDialog()und setState((){})in der nächsten Zeile anrufen .

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

Für die Synchronisierungsfunktion

Verwenden Sie den .then()Rückruf und rufen setState((){})Sie ihn an.


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