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. [...]
複製代碼