导航后打开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();
},
),
],
),
);
});
}