Fix SliverList scrollOffsetCorrection 0 case (#62615)

This commit is contained in:
Kate Lovett 2020-08-06 16:11:16 -07:00 committed by GitHub
parent 4680ff3a79
commit 79146fd0a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 6 deletions

View File

@ -169,20 +169,26 @@ class RenderSliverList extends RenderSliverMultiBoxAdaptor {
// If the scroll offset is at zero, we should make sure we are
// actually at the beginning of the list.
if (scrollOffset < precisionErrorTolerance) {
if (indexOf(firstChild) > 0) {
// We iterate from the firstChild in case the leading child has a 0 paint
// extent.
while (indexOf(firstChild) > 0) {
final double earliestScrollOffset = childScrollOffset(firstChild);
// We correct one child at a time. If there are more children before
// the earliestUsefulChild, we will correct it once the scroll offset
// reach zero again.
// reaches zero again.
earliestUsefulChild = insertAndLayoutLeadingChild(childConstraints, parentUsesSize: true);
assert(earliestUsefulChild != null);
final double firstChildScrollOffset = earliestScrollOffset - paintExtentOf(firstChild);
geometry = SliverGeometry(
scrollOffsetCorrection: -firstChildScrollOffset,
);
final SliverMultiBoxAdaptorParentData childParentData = firstChild.parentData as SliverMultiBoxAdaptorParentData;
childParentData.layoutOffset = 0.0;
return;
// We only need to correct if the leading child actually has a
// paint extent.
if (firstChildScrollOffset < -precisionErrorTolerance) {
geometry = SliverGeometry(
scrollOffsetCorrection: -firstChildScrollOffset,
);
return;
}
}
}

View File

@ -814,6 +814,31 @@ void main() {
expect(events, equals(<String>['tap']));
});
});
testWidgets('SliverList handles 0 scrollOffsetCorrection', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/62198
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: CustomScrollView(
physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
slivers: <Widget>[
SliverList(
delegate: SliverChildListDelegate(
const <Widget>[
SizedBox.shrink(),
Text('index 1'),
Text('index 2'),
]
),
),
],
)
),
));
await tester.fling(find.byType(Scrollable), const Offset(0.0, -500.0), 10000.0);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
});
}
bool isRight(Offset a, Offset b) => b.dx > a.dx;