[英]Why does Flutter State object require a Widget?
The demo example has the following code, which is apparently typical of Flutter apps:演示示例有以下代码,这显然是 Flutter 应用程序的典型:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {...}
}
I'm OK, I guess, with MyHomePage
overriding the StatefulWidget
createState
method.我想我没问题,因为
MyHomePage
覆盖了StatefulWidget
createState
方法。 It's a little awkward but what the heck?这有点尴尬,但到底是什么? And even depending on a
State
subclass.甚至取决于
State
子类。 Fine.美好的。
But then having the State
subclass turn around and depend on MyHomePage
?!但是让
State
子类转向并依赖MyHomePage
?! I'm having trouble wrapping my fairly abundant wits around that one.我很难用我相当丰富的智慧来解决这个问题。
So perhaps I'm unclear on what State<MyHomePage>
is/does.所以也许我不清楚
State<MyHomePage>
是/做什么。 With, say, Map<String, Object>
the meaning is clear: Associate a string with an object.例如,
Map<String, Object>
的含义很明确:将字符串与对象相关联。 Can someone please elucidate?有人可以解释一下吗? And if you could include something about the need for a state object to extend a widget, I would enjoy reading that.
如果您可以包含一些关于需要状态对象来扩展小部件的内容,我会很乐意阅读。
This is to make widget properties access far easier.这是为了使访问小部件属性变得更加容易。 When you do
当你做
new MyStatefulWidget(foo: 42, bar: "string")
Then you most likely want to access foo
/ bar
from your State
.那么你很可能想从你的
State
访问foo
/ bar
。
Without such syntax you'd have to type custom State
constructor and pass all StatefulWidget
subclass properties to State
subclass inside createState
.如果没有这样的语法,您必须键入自定义
State
构造函数并将所有StatefulWidget
子类属性传递给createState
内的State
子类。 Basically you'd have that:基本上你会有:
class MyStatefulWidget extends StatefulWidget {
final int foo;
MyStatefulWidget({this.foo});
@override
MyStatefulWidgetState createState() => MyStatefulWidgetState(foo: foo);
}
class MyStatefulWidgetState extends State<MyStatefulWidget> {
final int foo;
MyStatefulWidgetState({this.foo});
@override
Widget build(BuildContext context) {
return Container(
);
}
}
Boring.无聊的。 You have to write all fields of
StatefulWidget
subclass twice.您必须两次编写
StatefulWidget
子类的所有字段。
With the current syntax;使用当前的语法; you don't have to do this.
你不必这样做。 You can directly access all properties of the currently instantiated widget within
State
by using widget
field.您可以使用
widget
字段直接访问State
内当前实例化小部件的所有属性。
class MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
print(widget.foo);
return Container();
}
}
In Flutter, everything is a widget and there are 2 types of widgets: Stateless and Statefull.在 Flutter 中,一切都是小部件,有两种类型的小部件:无状态和有状态。
Stateless: A widget that does not require mutable state.无状态:不需要可变状态的小部件。
Statefull: A widget that has mutable state. Statefull:具有可变状态的小部件。
That is why all Statefull widget is dependent of a State<T>
, because it manages changes (state) in the widget.这就是所有 Statefull 小部件都依赖于
State<T>
原因,因为它管理小部件中的更改(状态)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.