From 112f2cc37be1c6d4b611b3236ad937615470bc7e Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Wed, 20 Apr 2016 11:02:20 -0700 Subject: [PATCH] Reset _simulation at the end of a fling (#3435) Also a bit of code cleanup. The key part of this patch is the addition in `_endScroll` to reset `_simulation`. It seems like this was the one place where it's possible for us to end the animation but not reset our state. Since we assert that are state is coherent, we were hitting asserts when a fling finished and then you interacted with the widget again. --- .../src/animation/animation_controller.dart | 4 +- .../flutter/lib/src/widgets/scrollable.dart | 29 ++++++++------ .../test/widget/scroll_interaction_test.dart | 39 +++++++++++++++++++ .../flutter_test/lib/src/widget_tester.dart | 2 +- 4 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 packages/flutter/test/widget/scroll_interaction_test.dart diff --git a/packages/flutter/lib/src/animation/animation_controller.dart b/packages/flutter/lib/src/animation/animation_controller.dart index 2cb9222eef..0d42ec2cc1 100644 --- a/packages/flutter/lib/src/animation/animation_controller.dart +++ b/packages/flutter/lib/src/animation/animation_controller.dart @@ -126,7 +126,7 @@ class AnimationController extends Animation /// The amount of time that has passed between the time the animation started and the most recent tick of the animation. /// - /// If the controller is not animating, the last elapsed duration is null; + /// If the controller is not animating, the last elapsed duration is null. Duration get lastElapsedDuration => _lastElapsedDuration; Duration _lastElapsedDuration; @@ -212,7 +212,7 @@ class AnimationController extends Animation assert(simulation != null); assert(!isAnimating); _simulation = simulation; - _lastElapsedDuration = const Duration(); + _lastElapsedDuration = Duration.ZERO; _value = simulation.x(0.0).clamp(lowerBound, upperBound); Future result = _ticker.start(); _checkStatusChanged(); diff --git a/packages/flutter/lib/src/widgets/scrollable.dart b/packages/flutter/lib/src/widgets/scrollable.dart index 4f521955af..12541c4ee4 100644 --- a/packages/flutter/lib/src/widgets/scrollable.dart +++ b/packages/flutter/lib/src/widgets/scrollable.dart @@ -223,17 +223,17 @@ abstract class ScrollableState extends State { @override void initState() { super.initState(); - _controller = new AnimationController.unbounded()..addListener(_handleAnimationChanged); + _controller = new AnimationController.unbounded() + ..addListener(_handleAnimationChanged); _scrollOffset = PageStorage.of(context)?.readState(context) ?? config.initialScrollOffset ?? 0.0; } - Simulation _simulation; + Simulation _simulation; // if we're flinging, then this is the animation with which we're doing it AnimationController _controller; @override void dispose() { - _simulation = null; - _controller.stop(); + _stop(); super.dispose(); } @@ -360,8 +360,7 @@ abstract class ScrollableState extends State { return new Future.value(); if (duration == null) { - _simulation = null; - _controller.stop(); + _stop(); _setScrollOffset(newScrollOffset); return new Future.value(); } @@ -371,8 +370,7 @@ abstract class ScrollableState extends State { } Future _animateTo(double newScrollOffset, Duration duration, Curve curve) { - _simulation = null; - _controller.stop(); + _stop(); _controller.value = scrollOffset; _startScroll(); return _controller.animateTo(newScrollOffset, duration: duration, curve: curve).then(_endScroll); @@ -388,6 +386,7 @@ abstract class ScrollableState extends State { /// If there are no in-progress scrolling physics, this function scrolls to /// the given offset instead. void didUpdateScrollBehavior(double newScrollOffset) { + assert(_controller.isAnimating || _simulation == null); if (_numberOfInProgressScrolls > 0) { if (_simulation != null) { double dx = _simulation.dx(_controller.lastElapsedDuration.inMicroseconds / Duration.MICROSECONDS_PER_SECOND); @@ -420,8 +419,7 @@ abstract class ScrollableState extends State { } Future _startToEndAnimation(double scrollVelocity) { - _simulation = null; - _controller.stop(); + _stop(); _simulation = _createSnapSimulation(scrollVelocity) ?? _createFlingSimulation(scrollVelocity); if (_simulation == null) return new Future.value(); @@ -498,8 +496,13 @@ abstract class ScrollableState extends State { } void _handleDragDown(_) { - _simulation = null; + _stop(); + } + + void _stop() { + assert(_controller.isAnimating || _simulation == null); _controller.stop(); + _simulation = null; } void _handleDragStart(_) { @@ -533,8 +536,10 @@ abstract class ScrollableState extends State { Null _endScroll([Null _]) { _numberOfInProgressScrolls -= 1; - if (_numberOfInProgressScrolls == 0) + if (_numberOfInProgressScrolls == 0) { + _simulation = null; dispatchOnScrollEnd(); + } return null; } diff --git a/packages/flutter/test/widget/scroll_interaction_test.dart b/packages/flutter/test/widget/scroll_interaction_test.dart new file mode 100644 index 0000000000..e81dcff141 --- /dev/null +++ b/packages/flutter/test/widget/scroll_interaction_test.dart @@ -0,0 +1,39 @@ +// Copyright 2015 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_test/flutter_test.dart'; +import 'package:flutter/widgets.dart'; +import 'package:test/test.dart'; + +void main() { + test('Scroll flings twice in a row does not crash', () { + testWidgets((WidgetTester tester) { + tester.pumpWidget(new Block( + children: [ + new Container(height: 100000.0) + ] + )); + + ScrollableState scrollable = + tester.stateOf/*>*/(find.byType(ScrollableViewport)); + + expect(scrollable.scrollOffset, equals(0.0)); + + tester.flingFrom(new Point(200.0, 300.0), new Offset(0.0, -200.0), 500.0); + tester.pump(); + tester.pump(const Duration(seconds: 5)); + + expect(scrollable.scrollOffset, greaterThan(0.0)); + + double oldOffset = scrollable.scrollOffset; + + tester.flingFrom(new Point(200.0, 300.0), new Offset(0.0, -200.0), 500.0); + tester.pump(); + tester.pump(const Duration(seconds: 5)); + + expect(scrollable.scrollOffset, greaterThan(oldOffset)); + + }); + }); +} diff --git a/packages/flutter_test/lib/src/widget_tester.dart b/packages/flutter_test/lib/src/widget_tester.dart index 1df73befeb..26ebbf2cd2 100644 --- a/packages/flutter_test/lib/src/widget_tester.dart +++ b/packages/flutter_test/lib/src/widget_tester.dart @@ -126,7 +126,7 @@ class WidgetTester { } /// Finds the first state object, searching in the depth-first traversal order. - State stateOf(Finder finder) { + State/*=T*/ stateOf/**/(Finder finder) { Element element = finder.findFirst(this); Widget widget = element.widget;