From 65d814516b773f2eb84d08d989a1ab0996057407 Mon Sep 17 00:00:00 2001 From: Hixie Date: Tue, 17 Nov 2015 16:49:22 -0800 Subject: [PATCH] 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. --- .../flutter/lib/src/material/icon_theme.dart | 2 +- .../lib/src/material/material_button.dart | 2 +- packages/flutter/lib/src/material/theme.dart | 2 +- packages/flutter/lib/src/widgets/basic.dart | 4 ++-- packages/flutter/lib/src/widgets/focus.dart | 8 +++---- .../flutter/lib/src/widgets/framework.dart | 23 ++++++++++++++++--- .../flutter/lib/src/widgets/media_query.dart | 2 +- .../flutter/lib/src/widgets/navigator.dart | 12 +--------- .../flutter/lib/src/widgets/page_storage.dart | 12 ++-------- 9 files changed, 33 insertions(+), 34 deletions(-) diff --git a/packages/flutter/lib/src/material/icon_theme.dart b/packages/flutter/lib/src/material/icon_theme.dart index 72b19a3031..18b81d8a04 100644 --- a/packages/flutter/lib/src/material/icon_theme.dart +++ b/packages/flutter/lib/src/material/icon_theme.dart @@ -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; } diff --git a/packages/flutter/lib/src/material/material_button.dart b/packages/flutter/lib/src/material/material_button.dart index e3044290ef..91d141a7c3 100644 --- a/packages/flutter/lib/src/material/material_button.dart +++ b/packages/flutter/lib/src/material/material_button.dart @@ -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; } diff --git a/packages/flutter/lib/src/material/theme.dart b/packages/flutter/lib/src/material/theme.dart index 7d9233a7c5..01aa26e196 100644 --- a/packages/flutter/lib/src/material/theme.dart +++ b/packages/flutter/lib/src/material/theme.dart @@ -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; } diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 7989cc9364..8b37e14125 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -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; } diff --git a/packages/flutter/lib/src/widgets/focus.dart b/packages/flutter/lib/src/widgets/focus.dart index 079ec9b29b..e34559edc1 100644 --- a/packages/flutter/lib/src/widgets/focus.dart +++ b/packages/flutter/lib/src/widgets/focus.dart @@ -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); } diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index 070398f93d..49f53b6a19 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -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 implements BuildContext { assert(() { _debugLifecycleState = _ElementLifecycle.defunct; return true; }); } + RenderObject findRenderObject() => renderObject; + Set _dependencies; - InheritedWidget inheritedWidgetOfType(Type targetType) { + InheritedWidget inheritFromWidgetOfType(Type targetType) { if (_dependencies == null) _dependencies = new Set(); _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; diff --git a/packages/flutter/lib/src/widgets/media_query.dart b/packages/flutter/lib/src/widgets/media_query.dart index 5640c47836..c5beabe81d 100644 --- a/packages/flutter/lib/src/widgets/media_query.dart +++ b/packages/flutter/lib/src/widgets/media_query.dart @@ -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; } diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index a6a0ba119b..8293121f31 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -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(); } diff --git a/packages/flutter/lib/src/widgets/page_storage.dart b/packages/flutter/lib/src/widgets/page_storage.dart index 051c765fe5..7bd1e18d2c 100644 --- a/packages/flutter/lib/src/widgets/page_storage.dart +++ b/packages/flutter/lib/src/widgets/page_storage.dart @@ -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;