Commit cab7ecb8 authored by Dwayne Slater's avatar Dwayne Slater Committed by Michael Goderbauer

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.
parent 936dea28
......@@ -427,7 +427,7 @@ class ClampingScrollPhysics extends ScrollPhysics {
return ScrollSpringSimulation(
spring,
position.pixels,
position.maxScrollExtent,
end,
math.min(0.0, velocity),
tolerance: tolerance
);
......
......@@ -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<void> 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);
});
}
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