From cab7ecb8e503cd1c2c198b3434be04a1d3f72f48 Mon Sep 17 00:00:00 2001 From: Dwayne Slater Date: Wed, 17 Oct 2018 04:49:27 -0400 Subject: [PATCH] Fix out of range spring simulation in ClampingScrollSimulation (#22394) If the scroll is out of range for any reason (for instance, changing scroll physics or manually messing with scroll position), the spring simulation causes the scroll position to slingshot towards the end. Also, before this change, `end` was unused. --- .../lib/src/widgets/scroll_physics.dart | 2 +- .../test/widgets/clamp_overscrolls_test.dart | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/scroll_physics.dart b/packages/flutter/lib/src/widgets/scroll_physics.dart index 3f19a0e4cf..7fe72d2aa4 100644 --- a/packages/flutter/lib/src/widgets/scroll_physics.dart +++ b/packages/flutter/lib/src/widgets/scroll_physics.dart @@ -427,7 +427,7 @@ class ClampingScrollPhysics extends ScrollPhysics { return ScrollSpringSimulation( spring, position.pixels, - position.maxScrollExtent, + end, math.min(0.0, velocity), tolerance: tolerance ); diff --git a/packages/flutter/test/widgets/clamp_overscrolls_test.dart b/packages/flutter/test/widgets/clamp_overscrolls_test.dart index 83f8b426ee..2d97aa3a62 100644 --- a/packages/flutter/test/widgets/clamp_overscrolls_test.dart +++ b/packages/flutter/test/widgets/clamp_overscrolls_test.dart @@ -11,10 +11,11 @@ import 'package:flutter/widgets.dart'; // The top of the bottom widget is at 550 (the top of the top widget // is at 0). The top of the bottom widget is 500 when it has been // scrolled completely into view. -Widget buildFrame(ScrollPhysics physics) { +Widget buildFrame(ScrollPhysics physics, {ScrollController scrollController}) { return SingleChildScrollView( key: UniqueKey(), physics: physics, + controller: scrollController, child: SizedBox( height: 650.0, child: Column( @@ -89,4 +90,19 @@ void main() { await tester.pump(); expect(scrollable.position.pixels, equals(50.0)); }); + + testWidgets('ClampingScrollPhysics handles out of bounds ScrollPosition', (WidgetTester tester) async { + Future testOutOfBounds(ScrollPhysics physics, double initialOffset, double expectedOffset) async { + final ScrollController scrollController = ScrollController(initialScrollOffset: initialOffset); + await tester.pumpWidget(buildFrame(physics, scrollController: scrollController)); + final ScrollableState scrollable = tester.state(find.byType(Scrollable)); + + expect(scrollable.position.pixels, equals(initialOffset)); + await tester.pump(const Duration(seconds: 1)); // Allow overscroll to settle + expect(scrollable.position.pixels, equals(expectedOffset)); + } + + await testOutOfBounds(const ClampingScrollPhysics(), -400.0, 0.0); + await testOutOfBounds(const ClampingScrollPhysics(), 800.0, 50.0); + }); }