как обновить родительский виджет после закрытия showDialog во Flutter?

Nov 29 2020

У меня есть виджет со значком, когда я нажимаю на него, отображается виджет диалога, вот вызов диалога:

// 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(() {});
              },
            ),

вот мой диалог:

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

то, что я пытаюсь достичь, - это когда диалоговое setStateокно закрывается. Я хочу, чтобы родительский виджет был обновлен, поэтому как я вызываю родительский виджет, когда виджет диалогового окна закрывается?

Ответы

2 TahaMalik Nov 29 2020 at 06:58

Для функции Async

Добавьте awaitдо showDialog()и позвоните setState((){})на следующую строку.

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

Для функции синхронизации

Используйте .then()обратный вызов и позвоните setState((){})в него.


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