Force Flutter navigator to reload state when popping

前端 未结 14 2207
情书的邮戳
情书的邮戳 2020-11-28 22:54

I have one StatefulWidget in Flutter with button, which navigates me to another StatefulWidget using Navigator.push(). On second widge

14条回答
  •  天涯浪人
    2020-11-28 23:20

    You can pass back a dynamic result when you are popping the context and then call the setState((){}) when the value is true otherwise just leave the state as it is.

    I have pasted some code snippets for your reference.

    handleClear() async {
        try {
          var delete = await deleteLoanWarning(
            context,
            'Clear Notifications?',
            'Are you sure you want to clear notifications. This action cannot be undone',
          );
          if (delete.toString() == 'true') {
            //call setState here to rebuild your state.
    
          }
        } catch (error) {
          print('error clearing notifications' + error.toString());
                 }
      }
    
    
    
    Future deleteLoanWarning(BuildContext context, String title, String msg) async {
    
      return await showDialog(
            context: context,
            child: new AlertDialog(
              title: new Text(
                title,
                style: new TextStyle(fontWeight: fontWeight, color: CustomColors.continueButton),
                textAlign: TextAlign.center,
              ),
              content: new Text(
                msg,
                textAlign: TextAlign.justify,
              ),
              actions: [
                new Container(
                  decoration: boxDecoration(),
                  child: new MaterialButton(
                    child: new Text('NO',),
                    onPressed: () {
                      Navigator.of(context).pop(false);
                    },
                  ),
                ),
                new Container(
                  decoration: boxDecoration(),
                  child: new MaterialButton(
                    child: new Text('YES', ),
                    onPressed: () {
                      Navigator.of(context).pop(true);
                    },
                  ),
                ),
              ],
            ),
          ) ??
          false;
    }
    

    Regards, Mahi

提交回复
热议问题