Improve SliverBlockDelegate.estimateScrollOffsetExtent (#7689)

We now report an accurate estimate when the lastIndex is the final index
in the list.

Also, we now project forward from the lastIndex using the extent of the
reified children instead of trying to project forward from index 0.

This new algorithm leads to estimation error as you reach the end of a
list, avoiding some ballistic overscrolls.
This commit is contained in:
Adam Barth
2017-01-26 22:59:10 -08:00
committed by GitHub
parent a67c79a1c7
commit 9c288040ce
2 changed files with 19 additions and 1 deletions

View File

@@ -78,7 +78,13 @@ abstract class SliverBlockDelegate {
double leadingScrollOffset,
double trailingScrollOffset,
) {
return childCount * (trailingScrollOffset - leadingScrollOffset) / (lastIndex - firstIndex + 1);
final int childCount = this.childCount;
if (lastIndex == childCount - 1)
return trailingScrollOffset;
final int reifiedCount = lastIndex - firstIndex + 1;
final double averageExtent = (trailingScrollOffset - leadingScrollOffset) / reifiedCount;
final int remainingCount = childCount - lastIndex - 1;
return trailingScrollOffset + averageExtent * remainingCount;
}
}

View File

@@ -119,4 +119,16 @@ void main() {
await tester.pumpWidget(buildBlock());
expect(key.currentState.scrollOffset, 0.0);
});
testWidgets('SliverBlockChildListDelegate.estimateScrollOffsetExtent hits end', (WidgetTester tester) async {
SliverBlockChildListDelegate delegate = new SliverBlockChildListDelegate(<Widget>[
new Container(),
new Container(),
new Container(),
new Container(),
new Container(),
]);
expect(delegate.estimateScrollOffsetExtent(3, 4, 25.0, 26.0), equals(26.0));
});
}