导航后打开Flutter对话框 [英] Open flutter dialog after navigation

查看:102
本文介绍了导航后打开Flutter对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我致电Navigator pushReplacement以在我的flutter应用程序中显示一个新视图,并想立即弹出一个简单对话框,向用户介绍该页面. (我希望用户能够在后台看到新视图)

I call Navigator pushReplacement to show a new view within my flutter app and want to immediately pop up a simple dialog to introduce the page to the user. (I want the user to be able to see the new view in the background)

如果我在窗口小部件的build方法内调用showDialog,然后随后从build方法返回窗口小部件(支架),则会出现错误,表明flutter已经在绘制窗口小部件.我希望我需要侦听构建完成事件,然后调用showDialog.

If I call showDialog within the build method of a widget, and then subsequently return a widget (scaffold) from the build method I get errors stating that flutter is already drawing a widget. I expect I need to listen on a build complete event and then call showDialog.

有关如何做到这一点的指导.

Guidance on how to do that much appreciated.

推荐答案

您可以从"initState()"内部调用对话框,在绘制第一帧后就可以保留对话框的外观.

You can call the dialog from inside 'initState()' dalaying its appearance after the first frame has been drawn.

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await showDialog<String>(
        context: context,
        builder: (BuildContext context) => new AlertDialog(
              title: new Text("title"),
              content: new Text("Message"),
              actions: <Widget>[
                new FlatButton(
                  child: new Text("OK"),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              ],
            ),
      );
    });
  }

这篇关于导航后打开Flutter对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆