From eb839755b75a0e60d95372add4a61be7a4d6b768 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 24 Feb 2017 08:11:08 -0800 Subject: [PATCH] Overscrolls should appear below pinned headers (#8383) With slivers, the app bar is part of the scroll view. Naively, the overscroll appears at the beginning of the scroll view, but that's not the desired behavior. Instead, the app bar should remained pinned to the top of the scroll view and the overscroll should appear below the app bar, which is what this patch does. Fixes #8228 --- .../flutter/lib/src/rendering/sliver.dart | 21 +++++++ .../rendering/sliver_persistent_header.dart | 12 ++-- .../flutter/lib/src/rendering/viewport.dart | 54 +++++++++------- .../widgets/slivers_appbar_pinned_test.dart | 61 ++++++++++++++++--- 4 files changed, 108 insertions(+), 40 deletions(-) diff --git a/packages/flutter/lib/src/rendering/sliver.dart b/packages/flutter/lib/src/rendering/sliver.dart index b372128535..15e9a3458d 100644 --- a/packages/flutter/lib/src/rendering/sliver.dart +++ b/packages/flutter/lib/src/rendering/sliver.dart @@ -435,6 +435,7 @@ class SliverGeometry { const SliverGeometry({ this.scrollExtent: 0.0, this.paintExtent: 0.0, + this.paintOrigin: 0.0, double layoutExtent, this.maxPaintExtent: 0.0, double hitTestExtent, @@ -456,6 +457,23 @@ class SliverGeometry { /// [SliverConstraints.remainingPaintExtent] provided during layout. final double scrollExtent; + /// The visual location of the first visible part of this sliver relative to + /// its layout position. + /// + /// For example, if the sliver wishes to paint visually before its layout + /// position, the [paintOrigin] is negative. The coordinate system this sliver + /// uses for painting is relative to this [paintOrigin]. + /// + /// This value does not affect the layout of subsequent slivers. The next + /// sliver is still placed at [layoutExtent] after this sliver's layout + /// position. This value does affect where the [paintExtent] extent is + /// measured from when computing the [SliverConstraints.overlap] for the next + /// sliver. + /// + /// Defaults to 0.0, which means slivers start painting at their layout + /// position by default. + final double paintOrigin; + /// The amount of visual space that was taken by the sliver to render the /// subset of the sliver that covers all or part of the /// [SliverConstraints.remainingPaintExtent]. @@ -513,6 +531,7 @@ class SliverGeometry { assert(scrollExtent >= 0.0); assert(paintExtent != null); assert(paintExtent >= 0.0); + assert(paintOrigin != null); assert(layoutExtent != null); assert(layoutExtent >= 0.0); assert(() { @@ -563,6 +582,8 @@ class SliverGeometry { } else { buffer.write('paintExtent: ${paintExtent.toStringAsFixed(1)} (!), '); } + if (paintOrigin != 0.0) + buffer.write('paintOrigin: ${paintOrigin.toStringAsFixed(1)}, '); if (layoutExtent != paintExtent) buffer.write('layoutExtent: ${layoutExtent.toStringAsFixed(1)}, '); buffer.write('maxPaintExtent: ${maxPaintExtent.toStringAsFixed(1)}, '); diff --git a/packages/flutter/lib/src/rendering/sliver_persistent_header.dart b/packages/flutter/lib/src/rendering/sliver_persistent_header.dart index 064d917a6b..925c5add9c 100644 --- a/packages/flutter/lib/src/rendering/sliver_persistent_header.dart +++ b/packages/flutter/lib/src/rendering/sliver_persistent_header.dart @@ -236,21 +236,19 @@ abstract class RenderSliverPinnedPersistentHeader extends RenderSliverPersistent @override void performLayout() { final double maxExtent = this.maxExtent; - layoutChild(constraints.scrollOffset + constraints.overlap, maxExtent, overlapsContent: constraints.overlap > 0.0); + layoutChild(constraints.scrollOffset, maxExtent, overlapsContent: constraints.overlap > 0.0); geometry = new SliverGeometry( scrollExtent: maxExtent, - paintExtent: math.min(constraints.overlap + childExtent, constraints.remainingPaintExtent), + paintOrigin: constraints.overlap, + paintExtent: math.min(childExtent, constraints.remainingPaintExtent), layoutExtent: (maxExtent - constraints.scrollOffset).clamp(0.0, constraints.remainingPaintExtent), - maxPaintExtent: constraints.overlap + maxExtent, + maxPaintExtent: maxExtent, hasVisualOverflow: true, // Conservatively say we do have overflow to avoid complexity. ); } @override - double childMainAxisPosition(RenderBox child) { - assert(child == this.child); - return constraints?.overlap; - } + double childMainAxisPosition(RenderBox child) => 0.0; } abstract class RenderSliverFloatingPersistentHeader extends RenderSliverPersistentHeader { diff --git a/packages/flutter/lib/src/rendering/viewport.dart b/packages/flutter/lib/src/rendering/viewport.dart index 71b60e104c..e5df61a7d7 100644 --- a/packages/flutter/lib/src/rendering/viewport.dart +++ b/packages/flutter/lib/src/rendering/viewport.dart @@ -120,6 +120,7 @@ abstract class RenderViewportBase= 0.0); + final double initialLayoutOffset = layoutOffset; final ScrollDirection adjustedUserScrollDirection = applyGrowthDirecitonToScrollDirection(offset.userScrollDirection, growthDirection); assert(adjustedUserScrollDirection != null); - double maxPaintOffset = layoutOffset; - final double initialLayoutOffset = layoutOffset; + double maxPaintOffset = layoutOffset + overlap; while (child != null) { assert(scrollOffset >= 0.0); child.layout(new SliverConstraints( @@ -146,19 +147,19 @@ abstract class RenderViewportBase[ + new SliverPersistentHeader(delegate: new TestDelegate(), pinned: true), + new SliverList( + delegate: new SliverChildListDelegate([ + new SizedBox( + height: 300.0, + child: new Text('X'), + ), + ]), + ), + ], + ), + ); + + expect(tester.getTopLeft(find.byType(Container)), Point.origin); + expect(tester.getTopLeft(find.text('X')), const Point(0.0, 200.0)); + + ScrollPosition position = tester.state(find.byType(Scrollable)).position; + position.jumpTo(-50.0); + await tester.pump(); + + expect(tester.getTopLeft(find.byType(Container)), Point.origin); + expect(tester.getTopLeft(find.text('X')), const Point(0.0, 250.0)); + + position.jumpTo(50.0); + await tester.pump(); + + expect(tester.getTopLeft(find.byType(Container)), Point.origin); + expect(tester.getTopLeft(find.text('X')), const Point(0.0, 150.0)); + + position.jumpTo(150.0); + await tester.pump(); + + expect(tester.getTopLeft(find.byType(Container)), Point.origin); + expect(tester.getTopLeft(find.text('X')), const Point(0.0, 50.0)); + }); } class TestDelegate extends SliverPersistentHeaderDelegate {