From 517f8dc2795aa797aa673e7eb06a3b9fd7fecc9d Mon Sep 17 00:00:00 2001 From: matthew-carroll Date: Wed, 9 Jan 2019 12:34:54 -0800 Subject: [PATCH] Add consumedScrollExtent to SliverConstraints as reported by Viewport (#24551) --- .../flutter/lib/src/rendering/sliver.dart | 27 +++++++ .../flutter/lib/src/rendering/viewport.dart | 15 ++-- .../test/rendering/slivers_helpers_test.dart | 48 +++++++++--- .../flutter/test/rendering/slivers_test.dart | 75 +++++++++++++++++++ .../test/widgets/sliver_constraints_test.dart | 39 ++++++++++ 5 files changed, 187 insertions(+), 17 deletions(-) create mode 100644 packages/flutter/test/widgets/sliver_constraints_test.dart diff --git a/packages/flutter/lib/src/rendering/sliver.dart b/packages/flutter/lib/src/rendering/sliver.dart index 067d5615f7..3e93e2b40a 100644 --- a/packages/flutter/lib/src/rendering/sliver.dart +++ b/packages/flutter/lib/src/rendering/sliver.dart @@ -99,6 +99,7 @@ class SliverConstraints extends Constraints { @required this.growthDirection, @required this.userScrollDirection, @required this.scrollOffset, + @required this.precedingScrollExtent, @required this.overlap, @required this.remainingPaintExtent, @required this.crossAxisExtent, @@ -110,6 +111,7 @@ class SliverConstraints extends Constraints { assert(growthDirection != null), assert(userScrollDirection != null), assert(scrollOffset != null), + assert(precedingScrollExtent != null), assert(overlap != null), assert(remainingPaintExtent != null), assert(crossAxisExtent != null), @@ -125,6 +127,7 @@ class SliverConstraints extends Constraints { GrowthDirection growthDirection, ScrollDirection userScrollDirection, double scrollOffset, + double precedingScrollExtent, double overlap, double remainingPaintExtent, double crossAxisExtent, @@ -138,6 +141,7 @@ class SliverConstraints extends Constraints { growthDirection: growthDirection ?? this.growthDirection, userScrollDirection: userScrollDirection ?? this.userScrollDirection, scrollOffset: scrollOffset ?? this.scrollOffset, + precedingScrollExtent: precedingScrollExtent ?? this.precedingScrollExtent, overlap: overlap ?? this.overlap, remainingPaintExtent: remainingPaintExtent ?? this.remainingPaintExtent, crossAxisExtent: crossAxisExtent ?? this.crossAxisExtent, @@ -225,6 +229,29 @@ class SliverConstraints extends Constraints { /// contents depends on the [growthDirection]. final double scrollOffset; + /// The scroll distance that has been consumed by all [Sliver]s that came + /// before this [Sliver]. + /// + /// # Edge Cases + /// + /// [Sliver]s often lazily create their internal content as layout occurs, + /// e.g., [SliverList]. In this case, when [Sliver]s exceed the viewport, + /// their children are built lazily, and the [Sliver] does not have enough + /// information to estimate its total extent, [precedingScrollExtent] will be + /// [double.infinity] for all [Sliver]s that appear after the lazily + /// constructed child. This is because a total [scrollExtent] cannot be + /// calculated unless all inner children have been created and sized, or the + /// number of children and estimated extents are provided. The infinite + /// [scrollExtent] will become finite as soon as enough information is + /// available to estimate the overall extent of all children within the given + /// [Sliver]. + /// + /// [Sliver]s may legitimately be infinite, meaning that they can scroll + /// content forever without reaching the end. For any [Sliver]s that appear + /// after the infinite [Sliver], the [precedingScrollExtent] will be + /// [double.infinity]. + final double precedingScrollExtent; + /// The number of pixels from where the pixels corresponding to the /// [scrollOffset] will be painted up to the first pixel that has not yet been /// painted on by an earlier sliver, in the [axisDirection]. diff --git a/packages/flutter/lib/src/rendering/viewport.dart b/packages/flutter/lib/src/rendering/viewport.dart index 30abc88db9..5932625703 100644 --- a/packages/flutter/lib/src/rendering/viewport.dart +++ b/packages/flutter/lib/src/rendering/viewport.dart @@ -388,17 +388,18 @@ abstract class RenderViewportBase= corectedCacheOrigin.abs()); - assert(corectedCacheOrigin <= 0.0); + assert(sliverScrollOffset >= correctedCacheOrigin.abs()); + assert(correctedCacheOrigin <= 0.0); assert(sliverScrollOffset >= 0.0); assert(cacheExtentCorrection <= 0.0); @@ -407,13 +408,14 @@ abstract class RenderViewportBase[ + sliver, + ], + ); + layout(root); + + expect(sliver.constraints.precedingScrollExtent, 0.0); + }); + + test('precedingScrollExtent accumulates over multiple Slivers', () { + const double viewportWidth = 800.0; + final RenderSliver sliver1 = RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(viewportWidth, 150.0)), + ); + final RenderSliver sliver2 = RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(viewportWidth, 150.0)), + ); + final RenderSliver sliver3 = RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(viewportWidth, 150.0)), + ); + final RenderViewport root = RenderViewport( + axisDirection: AxisDirection.down, + crossAxisDirection: AxisDirection.right, + offset: ViewportOffset.zero(), + children: [ + sliver1, + sliver2, + sliver3, + ], + ); + layout(root); + + // The 3rd Sliver comes after 300.0px of preceding scroll extent by first 2 Slivers. + expect(sliver3.constraints.precedingScrollExtent, 300.0); + }); + + test('precedingScrollExtent is not impacted by scrollOffset', () { + const double viewportWidth = 800.0; + final RenderSliver sliver1 = RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(viewportWidth, 150.0)), + ); + final RenderSliver sliver2 = RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(viewportWidth, 150.0)), + ); + final RenderSliver sliver3 = RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(viewportWidth, 150.0)), + ); + final RenderViewport root = RenderViewport( + axisDirection: AxisDirection.down, + crossAxisDirection: AxisDirection.right, + offset: ViewportOffset.fixed(100.0), + children: [ + sliver1, + sliver2, + sliver3, + ], + ); + layout(root); + + // The 3rd Sliver comes after 300.0px of preceding scroll extent by first 2 Slivers. + // In this test a ViewportOffset is applied to simulate a scrollOffset. That + // offset is not expected to impact the precedingScrollExtent. + expect(sliver3.constraints.precedingScrollExtent, 300.0); + }); } diff --git a/packages/flutter/test/widgets/sliver_constraints_test.dart b/packages/flutter/test/widgets/sliver_constraints_test.dart new file mode 100644 index 0000000000..f1f0efcc1b --- /dev/null +++ b/packages/flutter/test/widgets/sliver_constraints_test.dart @@ -0,0 +1,39 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/widgets.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('precedingScrollExtent is reported as infinity for Sliver of unknown size', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: CustomScrollView( + slivers: [ + const SliverToBoxAdapter(child: SizedBox(width: double.infinity, height: 150.0)), + const SliverToBoxAdapter(child: SizedBox(width: double.infinity, height: 150.0)), + SliverList( + delegate: SliverChildBuilderDelegate((BuildContext context, int index) { + if (index < 100) { + return const SizedBox(width: double.infinity, height: 150.0); + } else { + return null; + } + }), + ), + const SliverToBoxAdapter( + key: Key('final_sliver'), + child: SizedBox(width: double.infinity, height: 150.0), + ), + ], + ), + ), + ); + + // The last Sliver comes after a SliverList that has many more items than + // can fit in the viewport, and the SliverList doesn't report a child count, + // so the SliverList leads to an infinite precedingScrollExtent. + final RenderViewport renderViewport = tester.renderObject(find.byType(Viewport)); + final RenderSliver lastRenderSliver = renderViewport.lastChild; + expect(lastRenderSliver.constraints.precedingScrollExtent, double.infinity); + }); +} \ No newline at end of file