BuildContext在Flutter中做什么? [英] What does BuildContext do in Flutter?

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

问题描述

BuildContext的作用是什么,我们从中得到什么信息?

What does BuildContext do, and what information do we get out of it?

https://docs.flutter.io/flutter/widgets/BuildContext- class.html 尚不清楚.

https://flutter.io/widgets-intro/#basic-widgets在术语BuildContext的第9个实例中,有一个示例,但尚不清楚如何使用它.这是令我迷惑的一大堆代码的一部分,因此我很难理解BuildContext是什么.

https://flutter.io/widgets-intro/#basic-widgets on the 9th instance of the term BuildContext there is an example, but it's not clear how it is being used. It's part of a much larger set of code that loses me, and so I am having a hard time understanding just what BuildContext is.

有人可以用简单/非常基本的术语来解释吗?

Can someone explain this in simple/very basic terms?

推荐答案

BuildContext就像其名称所暗示的那样,是在其中构建特定小部件的上下文.

BuildContext is, like it's name is implying, the context in which a specific widget is built.

如果您以前曾经做过一些React,那么该上下文类似于React的上下文(但使用起来更加流畅);还有一些奖金.

If you ever done some React before, that context is kind of similar to React's context (but much smoother to use) ; with a few bonuses.

一般而言,上下文有2个用例:

Generally speaking, there are 2 uses cases for context :

  • 与父母互动(主要是获取/发布数据)
  • 在屏幕上渲染后,获取屏幕尺寸和位置

第二点有点罕见.另一方面,几乎在所有地方都使用了第一点.

The second point is kinda rare. On the other hand, the first point is used nearly everywhere.

例如,当您想推一条新路线时,您将执行Navigator.of(context).pushNamed('myRoute').

For example, when you want to push a new route, you'll do Navigator.of(context).pushNamed('myRoute').

在此处注意上下文.它将用于获取树中上方最靠近NavigatorState小部件的实例.然后在该实例上调用方法pushNamed.

Notice the context here. It'll be used to get the closest instance of NavigatorState widget above in the tree. Then call the method pushNamed on that instance.

很酷,但是什么时候要使用它?

Cool, but when do I want to use it ?

BuildContext非常有用.配置例如;您将需要在任何地方访问它们.但是您不想在每个构造函数上传递它.

BuildContext is really useful when you want to pass data downward without having to manually assign it to every widgets. Configurations for example ; you'll want to access them everywhere. But you don't want to pass it on every single constructor.

您可能会创建一个整体或一个单例;但是当conf更改后,您的小部件将不会自动重建.

You could potentially make a global or a singleton ; but then when confs change your widgets won't automatically rebuild.

在这种情况下,请使用InheritedWidget.有了它,您可能会编写以下内容:

In this case, you use InheritedWidget. With it you could potentially write the following :

class Configuration extends InheritedWidget {
  final String myConf;

  const Configuration({this.myConf, Widget child}): super(child: child);

  @override
  bool updateShouldNotify(Configuration oldWidget) {
    return myConf != oldWidget.myConf;
  }
}

然后,以这种方式使用它:

And then, use it this way :

void main() {
  runApp(
    new Configuration(
      myConf: "Hello world",
      child: new MaterialApp(
        // usual stuff here
      ),
    ),
  );
}

现在,在应用程序中无处不在,您可以使用BuildContext访问这些配置.通过做

Thanks to that, now everywhere inside your app, you can access these configs using the BuildContext. By doing

final configuration = context.inheritFromWidgetOfExactType(Configuration);

更酷的是,当配置更改时,调用inheritFromWidgetOfExactType(Configuration)所有小部件将自动重建.

And even cooler is that all widgets who call inheritFromWidgetOfExactType(Configuration) will automatically rebuild when the configurations change.

好极了吗?

这篇关于BuildContext在Flutter中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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