From 5e93756f1945841a6f56c44e216f03e3180b251c Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 22 Feb 2017 15:57:32 -0800 Subject: [PATCH] Clean up flipping of ScrollDirection (#8343) Makes RenderViewport.layoutOneSide more readable. --- .../flutter/lib/src/rendering/sliver.dart | 23 ++++++++++++++++++- .../flutter/lib/src/rendering/viewport.dart | 23 +++---------------- .../lib/src/rendering/viewport_offset.dart | 17 ++++++++++++++ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/packages/flutter/lib/src/rendering/sliver.dart b/packages/flutter/lib/src/rendering/sliver.dart index 700b9c8f79..9f03f8829f 100644 --- a/packages/flutter/lib/src/rendering/sliver.dart +++ b/packages/flutter/lib/src/rendering/sliver.dart @@ -131,6 +131,27 @@ AxisDirection applyGrowthDirectionToAxisDirection(AxisDirection axisDirection, G return null; } +/// Flips the [ScrollDirection] if the [GrowthDirection] is [GrowthDirection.reverse]. +/// +/// Specifically, returns `scrollDirection` if `scrollDirection` is +/// [GrowthDirection.forward], otherwise returns [flipScrollDirection] applied to +/// `scrollDirection`. +/// +/// This function is useful in [RenderSliver] subclasses that are given both an +/// [ScrollDirection] and a [GrowthDirection] and wish to compute the +/// [ScrollDirection] in which growth will occur. +ScrollDirection applyGrowthDirecitonToScrollDirection(ScrollDirection scrollDirection, GrowthDirection growthDirection) { + assert(scrollDirection != null); + assert(growthDirection != null); + switch (growthDirection) { + case GrowthDirection.forward: + return scrollDirection; + case GrowthDirection.reverse: + return flipScrollDirection(scrollDirection); + } + return null; +} + /// Immutable layout constraints for [RenderSliver] layout. /// /// The [SliverConstraints] describe the current scroll state of the viewport @@ -524,7 +545,7 @@ class SliverGeometry { @override String toString() { - StringBuffer buffer = new StringBuffer(); + final StringBuffer buffer = new StringBuffer(); buffer.write('SliverGeometry('); buffer.write('scrollExtent: ${scrollExtent.toStringAsFixed(1)}, '); if (paintExtent > 0.0) { diff --git a/packages/flutter/lib/src/rendering/viewport.dart b/packages/flutter/lib/src/rendering/viewport.dart index e89dac50be..3a09bbe959 100644 --- a/packages/flutter/lib/src/rendering/viewport.dart +++ b/packages/flutter/lib/src/rendering/viewport.dart @@ -129,28 +129,11 @@ abstract class RenderViewportBase= 0.0); - ScrollDirection adjustedUserScrollDirection; - switch (growthDirection) { - case GrowthDirection.forward: - adjustedUserScrollDirection = offset.userScrollDirection; - break; - case GrowthDirection.reverse: - switch (offset.userScrollDirection) { - case ScrollDirection.forward: - adjustedUserScrollDirection = ScrollDirection.reverse; - break; - case ScrollDirection.reverse: - adjustedUserScrollDirection = ScrollDirection.forward; - break; - case ScrollDirection.idle: - adjustedUserScrollDirection = ScrollDirection.idle; - break; - } - break; - } + final ScrollDirection adjustedUserScrollDirection = + applyGrowthDirecitonToScrollDirection(offset.userScrollDirection, growthDirection); assert(adjustedUserScrollDirection != null); double maxPaintOffset = layoutOffset; - double initialLayoutOffset = layoutOffset; + final double initialLayoutOffset = layoutOffset; while (child != null) { assert(scrollOffset >= 0.0); child.layout(new SliverConstraints( diff --git a/packages/flutter/lib/src/rendering/viewport_offset.dart b/packages/flutter/lib/src/rendering/viewport_offset.dart index 86930e026d..4824c7412f 100644 --- a/packages/flutter/lib/src/rendering/viewport_offset.dart +++ b/packages/flutter/lib/src/rendering/viewport_offset.dart @@ -33,6 +33,23 @@ enum ScrollDirection { reverse, } +/// Returns the opposite of the given [ScrollDirection]. +/// +/// Specifically, returns [AxisDirection.reverse] for [AxisDirection.forward] +/// (and vice versa) and returns [ScrollDirection.idle] for +/// [ScrollDirection.idle]. +ScrollDirection flipScrollDirection(ScrollDirection direction) { + switch (direction) { + case ScrollDirection.idle: + return ScrollDirection.idle; + case ScrollDirection.forward: + return ScrollDirection.reverse; + case ScrollDirection.reverse: + return ScrollDirection.forward; + } + return null; +} + abstract class ViewportOffset extends ChangeNotifier { ViewportOffset(); factory ViewportOffset.fixed(double value) = _FixedViewportOffset;