Skip to content

Commit 2b105ac

Browse files
authored
Fix ScrollPosition overscroll precision error (#127321)
## Description This PR fixes a precision error in ~~`ClampingScrollPhysics`~~ `ScrollPosition` that leads to `StretchingOverscrollIndicator` stretching its content unexpectedly in some devices (see flutter/flutter#126561 where this is visible in `TabBarView` and the test added in this PR where it reproduces with a `PageView`). ~~This PR also contains a change to `nested_scroll_view.dart` because the first change (the one in `ClampingScrollPhysics`) breaks the precision error test added by flutter/flutter#87801 ## Related Issue Fixes flutter/flutter#126561 ## Tests Adds 1 test.
1 parent 5451ea6 commit 2b105ac

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎packages/flutter/lib/src/widgets/scroll_position.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
368368
notifyListeners();
369369
didUpdateScrollPositionBy(pixels - oldPixels);
370370
}
371-
if (overscroll != 0.0) {
371+
if (overscroll.abs() > precisionErrorTolerance) {
372372
didOverscrollBy(overscroll);
373373
return overscroll;
374374
}

‎packages/flutter/test/widgets/page_view_test.dart‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,4 +1203,46 @@ void main() {
12031203
await tester.pump();
12041204
expect(tester.takeException(), isNull);
12051205
});
1206+
1207+
testWidgets('PageView content should not be stretched on precision error', (WidgetTester tester) async {
1208+
// Regression test for https://github.com/flutter/flutter/issues/126561.
1209+
final PageController controller = PageController();
1210+
1211+
const double pixel6EmulatorWidth = 411.42857142857144;
1212+
1213+
await tester.pumpWidget(MaterialApp(
1214+
theme: ThemeData(useMaterial3: true),
1215+
home: Center(
1216+
child: SizedBox(
1217+
width: pixel6EmulatorWidth,
1218+
child: PageView(
1219+
controller: controller,
1220+
physics: const PageScrollPhysics().applyTo(const ClampingScrollPhysics()),
1221+
children: const <Widget>[
1222+
Center(child: Text('First Page')),
1223+
Center(child: Text('Second Page')),
1224+
Center(child: Text('Third Page')),
1225+
],
1226+
),
1227+
),
1228+
),
1229+
));
1230+
1231+
controller.animateToPage(2, duration: const Duration(milliseconds: 300), curve: Curves.ease);
1232+
await tester.pumpAndSettle();
1233+
1234+
final Finder transformFinder = find.descendant(of: find.byType(PageView), matching: find.byType(Transform));
1235+
expect(transformFinder, findsOneWidget);
1236+
1237+
// Get the Transform widget that stretches the PageView.
1238+
final Transform transform = tester.firstWidget<Transform>(
1239+
find.descendant(
1240+
of: find.byType(PageView),
1241+
matching: find.byType(Transform),
1242+
),
1243+
);
1244+
1245+
// Check the stretch factor in the first element of the transform matrix.
1246+
expect(transform.transform.storage.first, 1.0);
1247+
});
12061248
}

0 commit comments

Comments
 (0)