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 {
final TextStyle style;
static TextStyle of(Component component) {
DefaultTextStyle result = component.inheritedOfType(DefaultTextStyle);
static TextStyle of(Widget widget) {
DefaultTextStyle result = widget.inheritedOfType(DefaultTextStyle);
return result == null ? null : result.style;
}
......
......@@ -272,6 +272,27 @@ abstract class Widget {
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() {
_renderObject = null;
setParent(null);
......@@ -453,10 +474,9 @@ abstract class Inherited extends TagNode {
void notifyDescendants() {
final Type ourRuntimeType = runtimeType;
void notifyChildren(Widget child) {
if (child is Component &&
child._dependencies != null &&
if (child._dependencies != null &&
child._dependencies.contains(ourRuntimeType))
child._dependenciesChanged();
child.dependenciesChanged();
if (child.runtimeType != ourRuntimeType)
child.walkChildren(notifyChildren);
}
......@@ -608,17 +628,7 @@ abstract class Component extends Widget {
_built.detachRoot();
}
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() {
void dependenciesChanged() {
// called by Inherited.sync()
_scheduleBuild();
}
......@@ -937,6 +947,11 @@ abstract class RenderObjectWrapper extends Widget {
}
}
void dependenciesChanged() {
// called by Inherited.sync()
syncRenderObject(this);
}
void remove() {
assert(renderObject != null);
_nodeMap.remove(renderObject);
......
......@@ -30,47 +30,34 @@ const Duration _kCheckDuration = const Duration(milliseconds: 200);
const Size _kSwitchSize = const Size(_kSwitchWidth + 2.0, _kSwitchHeight + 2.0);
const double _kReactionRadius = _kSwitchWidth / 2.0;
class Switch extends Component {
Switch({Key key, this.value, this.onChanged}) : super(key: key);
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})
class Switch extends LeafRenderObjectWrapper {
Switch({ Key key, this.value, this.onChanged })
: super(key: key);
final bool value;
final ValueChanged onChanged;
final Color thumbColor;
_RenderSwitch get renderObject => super.renderObject;
_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);
renderObject.value = value;
renderObject.onChanged = onChanged;
renderObject.thumbColor = thumbColor;
renderObject.thumbColor = Theme.of(this).accentColor;
}
}
class _RenderSwitch extends RenderToggleable {
_RenderSwitch(
{bool value, Color thumbColor: _kThumbOffColor, ValueChanged onChanged})
: _thumbColor = thumbColor,
_RenderSwitch({
bool value,
Color thumbColor: _kThumbOffColor,
ValueChanged onChanged
}) : _thumbColor = thumbColor,
super(value: value, onChanged: onChanged, size: _kSwitchSize) {}
Color _thumbColor;
......
......@@ -23,8 +23,8 @@ class Theme extends Inherited {
static final ThemeData _kFallbackTheme = new ThemeData.fallback();
static ThemeData of(Component component) {
Theme theme = component.inheritedOfType(Theme);
static ThemeData of(Widget widget) {
Theme theme = widget.inheritedOfType(Theme);
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