From 9c288040ce11c804986d00dd93dc22704806a539 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 26 Jan 2017 22:59:10 -0800 Subject: [PATCH] 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. --- packages/flutter/lib/src/widgets/block.dart | 8 +++++++- packages/flutter/test/widgets/block_test.dart | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/block.dart b/packages/flutter/lib/src/widgets/block.dart index 27b6431dcd..8957367acc 100644 --- a/packages/flutter/lib/src/widgets/block.dart +++ b/packages/flutter/lib/src/widgets/block.dart @@ -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; } } diff --git a/packages/flutter/test/widgets/block_test.dart b/packages/flutter/test/widgets/block_test.dart index 75c5d2cf0b..16f9131a0d 100644 --- a/packages/flutter/test/widgets/block_test.dart +++ b/packages/flutter/test/widgets/block_test.dart @@ -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([ + new Container(), + new Container(), + new Container(), + new Container(), + new Container(), + ]); + + expect(delegate.estimateScrollOffsetExtent(3, 4, 25.0, 26.0), equals(26.0)); + }); }