Commit f2809423 authored by Alexandre Ardhuin's avatar Alexandre Ardhuin Committed by GitHub

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
parent 92750833
// 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<GestureDemoState>(finder).swatch;
Future<Null> 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]);
});
}
...@@ -56,10 +56,10 @@ class _GesturePainter extends CustomPainter { ...@@ -56,10 +56,10 @@ class _GesturePainter extends CustomPainter {
class GestureDemo extends StatefulWidget { class GestureDemo extends StatefulWidget {
@override @override
_GestureDemoState createState() => new _GestureDemoState(); GestureDemoState createState() => new GestureDemoState();
} }
class _GestureDemoState extends State<GestureDemo> { class GestureDemoState extends State<GestureDemo> {
Offset _startingFocalPoint; Offset _startingFocalPoint;
...@@ -69,7 +69,29 @@ class _GestureDemoState extends State<GestureDemo> { ...@@ -69,7 +69,29 @@ class _GestureDemoState extends State<GestureDemo> {
double _previousZoom; double _previousZoom;
double _zoom = 1.0; double _zoom = 1.0;
MaterialColor _swatch = Colors.blue; static const List<MaterialColor> kSwatches = const <MaterialColor>[
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 _forward = true;
bool _scaleEnabled = true; bool _scaleEnabled = true;
...@@ -104,42 +126,10 @@ class _GestureDemoState extends State<GestureDemo> { ...@@ -104,42 +126,10 @@ class _GestureDemoState extends State<GestureDemo> {
void _handleColorChange() { void _handleColorChange() {
setState(() { setState(() {
if (_swatch == Colors.blueGrey) _swatchIndex += 1;
_swatch = Colors.red; if (_swatchIndex == kSwatches.length)
else if (_swatch == Colors.red) _swatchIndex = 0;
_swatch = Colors.pink; _swatch = kSwatches[_swatchIndex];
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;
}); });
} }
...@@ -152,6 +142,7 @@ class _GestureDemoState extends State<GestureDemo> { ...@@ -152,6 +142,7 @@ class _GestureDemoState extends State<GestureDemo> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new Stack( return new Stack(
fit: StackFit.expand,
children: <Widget>[ children: <Widget>[
new GestureDetector( new GestureDetector(
onScaleStart: _scaleEnabled ? _handleScaleStart : null, onScaleStart: _scaleEnabled ? _handleScaleStart : null,
...@@ -163,7 +154,7 @@ class _GestureDemoState extends State<GestureDemo> { ...@@ -163,7 +154,7 @@ class _GestureDemoState extends State<GestureDemo> {
painter: new _GesturePainter( painter: new _GesturePainter(
zoom: _zoom, zoom: _zoom,
offset: _offset, offset: _offset,
swatch: _swatch, swatch: swatch,
forward: _forward, forward: _forward,
scaleEnabled: _scaleEnabled, scaleEnabled: _scaleEnabled,
tapEnabled: _tapEnabled, tapEnabled: _tapEnabled,
......
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