在 Flutter 中用户 Future.delayed 之后,文本小部件不会出现在对话框中

Text widget doesn't appear in dialog after user Future.delayed in Flutter

用户点击发送按钮后会出现一个对话框。当对话框出现时,我想在 5 秒后显示一个文本。我使用 Future.delayed 但文本根本没有出现。它仅在我关闭对话框并再次打开时出现。我想在打开对话框 5 秒后显示文本。

这是我的函数

  void _initialize() {
    Future<void>.delayed(const Duration(seconds: 3), () {
      if (mounted) {
        setState(() {
          visibility = true;
        });
      }
    });
  }
}

这是我在按钮的 onTab 中的对话框代码

_initialize()
 showDialog(context: context,
        barrierDismissible: false,

        builder: (BuildContext contextd){

          return  WillPopScope(

            onWillPop: () {return Future.value(false);},
            child: Dialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10)
              ),
              child: Padding(
                padding: EdgeInsets.fromLTRB(20.w, 20.h, 20.w, 20.h),
                child: Column(
                        children: [
                       //here are some widgets
                      Visibility(
                        visible:visibility?true:false,
                        child: Text("Resend?",style: TextStyle(decoration: 
                              TextDecoration.underline,),)),
               ],),
              ),
            ),
          );

        });

尝试像这样使用 StatefulBuilder

showDialog(
        context: context,
        builder: (BuildContext context) => StatefulBuilder(
          builder: (context, setState) {
            return //somthing to return;
          },
        ));
  

尝试使用 StatefulBuilder。使用 bool visiblesetDialogState 参数,您可以更新对话框内容的状态。

showDialog(
  context: context,
  barrierDismissible: false,
  builder: (BuildContext context) {
    return StatefulBuilder(
      builder(context, setDialogState) {
        // this variable is used for the conditional display
        bool visible = false;
        // this future will update the visible variable in 3 seconds, after the dialog is displayed already
        Future.delayed(Duration(seconds: 3)).then(() => setDialogState(() { visible = true;}));
        return WillPopScope(
          onWillPop: () { return Future.value(false); },
          child: Dialog(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10)
              ),
            child: Padding(
              padding: EdgeInsets.fromLTRB(20.w, 20.h, 20.w, 20.h),
              child: Column(
                children: [
                  //here are some widgets
                  Visibility(
                    visible:visible,
                    child: Text(
                      "Resend?",
                      style: TextStyle(
                        decoration: TextDecoration.underline,
                        ),
                      ),
                    ),
                  ],
                  ),
              ),
            ),
          );
      },
      );
};

发生这种情况是因为您的 setState 更新 visibility 具有与构建器对话框不同的上下文。 Visibility 实际上是更新,但仅在关闭并再次打开时出现在对话框中,因为这是构建对话框的更新方式。如果你想使用上下文对话框需要使用那个小部件:

StatefulBuilder(
      builder: (context, setState) {

进入您的对话框,然后调用 setState