Commit 21fa2753 authored by Adam Barth's avatar Adam Barth

Switch thumb decorations don't load (#4209)

We weren't listening to the decorations to see when they loaded.

Fixes #4185
parent 239a717d
......@@ -195,19 +195,43 @@ class _RenderSwitch extends RenderToggleable {
set activeThumbDecoration(Decoration value) {
if (value == _activeThumbDecoration)
return;
_removeActiveThumbListenerIfNeeded();
_activeThumbDecoration = value;
_addActiveThumbListenerIfNeeded();
markNeedsPaint();
}
void _addActiveThumbListenerIfNeeded() {
if (attached && _activeThumbDecoration != null && _activeThumbDecoration.needsListeners)
_activeThumbDecoration.addChangeListener(markNeedsPaint);
}
void _removeActiveThumbListenerIfNeeded() {
if (attached && _activeThumbDecoration != null && _activeThumbDecoration.needsListeners)
_activeThumbDecoration.removeChangeListener(markNeedsPaint);
}
Decoration get inactiveThumbDecoration => _inactiveThumbDecoration;
Decoration _inactiveThumbDecoration;
set inactiveThumbDecoration(Decoration value) {
if (value == _inactiveThumbDecoration)
return;
_removeInactiveThumbListenerIfNeeded();
_inactiveThumbDecoration = value;
_addInactiveThumbListenerIfNeeded();
markNeedsPaint();
}
void _addInactiveThumbListenerIfNeeded() {
if (attached && _inactiveThumbDecoration != null && _inactiveThumbDecoration.needsListeners)
_inactiveThumbDecoration.addChangeListener(markNeedsPaint);
}
void _removeInactiveThumbListenerIfNeeded() {
if (attached && _inactiveThumbDecoration != null && _inactiveThumbDecoration.needsListeners)
_inactiveThumbDecoration.removeChangeListener(markNeedsPaint);
}
Color get activeTrackColor => _activeTrackColor;
Color _activeTrackColor;
set activeTrackColor(Color value) {
......@@ -228,6 +252,20 @@ class _RenderSwitch extends RenderToggleable {
markNeedsPaint();
}
@override
void attach(PipelineOwner owner) {
super.attach(owner);
_addInactiveThumbListenerIfNeeded();
_addActiveThumbListenerIfNeeded();
}
@override
void detach() {
_removeActiveThumbListenerIfNeeded();
_removeInactiveThumbListenerIfNeeded();
super.detach();
}
double get _trackInnerLength => size.width - 2.0 * kRadialReactionRadius;
HorizontalDragGestureRecognizer _drag;
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class TestBoxPainter extends BoxPainter {
@override
void paint(Canvas canvas, Rect rect) { }
}
class TestDecoration extends Decoration {
final List<VoidCallback> listeners = <VoidCallback>[];
@override
bool get needsListeners => true;
@override
void addChangeListener(VoidCallback listener) { listeners.add(listener); }
@override
void removeChangeListener(VoidCallback listener) { listeners.remove(listener); }
@override
BoxPainter createBoxPainter() => new TestBoxPainter();
}
void main() {
testWidgets('Switch can toggle on tap', (WidgetTester tester) async {
Key switchKey = new UniqueKey();
bool value = false;
await tester.pumpWidget(
new StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return new Material(
child: new Center(
child: new Switch(
key: switchKey,
value: value,
onChanged: (bool newValue) {
setState(() {
value = newValue;
});
}
)
)
);
}
)
);
expect(value, isFalse);
await tester.tap(find.byKey(switchKey));
expect(value, isTrue);
});
testWidgets('Switch listens to decorations', (WidgetTester tester) async {
TestDecoration activeDecoration = new TestDecoration();
TestDecoration inactiveDecoration = new TestDecoration();
Widget build(TestDecoration activeDecoration, TestDecoration inactiveDecoration) {
return new Material(
child: new Center(
child: new Switch(
value: false,
activeThumbDecoration: activeDecoration,
inactiveThumbDecoration: inactiveDecoration
)
)
);
}
await tester.pumpWidget(build(activeDecoration, inactiveDecoration));
expect(activeDecoration.listeners.length, 1);
expect(inactiveDecoration.listeners.length, 1);
await tester.pumpWidget(build(activeDecoration, null));
expect(activeDecoration.listeners.length, 1);
expect(inactiveDecoration.listeners.length, 0);
await tester.pumpWidget(new Container(key: new UniqueKey()));
expect(activeDecoration.listeners.length, 0);
expect(inactiveDecoration.listeners.length, 0);
});
}
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