【发布时间】:2018-10-30 00:20:00
【问题描述】:
我正在尝试从 Flutter 中的不同小部件更改状态。例如,在以下示例中,我在几秒钟后设置状态。
下面是代码:
class _MyAppState extends State<MyApp> {
int number = 1;
@override
void initState() {
super.initState();
new Future.delayed(new Duration(seconds: 5)).then((_) {
this.setState(() => number = 2);
print("Changed");
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new FlatButton(
color: Colors.blue,
child: new Text("Next Page"),
onPressed: () {
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) => new StatefulBuilder(builder: (BuildContext context, setState) =>new MySecondPage(number))
));
},
),
),
);
}
}
我尝试使用InheritedWidget,但除非我将它包裹在我的顶级小部件周围,否则这将不起作用,这对于我正在尝试做的事情是不可行的(上面的代码是我所做的简化)我试图实现)。
关于在 Flutter 中实现这一目标的最佳方式有什么想法吗?
【问题讨论】:
-
相似,如果不相同:) stackoverflow.com/questions/52761888/…
-
@giorgio79 这个问题是在您链接的问题之前提出的?