From 21fa2753d8f6481f66bc2439eeb41dffc2d999ce Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 26 May 2016 11:26:11 -0700 Subject: [PATCH] Switch thumb decorations don't load (#4209) We weren't listening to the decorations to see when they loaded. Fixes #4185 --- packages/flutter/lib/src/material/switch.dart | 38 ++++++++ .../flutter/test/material/switch_test.dart | 90 +++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 packages/flutter/test/material/switch_test.dart diff --git a/packages/flutter/lib/src/material/switch.dart b/packages/flutter/lib/src/material/switch.dart index e02e5f596b..ec71410a4c 100644 --- a/packages/flutter/lib/src/material/switch.dart +++ b/packages/flutter/lib/src/material/switch.dart @@ -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; diff --git a/packages/flutter/test/material/switch_test.dart b/packages/flutter/test/material/switch_test.dart new file mode 100644 index 0000000000..50677def43 --- /dev/null +++ b/packages/flutter/test/material/switch_test.dart @@ -0,0 +1,90 @@ +// 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 listeners = []; + + @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); + }); +}