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

非同期機能の場合

await前に追加して、次の行showDialog()を呼び出しますsetState((){})

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

同期機能用

.then()コールバックを使用して、呼び出しますsetState((){})


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