【发布时间】:2019-01-16 21:59:51
【问题描述】:
我正在尝试使用showDialog(context, builder) 在用户导航到某个页面时显示问候消息。
我通过在该页面上的有状态小部件的initState 方法中调用showDialog 来尝试此操作。虽然它确实有效,但似乎我无法访问实际上下文。
Flutter 中有没有办法在initState() 方法中访问context?
如果没有,是否有其他方法可以更好地实现此行为?
@override
void initState() {
super.initState();
new Future.delayed(Duration.zero, () {
showDialog(context: context,
builder: (BuildContext context) {
return new Container(child: new Text('foo'));
});
});
}
【问题讨论】:
-
您可以在
initState方法中访问context。而且您链接的代码确实有效 -
“虽然它确实有效,但我似乎无法访问实际的上下文。”.. 你是怎么说的?
-
也许我误解了这个问题,我的小部件没有应用主题,而且这段代码看起来很可怕,也许上下文在那里,但我必须为上下文获取主题或什么?我回家后会检查一下。
-
次要,但您可能不需要
Container。
标签: flutter