BuildContext 在 Flutter 中有什么作用?
What does BuildContext do in Flutter?
BuildContext
做什么,我们从中得到什么信息?
https://docs.flutter.dev/flutter/widgets/BuildContext-class.html就是不清楚。
https://flutter.dev/widgets-intro/#basic-widgets 术语BuildContext
的第9个实例有一个例子,但不清楚它是如何使用的。它是让我迷失的更大代码集的一部分,所以我很难理解 BuildContext
是什么。
有人可以用 simple/very 基本术语解释一下吗?
BuildContext
就像它的名字所暗示的那样,是构建特定小部件的上下文。
如果你以前做过一些 React,那个上下文有点类似于 React 的上下文(但使用起来更顺畅);有一些奖金。
一般来说,上下文有 2 个用例:
- 与您的 parents 互动(主要是 get/post 数据)
- 在屏幕上呈现后,获取屏幕大小和位置
第二点比较少见。另一方面,第一点几乎无处不在。
例如,当你想推送一条新路由时,你会做Navigator.of(context).pushNamed('myRoute')
。
注意这里的上下文。它将用于获取树中上方最接近的 NavigatorState
小部件实例。然后在该实例上调用方法 pushNamed
。
很酷,但是我什么时候想用它?
BuildContext 在您想要向下传递数据时非常有用 而无需 必须手动将其分配给每个小部件的配置;例如;您将希望随处访问它们。但是您不想将它传递给每个构造函数。
您可能会制作一个全局的或单例的;但是当 confs 更改时,您的小部件将不会自动重建。
在这种情况下,您使用 InheritedWidget
。有了它,您可能会编写以下内容:
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;
}
}
然后,这样使用它:
void main() {
runApp(
new Configuration(
myConf: "Hello world",
child: new MaterialApp(
// usual stuff here
),
),
);
}
多亏了这一点,现在 在您的应用中的任何地方 ,您都可以使用 BuildContext
访问这些配置。通过做
final configuration = context.inheritFromWidgetOfExactType(Configuration);
更酷的是 所有 调用 inheritFromWidgetOfExactType(Configuration)
的小部件将在配置更改时自动重建。
很棒吧?
什么是 BuildContext object/context?
在了解 BuildCotext 之前,我们必须了解 Element 对象。
什么是Element对象
(注意:作为一名 flutter 开发人员,我们从未使用过 Element 对象,但我们使用过一个类似于 Element 对象的对象(称为 BuildContext 对象))
The Element object is the build location of the current widget.
“构建位置”的真正含义是什么?
- when the framework builds a widget object by calling its constructor will correspondingly need to create an element object for that widget object.
- And this element object represents the build location of that widget.
- This element object has many useful instance methods.
谁在使用 Element 对象及其方法?
They are 02 parties that use the Element object and its methods.
- Framework (To create RenderObject tree etc)
- Developers (Like us)
什么是 BuildContext 对象?
BuildContext objects are actually Element objects. The BuildContext interface is used to discourage direct manipulation of Element objects.
因此 BuildContext object = discouraged element object
(与原始 Element 对象相比,包含较少数量的实例方法)
为什么框架不鼓励将 Element 对象传递给我们?
Because Element object has instance methods that must only be needed by the framework itself.
but what happens when we access these methods by us, It's something that should not be done.
So that the reason why framework discouraged the Element object and pass it to us
好了下面说正题
BuildContext 对象在 Flutter 中有什么作用?
BuildContext object has several useful methods to easily perform certain tasks that need to be done in the widget tree.
- findAncestorWidgetOfExactType()。
Returns the nearest ancestor widget of the given type T.
- findAncestorStateOfType()。
Returns the State object of the nearest ancestor StatefulWidget.
- dependOnInheritedWidgetOfExactType()。
Obtains the nearest widget of the given type T, which must be the type of a concrete InheritedWidget subclass, and registers this build context with that widget such that when that widget changes.
[Used by Provider package]
如果您想查看该 BuildContext 对象的所有方法,请访问此 LINK + 请参阅 @remi Rousselot 的回答。
以上方法主要是 BuildContext 对象的实例方法。
BuildContext
做什么,我们从中得到什么信息?
https://docs.flutter.dev/flutter/widgets/BuildContext-class.html就是不清楚。
https://flutter.dev/widgets-intro/#basic-widgets 术语BuildContext
的第9个实例有一个例子,但不清楚它是如何使用的。它是让我迷失的更大代码集的一部分,所以我很难理解 BuildContext
是什么。
有人可以用 simple/very 基本术语解释一下吗?
BuildContext
就像它的名字所暗示的那样,是构建特定小部件的上下文。
如果你以前做过一些 React,那个上下文有点类似于 React 的上下文(但使用起来更顺畅);有一些奖金。
一般来说,上下文有 2 个用例:
- 与您的 parents 互动(主要是 get/post 数据)
- 在屏幕上呈现后,获取屏幕大小和位置
第二点比较少见。另一方面,第一点几乎无处不在。
例如,当你想推送一条新路由时,你会做Navigator.of(context).pushNamed('myRoute')
。
注意这里的上下文。它将用于获取树中上方最接近的 NavigatorState
小部件实例。然后在该实例上调用方法 pushNamed
。
很酷,但是我什么时候想用它?
BuildContext 在您想要向下传递数据时非常有用 而无需 必须手动将其分配给每个小部件的配置;例如;您将希望随处访问它们。但是您不想将它传递给每个构造函数。
您可能会制作一个全局的或单例的;但是当 confs 更改时,您的小部件将不会自动重建。
在这种情况下,您使用 InheritedWidget
。有了它,您可能会编写以下内容:
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;
}
}
然后,这样使用它:
void main() {
runApp(
new Configuration(
myConf: "Hello world",
child: new MaterialApp(
// usual stuff here
),
),
);
}
多亏了这一点,现在 在您的应用中的任何地方 ,您都可以使用 BuildContext
访问这些配置。通过做
final configuration = context.inheritFromWidgetOfExactType(Configuration);
更酷的是 所有 调用 inheritFromWidgetOfExactType(Configuration)
的小部件将在配置更改时自动重建。
很棒吧?
什么是 BuildContext object/context?
在了解 BuildCotext 之前,我们必须了解 Element 对象。
什么是Element对象
(注意:作为一名 flutter 开发人员,我们从未使用过 Element 对象,但我们使用过一个类似于 Element 对象的对象(称为 BuildContext 对象))
The Element object is the build location of the current widget.
“构建位置”的真正含义是什么?
- when the framework builds a widget object by calling its constructor will correspondingly need to create an element object for that widget object.
- And this element object represents the build location of that widget.
- This element object has many useful instance methods.
谁在使用 Element 对象及其方法?
They are 02 parties that use the Element object and its methods.
- Framework (To create RenderObject tree etc)
- Developers (Like us)
什么是 BuildContext 对象?
BuildContext objects are actually Element objects. The BuildContext interface is used to discourage direct manipulation of Element objects.
因此 BuildContext object = discouraged element object
(与原始 Element 对象相比,包含较少数量的实例方法)
为什么框架不鼓励将 Element 对象传递给我们?
Because Element object has instance methods that must only be needed by the framework itself. but what happens when we access these methods by us, It's something that should not be done. So that the reason why framework discouraged the Element object and pass it to us
好了下面说正题
BuildContext 对象在 Flutter 中有什么作用?
BuildContext object has several useful methods to easily perform certain tasks that need to be done in the widget tree.
- findAncestorWidgetOfExactType()。
Returns the nearest ancestor widget of the given type T.
- findAncestorStateOfType()。
Returns the State object of the nearest ancestor StatefulWidget.
- dependOnInheritedWidgetOfExactType()。
Obtains the nearest widget of the given type T, which must be the type of a concrete InheritedWidget subclass, and registers this build context with that widget such that when that widget changes. [Used by Provider package]
如果您想查看该 BuildContext 对象的所有方法,请访问此 LINK + 请参阅 @remi Rousselot 的回答。
以上方法主要是 BuildContext 对象的实例方法。