BuildContext
是Flutter的重要部分,可是目前网上讲BuildContext
的文章太少了,因此本篇文章讲讲BuildContext
。html
BuildContext
,顾名思义,Build(构建Widget) Context(应用上下文),就是构建Widget中的应用上下文。bash
因此BuildContext
只出如今两个地方:app
BuildContext
实际是Element
,BuildContext
是为了阻止直接对Element
操做而抽象出来的,因此BuildContext
是Element
的抽象类,全部Element
都继承自BuildContext
。less
每个Widget都有一个BuildContext
。ide
BuildContext
是Widget在Widget树中位置的句柄。ui
BuildContext
被WidgetBulder
(例如:StatelessWidget.build, State.build)方法传递;this
BuildContext
也是State
的成员变量,在State
内部,能够经过context
直接访问spa
BuildContext
的做用主要是经过上下文获取指定的数据;rest
例如:Theme.of(context)
或者 showDialog(context: context,....)
都须要BuildContext
做为参数,这里的BuildContext
就是调用这些方法的Widget的表明。code
每个Widget都有一个BuildContext
。假设有个A Widget,A Widget里确定会有StatelessWidget.build
或者State.build
的build方法,build方法建立了 B Widget并返回,A Widget就是B Widget的父Widget,相应的, A Widget的BuildContext
也是是B Widget 的BuildContext
的父节点。
下面给一个例子加深理解:
@override
Widget build(BuildContext context) {
// here, Scaffold.of(context) returns null,
//在Scaffold建立以前,若是在这里调用Scaffold.of(context)会返回null,是由于此时Scaffo//ld还没建立,因此其BuildContext也没有建立,为了在建立Scaffold的时候就取到他的BuildC//ontext,要使用Builder
return Scaffold(
appBar: AppBar(title: Text('Demo')),
body: Builder(
builder: (BuildContext context) {
return FlatButton(
child: Text('BUTTON'),
onPressed: () {
// here, Scaffold.of(context) returns the locally created Scaffold
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Hello.')
));
}
);
}
)
);
}
复制代码
为何BuildContext
能够用来获取上下文的数据,主要是由于BuildContext
具备的如下方法:
Obtains the element corresponding to the nearest widget of the given type, which must be the type of a concrete InheritedWidget subclass. [...]
复制代码
Returns the RenderObject object of the nearest ancestor RenderObjectWidget widget that matches the given TypeMatcher. [...]
复制代码
Returns the State object of the nearest ancestor StatefulWidget widget that matches the given TypeMatcher. [...]
复制代码
Returns the nearest ancestor widget of the given type, which must be the type of a concrete Widget subclass. [...]
复制代码
The current RenderObject for the widget. If the widget is a RenderObjectWidget, this is the render object that the widget created for itself. Otherwise, it is the render object of the first descendant RenderObjectWidget. [...]
复制代码
Registers this build context with ancestor such that when ancestor's widget changes this build context is rebuilt. [...] 复制代码
Obtains the nearest widget of the given type, which must be the type of a concrete InheritedWidget subclass, and registers this build context with that widget such that when that widget changes (or a new widget of that type is introduced, or the widget goes away), this build context is rebuilt so that it can obtain new values from that widget. [...]
复制代码
Returns the State object of the furthest ancestor StatefulWidget widget that matches the given TypeMatcher. [...]
复制代码
Walks the ancestor chain, starting with the parent of this build context's widget, invoking the argument for each ancestor. The callback is given a reference to the ancestor widget's corresponding Element object. The walk stops when it reaches the root widget or when the callback returns false. The callback must not return null. [...]
复制代码
Walks the children of this widget. [...]
复制代码