Flutter에서 showDialog를 닫은 후 부모 위젯을 업데이트하는 방법은 무엇입니까?

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