为什么Flutter State对象需要Widget? [英] Why does Flutter State object require a Widget?

查看:79
本文介绍了为什么Flutter State对象需要Widget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该演示示例包含以下代码,这显然是Flutter应用程序的典型代码:

The demo example has the following code, which is apparently typical of Flutter apps:

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) {...}
}

我很好,我想用 MyHomePage 覆盖 StatefulWidget createState 方法.有点尴尬,但是到底是什么呢?甚至取决于 State 子类.好吧.

I'm OK, I guess, with MyHomePage overriding the StatefulWidget createState method. It's a little awkward but what the heck? And even depending on a State subclass. Fine.

但是然后让 State 子类转过来并依赖于 MyHomePage 吗?我在把我相当丰富的智慧缠在那上面时遇到了麻烦.

But then having the State subclass turn around and depend on MyHomePage?! I'm having trouble wrapping my fairly abundant wits around that one.

因此,也许我不清楚什么是 State< MyHomePage> .例如,使用 Map< String,Object> 的含义很明确:将字符串与对象相关联.有人可以说明一下吗?而且,如果您可以包含有关需要使用状态对象来扩展窗口小部件的信息,那么我将很乐于阅读.

So perhaps I'm unclear on what State<MyHomePage> is/does. With, say, Map<String, Object> the meaning is clear: Associate a string with an 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")

然后,您很可能希望从 State 访问 foo / bar .

Then you most likely want to access foo/bar from your State.

如果没有这样的语法,则必须键入自定义的 State 构造函数,并将所有 StatefulWidget 子类属性传递给 createState内部的 State 子类.基本上,您会得到:

Without such syntax you'd have to type custom State constructor and pass all StatefulWidget subclass properties to State subclass inside createState. 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(

    );
  }
}

无聊.您必须两次编写 StatefulWidget 子类的所有字段.

Boring. You have to write all fields of StatefulWidget subclass twice.

使用当前语法;您不必这样做.您可以使用 widget 字段直接在 State 中访问当前实例化的小部件的所有属性.

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.

class MyStatefulWidgetState extends State<MyStatefulWidget> {
  @override
  Widget build(BuildContext context) {
    print(widget.foo);
    return Container();
  }
}

这篇关于为什么Flutter State对象需要Widget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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