From 81bbd3e12c32456cc307196acb96dc5059b30921 Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Fri, 14 Jun 2019 09:44:57 -0700 Subject: [PATCH] SliverFillRemaining flag for different use cases (#33627) * WIP * Fixing nested scroll view tests * Fixing nested scroll view tests * Fixing nested scroll view tests * WIP * Added flag for SliverFillRemaining * Clean up * Review * Reverted nested scroll view tests * Updating tests * Updated for overscroll behavior --- .../lib/src/rendering/sliver_fill.dart | 23 ++++++- packages/flutter/lib/src/widgets/sliver.dart | 18 +++++- .../widgets/sliver_fill_remaining_test.dart | 60 +++++++++++++++++++ .../widgets/sliver_fill_viewport_test.dart | 2 +- 4 files changed, 97 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/rendering/sliver_fill.dart b/packages/flutter/lib/src/rendering/sliver_fill.dart index 89ca49725a..1adbe83af1 100644 --- a/packages/flutter/lib/src/rendering/sliver_fill.dart +++ b/packages/flutter/lib/src/rendering/sliver_fill.dart @@ -113,18 +113,35 @@ class RenderSliverFillRemaining extends RenderSliverSingleBoxAdapter { /// the remaining space in the viewport. RenderSliverFillRemaining({ RenderBox child, - }) : super(child: child); + this.hasScrollBody = true, + }) : assert(hasScrollBody != null), + super(child: child); + + /// Whether the child has a scrollable body, this value cannot be null. + /// + /// Defaults to true such that the child will extend beyond the viewport and + /// scroll, as seen in [NestedScrollView]. + /// + /// Setting this value to false will allow the child to fill the remainder of + /// the viewport and not extend further. + bool hasScrollBody; @override void performLayout() { - final double extent = constraints.remainingPaintExtent - math.min(constraints.overlap, 0.0); + final double extent = constraints.remainingPaintExtent + - math.min(constraints.overlap, 0.0) + // Adding the offset for when this SliverFillRemaining is not scrollable, + // so it will stretch to fill on overscroll. + + (hasScrollBody ? 0.0 : constraints.scrollOffset); if (child != null) child.layout(constraints.asBoxConstraints(minExtent: extent, maxExtent: extent), parentUsesSize: true); final double paintedChildSize = calculatePaintOffset(constraints, from: 0.0, to: extent); assert(paintedChildSize.isFinite); assert(paintedChildSize >= 0.0); geometry = SliverGeometry( - scrollExtent: constraints.viewportMainAxisExtent, + // 0.0 can be applied here for cases when there is not scroll body since + // SliverFillRemaining will not have any slivers following it. + scrollExtent: hasScrollBody ? constraints.viewportMainAxisExtent : 0.0, paintExtent: paintedChildSize, maxPaintExtent: paintedChildSize, hasVisualOverflow: extent > constraints.remainingPaintExtent || constraints.scrollOffset > 0.0, diff --git a/packages/flutter/lib/src/widgets/sliver.dart b/packages/flutter/lib/src/widgets/sliver.dart index 53a28a64e2..0e0fb864d5 100644 --- a/packages/flutter/lib/src/widgets/sliver.dart +++ b/packages/flutter/lib/src/widgets/sliver.dart @@ -1368,10 +1368,24 @@ class SliverFillRemaining extends SingleChildRenderObjectWidget { const SliverFillRemaining({ Key key, Widget child, - }) : super(key: key, child: child); + this.hasScrollBody = true, + }) : assert(hasScrollBody != null), + super(key: key, child: child); + + /// Whether the child has a scrollable body, this value cannot be null. + /// + /// Defaults to true such that the child will extend beyond the viewport and + /// scroll, as seen in [NestedScrollView]. + /// + /// Setting this value to false will allow the child to fill the remainder of + /// the viewport and not extend further. + final bool hasScrollBody; @override - RenderSliverFillRemaining createRenderObject(BuildContext context) => RenderSliverFillRemaining(); + RenderSliverFillRemaining createRenderObject(BuildContext context) => RenderSliverFillRemaining(hasScrollBody: hasScrollBody); + + @override + void updateRenderObject(BuildContext context, RenderSliverFillRemaining renderObject) => renderObject.hasScrollBody = hasScrollBody; } /// Mark a child as needing to stay alive even when it's in a lazy list that diff --git a/packages/flutter/test/widgets/sliver_fill_remaining_test.dart b/packages/flutter/test/widgets/sliver_fill_remaining_test.dart index ff2090b486..1b58378730 100644 --- a/packages/flutter/test/widgets/sliver_fill_remaining_test.dart +++ b/packages/flutter/test/widgets/sliver_fill_remaining_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; void main() { @@ -62,4 +63,63 @@ void main() { await tester.pump(); expect(tester.renderObject(find.byType(Container)).size.height, equals(500.0)); }); + + testWidgets('SliverFillRemaining does not extend past viewport.', (WidgetTester tester) async { + final ScrollController controller = ScrollController(); + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: CustomScrollView( + controller: controller, + slivers: [ + SliverToBoxAdapter( + child: Container( + color: Colors.red, + height: 150.0, + ), + ), + SliverFillRemaining( + child: Container(color: Colors.white), + hasScrollBody: false, + ), + ], + ), + ), + ); + expect(controller.offset, 0.0); + expect(find.byType(Container), findsNWidgets(2)); + controller.jumpTo(150.0); + await tester.pumpAndSettle(); + expect(controller.offset, 0.0); + expect(find.byType(Container), findsNWidgets(2)); + }); + + testWidgets('SliverFillRemaining scrolls beyond viewport by default.', (WidgetTester tester) async { + final ScrollController controller = ScrollController(); + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: CustomScrollView( + controller: controller, + slivers: [ + SliverToBoxAdapter( + child: Container( + color: Colors.red, + height: 150.0, + ), + ), + SliverFillRemaining( + child: Container(color: Colors.white), + ), + ], + ), + ), + ); + expect(controller.offset, 0.0); + expect(find.byType(Container), findsNWidgets(2)); + controller.jumpTo(150.0); + await tester.pumpAndSettle(); + expect(controller.offset, 150.0); + expect(find.byType(Container), findsOneWidget); + }); } diff --git a/packages/flutter/test/widgets/sliver_fill_viewport_test.dart b/packages/flutter/test/widgets/sliver_fill_viewport_test.dart index e71db184c2..e5b60dd623 100644 --- a/packages/flutter/test/widgets/sliver_fill_viewport_test.dart +++ b/packages/flutter/test/widgets/sliver_fill_viewport_test.dart @@ -6,7 +6,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/widgets.dart'; void main() { - testWidgets('SliverFillRemaining control test', (WidgetTester tester) async { + testWidgets('SliverFillViewport control test', (WidgetTester tester) async { final List children = List.generate(20, (int i) { return Container(child: Text('$i', textDirection: TextDirection.ltr)); });