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

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

问题描述

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've ever done some React before, that context is kind of similar to React's context (but much smoother to use) ; with a few bonuses.

一般来说,上下文有两个用例:

Generally speaking, there are 2 use 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.

您可能会创建一个全局或单例;但是当 confs 更改时,您的小部件将不会自动重建.

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.

很棒吧?

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

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