Pave the Foo.of() cowpath.
I'm planning on adding more .of() functions and so to avoid further copypasta I'm providing some general utility functions here.
This commit is contained in:
@@ -19,7 +19,7 @@ class IconTheme extends InheritedWidget {
|
||||
final IconThemeData data;
|
||||
|
||||
static IconThemeData of(BuildContext context) {
|
||||
IconTheme result = context.inheritedWidgetOfType(IconTheme);
|
||||
IconTheme result = context.inheritFromWidgetOfType(IconTheme);
|
||||
return result?.data;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ class ButtonTheme extends InheritedWidget {
|
||||
final ButtonColor color;
|
||||
|
||||
static ButtonColor of(BuildContext context) {
|
||||
ButtonTheme result = context.inheritedWidgetOfType(ButtonTheme);
|
||||
ButtonTheme result = context.inheritFromWidgetOfType(ButtonTheme);
|
||||
return result?.color ?? ButtonColor.normal;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ class Theme extends InheritedWidget {
|
||||
static final ThemeData _kFallbackTheme = new ThemeData.fallback();
|
||||
|
||||
static ThemeData of(BuildContext context) {
|
||||
Theme theme = context.inheritedWidgetOfType(Theme);
|
||||
Theme theme = context.inheritFromWidgetOfType(Theme);
|
||||
return theme == null ? _kFallbackTheme : theme.data;
|
||||
}
|
||||
|
||||
|
||||
@@ -972,7 +972,7 @@ class DefaultTextStyle extends InheritedWidget {
|
||||
final TextStyle style;
|
||||
|
||||
static TextStyle of(BuildContext context) {
|
||||
DefaultTextStyle result = context.inheritedWidgetOfType(DefaultTextStyle);
|
||||
DefaultTextStyle result = context.inheritFromWidgetOfType(DefaultTextStyle);
|
||||
return result?.style;
|
||||
}
|
||||
|
||||
@@ -1163,7 +1163,7 @@ class DefaultAssetBundle extends InheritedWidget {
|
||||
final AssetBundle bundle;
|
||||
|
||||
static AssetBundle of(BuildContext context) {
|
||||
DefaultAssetBundle result = context.inheritedWidgetOfType(DefaultAssetBundle);
|
||||
DefaultAssetBundle result = context.inheritFromWidgetOfType(DefaultAssetBundle);
|
||||
return result?.bundle;
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ class Focus extends StatefulComponent {
|
||||
static bool at(BuildContext context, Widget widget, { bool autofocus: true }) {
|
||||
assert(widget != null);
|
||||
assert(widget.key is GlobalKey);
|
||||
_FocusScope focusScope = context.inheritedWidgetOfType(_FocusScope);
|
||||
_FocusScope focusScope = context.inheritFromWidgetOfType(_FocusScope);
|
||||
if (focusScope != null) {
|
||||
if (autofocus)
|
||||
focusScope._setFocusedWidgetIfUnset(widget.key);
|
||||
@@ -100,7 +100,7 @@ class Focus extends StatefulComponent {
|
||||
|
||||
static bool _atScope(BuildContext context, Widget widget, { bool autofocus: true }) {
|
||||
assert(widget != null);
|
||||
_FocusScope focusScope = context.inheritedWidgetOfType(_FocusScope);
|
||||
_FocusScope focusScope = context.inheritFromWidgetOfType(_FocusScope);
|
||||
if (focusScope != null) {
|
||||
if (autofocus)
|
||||
focusScope._setFocusedScopeIfUnset(widget.key);
|
||||
@@ -118,7 +118,7 @@ class Focus extends StatefulComponent {
|
||||
static void moveTo(BuildContext context, Widget widget) {
|
||||
assert(widget != null);
|
||||
assert(widget.key is GlobalKey);
|
||||
_FocusScope focusScope = context.inheritedWidgetOfType(_FocusScope);
|
||||
_FocusScope focusScope = context.inheritFromWidgetOfType(_FocusScope);
|
||||
if (focusScope != null)
|
||||
focusScope.focusState._setFocusedWidget(widget.key);
|
||||
}
|
||||
@@ -126,7 +126,7 @@ class Focus extends StatefulComponent {
|
||||
static void moveScopeTo(BuildContext context, Focus component) {
|
||||
assert(component != null);
|
||||
assert(component.key != null);
|
||||
_FocusScope focusScope = context.inheritedWidgetOfType(_FocusScope);
|
||||
_FocusScope focusScope = context.inheritFromWidgetOfType(_FocusScope);
|
||||
if (focusScope != null)
|
||||
focusScope.focusState._setFocusedScope(component.key);
|
||||
}
|
||||
|
||||
@@ -534,7 +534,9 @@ typedef void ElementVisitor(Element element);
|
||||
abstract class BuildContext {
|
||||
Widget get widget;
|
||||
RenderObject findRenderObject();
|
||||
InheritedWidget inheritedWidgetOfType(Type targetType);
|
||||
InheritedWidget inheritFromWidgetOfType(Type targetType);
|
||||
Widget ancestorWidgetOfType(Type targetType);
|
||||
State ancestorStateOfType(Type targetType);
|
||||
void visitAncestorElements(bool visitor(Element element));
|
||||
void visitChildElements(void visitor(Element element));
|
||||
}
|
||||
@@ -806,18 +808,33 @@ abstract class Element<T extends Widget> implements BuildContext {
|
||||
assert(() { _debugLifecycleState = _ElementLifecycle.defunct; return true; });
|
||||
}
|
||||
|
||||
RenderObject findRenderObject() => renderObject;
|
||||
|
||||
Set<Type> _dependencies;
|
||||
InheritedWidget inheritedWidgetOfType(Type targetType) {
|
||||
InheritedWidget inheritFromWidgetOfType(Type targetType) {
|
||||
if (_dependencies == null)
|
||||
_dependencies = new Set<Type>();
|
||||
_dependencies.add(targetType);
|
||||
return ancestorWidgetOfType(targetType);
|
||||
}
|
||||
|
||||
Widget ancestorWidgetOfType(Type targetType) {
|
||||
Element ancestor = _parent;
|
||||
while (ancestor != null && ancestor.widget.runtimeType != targetType)
|
||||
ancestor = ancestor._parent;
|
||||
return ancestor?.widget;
|
||||
}
|
||||
|
||||
RenderObject findRenderObject() => renderObject;
|
||||
State ancestorStateOfType(Type targetType) {
|
||||
Element ancestor = _parent;
|
||||
while (ancestor != null) {
|
||||
if (ancestor is StatefulComponentElement && ancestor.state.runtimeType == targetType)
|
||||
break;
|
||||
ancestor = ancestor._parent;
|
||||
}
|
||||
StatefulComponentElement statefulAncestor = ancestor;
|
||||
return statefulAncestor?.state;
|
||||
}
|
||||
|
||||
void visitAncestorElements(bool visitor(Element element)) {
|
||||
Element ancestor = _parent;
|
||||
|
||||
@@ -42,7 +42,7 @@ class MediaQuery extends InheritedWidget {
|
||||
final MediaQueryData data;
|
||||
|
||||
static MediaQueryData of(BuildContext context) {
|
||||
MediaQuery query = context.inheritedWidgetOfType(MediaQuery);
|
||||
MediaQuery query = context.inheritFromWidgetOfType(MediaQuery);
|
||||
return query == null ? null : query.data;
|
||||
}
|
||||
|
||||
|
||||
@@ -54,17 +54,7 @@ class Navigator extends StatefulComponent {
|
||||
|
||||
static const String defaultRouteName = '/';
|
||||
|
||||
static NavigatorState of(BuildContext context) {
|
||||
NavigatorState result;
|
||||
context.visitAncestorElements((Element element) {
|
||||
if (element is StatefulComponentElement && element.state is NavigatorState) {
|
||||
result = element.state;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
static NavigatorState of(BuildContext context) => context.ancestorStateOfType(NavigatorState);
|
||||
|
||||
NavigatorState createState() => new NavigatorState();
|
||||
}
|
||||
|
||||
@@ -86,16 +86,8 @@ class PageStorage extends StatelessComponent {
|
||||
|
||||
/// Might return null if there is no PageStorage in this context.
|
||||
static PageStorageBucket of(BuildContext context) {
|
||||
PageStorageBucket result;
|
||||
context.visitAncestorElements((Element element) {
|
||||
Widget widget = element.widget;
|
||||
if (widget is PageStorage) {
|
||||
result = widget.bucket;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return result;
|
||||
PageStorage widget = context.ancestorWidgetOfType(PageStorage);
|
||||
return widget?.bucket;
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) => child;
|
||||
|
||||
Reference in New Issue
Block a user