From f2809423cd2a5a211361a01bdc2e0331940d9ec5 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Thu, 13 Jul 2017 09:16:03 +0200 Subject: [PATCH] fix examples/layers/widgets/gestures.dart (#11138) * fix examples/layers/widgets/gestures.dart * use a list of colors to avoid error prone in elseif * address review comments * add tests * keep index to lower complexity * address review comments --- examples/layers/test/gestures_test.dart | 35 ++++++++++++ examples/layers/widgets/gestures.dart | 71 +++++++++++-------------- 2 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 examples/layers/test/gestures_test.dart diff --git a/examples/layers/test/gestures_test.dart b/examples/layers/test/gestures_test.dart new file mode 100644 index 00000000000..3d82e2aaa48 --- /dev/null +++ b/examples/layers/test/gestures_test.dart @@ -0,0 +1,35 @@ +// Copyright 2017 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'; + +import '../widgets/gestures.dart'; + +void main() { + testWidgets('Tap on center change color', (WidgetTester tester) async { + await tester.pumpWidget(new GestureDemo()); + final Finder finder = find.byType(GestureDemo); + + MaterialColor getSwatch() => tester.state(finder).swatch; + Future tap() async { + final Offset topLeft = tester.getTopLeft(finder); + await tester.tapAt(tester.getSize(finder).center(topLeft)); + await tester.pump(const Duration(seconds: 1)); + } + + // initial swatch + expect(getSwatch(), GestureDemoState.kSwatches[0]); + + // every tap change swatch + for (int i = 1; i < GestureDemoState.kSwatches.length; i++) { + await tap(); + expect(getSwatch(), GestureDemoState.kSwatches[i]); + } + + // tap on last swatch display first swatch + await tap(); + expect(getSwatch(), GestureDemoState.kSwatches[0]); + }); +} diff --git a/examples/layers/widgets/gestures.dart b/examples/layers/widgets/gestures.dart index 40a62a7eb46..3b97be7fd97 100644 --- a/examples/layers/widgets/gestures.dart +++ b/examples/layers/widgets/gestures.dart @@ -56,10 +56,10 @@ class _GesturePainter extends CustomPainter { class GestureDemo extends StatefulWidget { @override - _GestureDemoState createState() => new _GestureDemoState(); + GestureDemoState createState() => new GestureDemoState(); } -class _GestureDemoState extends State { +class GestureDemoState extends State { Offset _startingFocalPoint; @@ -69,7 +69,29 @@ class _GestureDemoState extends State { double _previousZoom; double _zoom = 1.0; - MaterialColor _swatch = Colors.blue; + static const List kSwatches = const [ + Colors.red, + Colors.pink, + Colors.purple, + Colors.deepPurple, + Colors.indigo, + Colors.blue, + Colors.lightBlue, + Colors.cyan, + Colors.green, + Colors.lightGreen, + Colors.lime, + Colors.yellow, + Colors.amber, + Colors.orange, + Colors.deepOrange, + Colors.brown, + Colors.grey, + Colors.blueGrey, + ]; + int _swatchIndex = 0; + MaterialColor _swatch = kSwatches.first; + MaterialColor get swatch => _swatch; bool _forward = true; bool _scaleEnabled = true; @@ -104,42 +126,10 @@ class _GestureDemoState extends State { void _handleColorChange() { setState(() { - if (_swatch == Colors.blueGrey) - _swatch = Colors.red; - else if (_swatch == Colors.red) - _swatch = Colors.pink; - else if (_swatch == Colors.pink) - _swatch = Colors.purple; - else if (_swatch == Colors.purple) - _swatch = Colors.deepPurple; - else if (_swatch == Colors.deepPurple) - _swatch = Colors.indigo; - else if (_swatch == Colors.indigo) - _swatch = Colors.blue; - else if (_swatch == Colors.blue) - _swatch = Colors.lightBlue; - else if (_swatch == Colors.lightBlue) - _swatch = Colors.cyan; - else if (_swatch == Colors.teal) - _swatch = Colors.green; - else if (_swatch == Colors.green) - _swatch = Colors.lightGreen; - else if (_swatch == Colors.lightGreen) - _swatch = Colors.lime; - else if (_swatch == Colors.lime) - _swatch = Colors.yellow; - else if (_swatch == Colors.yellow) - _swatch = Colors.amber; - else if (_swatch == Colors.amber) - _swatch = Colors.orange; - else if (_swatch == Colors.orange) - _swatch = Colors.deepOrange; - else if (_swatch == Colors.deepOrange) - _swatch = Colors.brown; - else if (_swatch == Colors.brown) - _swatch = Colors.grey; - else if (_swatch == Colors.grey) - _swatch = Colors.blueGrey; + _swatchIndex += 1; + if (_swatchIndex == kSwatches.length) + _swatchIndex = 0; + _swatch = kSwatches[_swatchIndex]; }); } @@ -152,6 +142,7 @@ class _GestureDemoState extends State { @override Widget build(BuildContext context) { return new Stack( + fit: StackFit.expand, children: [ new GestureDetector( onScaleStart: _scaleEnabled ? _handleScaleStart : null, @@ -163,7 +154,7 @@ class _GestureDemoState extends State { painter: new _GesturePainter( zoom: _zoom, offset: _offset, - swatch: _swatch, + swatch: swatch, forward: _forward, scaleEnabled: _scaleEnabled, tapEnabled: _tapEnabled,