Commit 4cae568f authored by Collin Jackson's avatar Collin Jackson

Move inheritedOfType from Component to Widget so that Switch can use it

parent 74a14d2a
...@@ -19,8 +19,8 @@ class DefaultTextStyle extends Inherited { ...@@ -19,8 +19,8 @@ class DefaultTextStyle extends Inherited {
final TextStyle style; final TextStyle style;
static TextStyle of(Component component) { static TextStyle of(Widget widget) {
DefaultTextStyle result = component.inheritedOfType(DefaultTextStyle); DefaultTextStyle result = widget.inheritedOfType(DefaultTextStyle);
return result == null ? null : result.style; return result == null ? null : result.style;
} }
......
...@@ -272,6 +272,27 @@ abstract class Widget { ...@@ -272,6 +272,27 @@ abstract class Widget {
return ancestor; return ancestor;
} }
Set<Type> _dependencies;
Inherited inheritedOfType(Type targetType) {
if (_dependencies == null)
_dependencies = new Set<Type>();
_dependencies.add(targetType);
Widget ancestor = parent;
while (ancestor != null && ancestor.runtimeType != targetType)
ancestor = ancestor.parent;
return ancestor;
}
void dependenciesChanged() {
// This is called if you've use inheritedOfType and the Inherited
// ancestor you were provided was changed. For a widget to use Inherited
// it needs an implementation of dependenciesChanged. If your widget extends
// Component or RenderObjectWrapper this is provided for you automatically.
// If you aren't able to extend one of those classes, you need to
// provide your own implementation of dependenciesChanged.
assert(false);
}
void remove() { void remove() {
_renderObject = null; _renderObject = null;
setParent(null); setParent(null);
...@@ -453,10 +474,9 @@ abstract class Inherited extends TagNode { ...@@ -453,10 +474,9 @@ abstract class Inherited extends TagNode {
void notifyDescendants() { void notifyDescendants() {
final Type ourRuntimeType = runtimeType; final Type ourRuntimeType = runtimeType;
void notifyChildren(Widget child) { void notifyChildren(Widget child) {
if (child is Component && if (child._dependencies != null &&
child._dependencies != null &&
child._dependencies.contains(ourRuntimeType)) child._dependencies.contains(ourRuntimeType))
child._dependenciesChanged(); child.dependenciesChanged();
if (child.runtimeType != ourRuntimeType) if (child.runtimeType != ourRuntimeType)
child.walkChildren(notifyChildren); child.walkChildren(notifyChildren);
} }
...@@ -608,17 +628,7 @@ abstract class Component extends Widget { ...@@ -608,17 +628,7 @@ abstract class Component extends Widget {
_built.detachRoot(); _built.detachRoot();
} }
Set<Type> _dependencies; void dependenciesChanged() {
Inherited inheritedOfType(Type targetType) {
if (_dependencies == null)
_dependencies = new Set<Type>();
_dependencies.add(targetType);
Widget ancestor = parent;
while (ancestor != null && ancestor.runtimeType != targetType)
ancestor = ancestor.parent;
return ancestor;
}
void _dependenciesChanged() {
// called by Inherited.sync() // called by Inherited.sync()
_scheduleBuild(); _scheduleBuild();
} }
...@@ -937,6 +947,11 @@ abstract class RenderObjectWrapper extends Widget { ...@@ -937,6 +947,11 @@ abstract class RenderObjectWrapper extends Widget {
} }
} }
void dependenciesChanged() {
// called by Inherited.sync()
syncRenderObject(this);
}
void remove() { void remove() {
assert(renderObject != null); assert(renderObject != null);
_nodeMap.remove(renderObject); _nodeMap.remove(renderObject);
......
...@@ -30,47 +30,34 @@ const Duration _kCheckDuration = const Duration(milliseconds: 200); ...@@ -30,47 +30,34 @@ const Duration _kCheckDuration = const Duration(milliseconds: 200);
const Size _kSwitchSize = const Size(_kSwitchWidth + 2.0, _kSwitchHeight + 2.0); const Size _kSwitchSize = const Size(_kSwitchWidth + 2.0, _kSwitchHeight + 2.0);
const double _kReactionRadius = _kSwitchWidth / 2.0; const double _kReactionRadius = _kSwitchWidth / 2.0;
class Switch extends Component { class Switch extends LeafRenderObjectWrapper {
Switch({Key key, this.value, this.onChanged}) : super(key: key); Switch({ Key key, this.value, this.onChanged })
final bool value;
final ValueChanged onChanged;
Widget build() {
return new _SwitchWrapper(
value: value,
onChanged: onChanged,
thumbColor: Theme.of(this).accentColor);
}
}
// This wrapper class exists only because Switch needs to be a Component in
// order to get an accent color from a Theme but Components do not know how to
// host RenderObjects.
class _SwitchWrapper extends LeafRenderObjectWrapper {
_SwitchWrapper({Key key, this.value, this.onChanged, this.thumbColor})
: super(key: key); : super(key: key);
final bool value; final bool value;
final ValueChanged onChanged; final ValueChanged onChanged;
final Color thumbColor;
_RenderSwitch get renderObject => super.renderObject; _RenderSwitch get renderObject => super.renderObject;
_RenderSwitch createNode() => new _RenderSwitch( _RenderSwitch createNode() => new _RenderSwitch(
value: value, thumbColor: thumbColor, onChanged: onChanged); value: value,
thumbColor: null,
onChanged: onChanged
);
void syncRenderObject(_SwitchWrapper old) { void syncRenderObject(Switch old) {
super.syncRenderObject(old); super.syncRenderObject(old);
renderObject.value = value; renderObject.value = value;
renderObject.onChanged = onChanged; renderObject.onChanged = onChanged;
renderObject.thumbColor = thumbColor; renderObject.thumbColor = Theme.of(this).accentColor;
} }
} }
class _RenderSwitch extends RenderToggleable { class _RenderSwitch extends RenderToggleable {
_RenderSwitch( _RenderSwitch({
{bool value, Color thumbColor: _kThumbOffColor, ValueChanged onChanged}) bool value,
: _thumbColor = thumbColor, Color thumbColor: _kThumbOffColor,
ValueChanged onChanged
}) : _thumbColor = thumbColor,
super(value: value, onChanged: onChanged, size: _kSwitchSize) {} super(value: value, onChanged: onChanged, size: _kSwitchSize) {}
Color _thumbColor; Color _thumbColor;
......
...@@ -23,8 +23,8 @@ class Theme extends Inherited { ...@@ -23,8 +23,8 @@ class Theme extends Inherited {
static final ThemeData _kFallbackTheme = new ThemeData.fallback(); static final ThemeData _kFallbackTheme = new ThemeData.fallback();
static ThemeData of(Component component) { static ThemeData of(Widget widget) {
Theme theme = component.inheritedOfType(Theme); Theme theme = widget.inheritedOfType(Theme);
return theme == null ? _kFallbackTheme : theme.data; return theme == null ? _kFallbackTheme : theme.data;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment