From 30fd06f7871288ee6c4cd62cccac406766a84ab5 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Tue, 5 Sep 2017 12:42:28 -0700 Subject: [PATCH] getOffsetToReveal deals with pinned slivers (#11878) * getOffsetToReveal deals with pinned slivers If a Sliver can potentially be pinned at the edge of a viewport, getOffsetToReveal will take that into account and scroll further up/down to ensure that the object to reveal doesn't end up covered by a pinned sliver. This is important for accessibility scrolling with app bars. Since how much a pinned sliver is covering is dynamic (it can change with scroll offset, etc), getOffsetToReveal deals with the worst case and tries to ensure that the object to uncover is visible when the pinned slivers are at their max pinned extent. * name fixes * review feedback * typos * renaming * fix analyzer * fix test * analyzer fixes --- .../flutter/lib/src/rendering/sliver.dart | 11 ++ .../rendering/sliver_persistent_header.dart | 3 + .../flutter/lib/src/rendering/viewport.dart | 64 ++++++++- .../widgets/scrollable_semantics_test.dart | 125 +++++++++++++++++- 4 files changed, 200 insertions(+), 3 deletions(-) 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 +}