diff --git a/packages/flutter/lib/src/rendering/sliver.dart b/packages/flutter/lib/src/rendering/sliver.dart index 9458897f43..b559476ffe 100644 --- a/packages/flutter/lib/src/rendering/sliver.dart +++ b/packages/flutter/lib/src/rendering/sliver.dart @@ -525,6 +525,7 @@ class SliverGeometry extends Diagnosticable { this.paintOrigin: 0.0, double layoutExtent, this.maxPaintExtent: 0.0, + this.maxScrollObstructionExtent: 0.0, double hitTestExtent, bool visible, this.hasVisualOverflow: false, @@ -592,6 +593,16 @@ class SliverGeometry extends Diagnosticable { /// By definition, this cannot be less than [paintExtent]. final double maxPaintExtent; + /// The maximum extent by which this sliver can reduce the area in which + /// content can scroll if the sliver were pinned at the edge. + /// + /// Slivers that never get pinned at the edge, should return zero. + /// + /// A pinned app bar is an example for a sliver that would use this setting: + /// When the app bar is pinned to the top, the area in which content can + /// actually scroll is reduced by the height of the app bar. + final double maxScrollObstructionExtent; + /// The distance from where this sliver started painting to the bottom of /// where it should accept hits. /// diff --git a/packages/flutter/lib/src/rendering/sliver_persistent_header.dart b/packages/flutter/lib/src/rendering/sliver_persistent_header.dart index b543710ae0..46dae37589 100644 --- a/packages/flutter/lib/src/rendering/sliver_persistent_header.dart +++ b/packages/flutter/lib/src/rendering/sliver_persistent_header.dart @@ -297,6 +297,7 @@ abstract class RenderSliverPinnedPersistentHeader extends RenderSliverPersistent paintExtent: math.min(childExtent, constraints.remainingPaintExtent), layoutExtent: (maxExtent - constraints.scrollOffset).clamp(0.0, constraints.remainingPaintExtent), maxPaintExtent: maxExtent, + maxScrollObstructionExtent: minExtent, hasVisualOverflow: true, // Conservatively say we do have overflow to avoid complexity. ); } @@ -410,6 +411,7 @@ abstract class RenderSliverFloatingPersistentHeader extends RenderSliverPersiste paintExtent: paintExtent.clamp(0.0, constraints.remainingPaintExtent), layoutExtent: layoutExtent.clamp(0.0, constraints.remainingPaintExtent), maxPaintExtent: maxExtent, + maxScrollObstructionExtent: maxExtent, hasVisualOverflow: true, // Conservatively say we do have overflow to avoid complexity. ); return math.min(0.0, paintExtent - childExtent); @@ -519,6 +521,7 @@ abstract class RenderSliverFloatingPinnedPersistentHeader extends RenderSliverFl paintExtent: paintExtent.clamp(minExtent, constraints.remainingPaintExtent), layoutExtent: layoutExtent.clamp(0.0, constraints.remainingPaintExtent - minExtent), maxPaintExtent: maxExtent, + maxScrollObstructionExtent: maxExtent, hasVisualOverflow: true, // Conservatively say we do have overflow to avoid complexity. ); return 0.0; diff --git a/packages/flutter/lib/src/rendering/viewport.dart b/packages/flutter/lib/src/rendering/viewport.dart index b2fc62a405..2a95ef5de1 100644 --- a/packages/flutter/lib/src/rendering/viewport.dart +++ b/packages/flutter/lib/src/rendering/viewport.dart @@ -479,15 +479,24 @@ abstract class RenderViewportBase containers = []; + for (int i = 0; i < 80; i++) + containers.add(new MergeSemantics(child: new Container( + height: kItemHeight, + child: new Text('container $i'), + ))); + + final ScrollController scrollController = new ScrollController( + initialScrollOffset: kItemHeight / 2, + ); + + await tester.pumpWidget(new Directionality( + textDirection: TextDirection.ltr, + child: new MediaQuery( + data: const MediaQueryData(), + child: new Scrollable( + controller: scrollController, + viewportBuilder: (BuildContext context, ViewportOffset offset) { + return new Viewport( + offset: offset, + slivers: [ + const SliverAppBar( + pinned: true, + expandedHeight: kExpandedAppBarHeight, + flexibleSpace: const FlexibleSpaceBar( + title: const Text('App Bar'), + ), + ), + new SliverList( + delegate: new SliverChildListDelegate(containers), + ) + ], + ); + }), + ), + )); + + expect(scrollController.offset, kItemHeight / 2); + + final int firstContainerId = tester.renderObject(find.byWidget(containers.first)).debugSemantics.id; + tester.binding.pipelineOwner.semanticsOwner.performAction(firstContainerId, SemanticsAction.showOnScreen); + await tester.pump(); + await tester.pump(const Duration(seconds: 5)); + expect(tester.getTopLeft(find.byWidget(containers.first)).dy, kExpandedAppBarHeight); + + final int secondContainerId = tester.renderObject(find.byWidget(containers[1])).debugSemantics.id; + tester.binding.pipelineOwner.semanticsOwner.performAction(secondContainerId, SemanticsAction.showOnScreen); + await tester.pump(); + await tester.pump(const Duration(seconds: 5)); + expect(tester.getTopLeft(find.byWidget(containers[1])).dy, kExpandedAppBarHeight); + }); + + testWidgets('showOnScreen works with pinned app bar and individual slivers', (WidgetTester tester) async { + new SemanticsTester(tester); // enables semantics tree generation + + const double kItemHeight = 100.0; + const double kExpandedAppBarHeight = 256.0; + + + final List semantics = []; + final List slivers = new List.generate(30, (int i) { + final Widget child = new MergeSemantics( + child: new Container( + child: new Text('Item $i'), + height: 72.0, + ), + ); + semantics.add(child); + return new SliverToBoxAdapter( + child: child, + ); + }); + + final ScrollController scrollController = new ScrollController( + initialScrollOffset: kItemHeight / 2, + ); + + await tester.pumpWidget(new Directionality( + textDirection: TextDirection.ltr, + child:new MediaQuery( + data: const MediaQueryData(), + child: new Scrollable( + controller: scrollController, + viewportBuilder: (BuildContext context, ViewportOffset offset) { + return new Viewport( + offset: offset, + slivers: [ + const SliverAppBar( + pinned: true, + expandedHeight: kExpandedAppBarHeight, + flexibleSpace: const FlexibleSpaceBar( + title: const Text('App Bar'), + ), + ), + ]..addAll(slivers), + ); + }, + ), + ), + )); + + expect(scrollController.offset, kItemHeight / 2); + + final int id0 = tester.renderObject(find.byWidget(semantics[0])).debugSemantics.id; + tester.binding.pipelineOwner.semanticsOwner.performAction(id0, SemanticsAction.showOnScreen); + await tester.pump(); + await tester.pump(const Duration(seconds: 5)); + expect(tester.getTopLeft(find.byWidget(semantics[0])).dy, kToolbarHeight); + + final int id1 = tester.renderObject(find.byWidget(semantics[1])).debugSemantics.id; + tester.binding.pipelineOwner.semanticsOwner.performAction(id1, SemanticsAction.showOnScreen); + await tester.pump(); + await tester.pump(const Duration(seconds: 5)); + expect(tester.getTopLeft(find.byWidget(semantics[1])).dy, kToolbarHeight); + }); } Future flingUp(WidgetTester tester, { int repetitions: 1 }) async { @@ -88,4 +211,4 @@ Future flingDown(WidgetTester tester, { int repetitions: 1 }) async { await tester.pump(); await tester.pump(const Duration(seconds: 5)); } -} \ No newline at end of file +}