Unverified Commit 60d28ad9 authored by Nate's avatar Nate Committed by GitHub

Implementing `switch` expressions in `rendering/` (#143812)

This pull request is part of the effort to solve issue #136139.

The previous [`switch` expressions PR](https://github.com/flutter/flutter/pull/143496) was comprised of many simple changes throughout `flutter/lib/src/`, but due to some more in-depth refactoring in `flutter/lib/src/rendering/`, I decided to submit the changes to this directory as a separate pull request.

There was really just one function that I changed significantly; I'll add a comment for explanation.
parent 469a2338
......@@ -93,32 +93,27 @@ class RenderDecoratedSliver extends RenderProxySliver {
@override
void paint(PaintingContext context, Offset offset) {
if (child != null && child!.geometry!.visible) {
final SliverPhysicalParentData childParentData = child!.parentData! as SliverPhysicalParentData;
final Size childSize;
final Offset scrollOffset;
// In the case where the child sliver has infinite scroll extent, the decoration
// should only extend down to the bottom cache extent.
final double cappedMainAxisExtent = child!.geometry!.scrollExtent.isInfinite
? constraints.scrollOffset + child!.geometry!.cacheExtent + constraints.cacheOrigin
: child!.geometry!.scrollExtent;
switch (constraints.axis) {
case Axis.vertical:
childSize = Size(constraints.crossAxisExtent, cappedMainAxisExtent);
scrollOffset = Offset(0.0, -constraints.scrollOffset);
case Axis.horizontal:
childSize = Size(cappedMainAxisExtent, constraints.crossAxisExtent);
scrollOffset = Offset(-constraints.scrollOffset, 0.0);
}
final Offset childOffset = offset + childParentData.paintOffset;
if (position == DecorationPosition.background) {
_painter!.paint(context.canvas, childOffset + scrollOffset, configuration.copyWith(size: childSize));
}
context.paintChild(child!, childOffset);
if (position == DecorationPosition.foreground) {
_painter!.paint(context.canvas, childOffset + scrollOffset, configuration.copyWith(size: childSize));
}
if (child == null || !child!.geometry!.visible) {
return;
}
// In the case where the child sliver has infinite scroll extent, the decoration
// should only extend down to the bottom cache extent.
final double cappedMainAxisExtent = child!.geometry!.scrollExtent.isInfinite
? constraints.scrollOffset + child!.geometry!.cacheExtent + constraints.cacheOrigin
: child!.geometry!.scrollExtent;
final (Size childSize, Offset scrollOffset) = switch (constraints.axis) {
Axis.horizontal => (Size(cappedMainAxisExtent, constraints.crossAxisExtent), Offset(-constraints.scrollOffset, 0.0)),
Axis.vertical => (Size(constraints.crossAxisExtent, cappedMainAxisExtent), Offset(0.0, -constraints.scrollOffset)),
};
offset += (child!.parentData! as SliverPhysicalParentData).paintOffset;
void paintDecoration() => _painter!.paint(context.canvas, offset + scrollOffset, configuration.copyWith(size: childSize));
switch (position) {
case DecorationPosition.background:
paintDecoration();
context.paintChild(child!, offset);
case DecorationPosition.foreground:
context.paintChild(child!, offset);
paintDecoration();
}
}
}
......@@ -67,14 +67,11 @@ class TextSelectionPoint {
@override
String toString() {
switch (direction) {
case TextDirection.ltr:
return '$point-ltr';
case TextDirection.rtl:
return '$point-rtl';
case null:
return '$point';
}
return switch (direction) {
TextDirection.ltr => '$point-ltr',
TextDirection.rtl => '$point-rtl',
null => '$point',
};
}
@override
......@@ -1686,32 +1683,26 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin,
Axis get _viewportAxis => _isMultiline ? Axis.vertical : Axis.horizontal;
Offset get _paintOffset {
switch (_viewportAxis) {
case Axis.horizontal:
return Offset(-offset.pixels, 0.0);
case Axis.vertical:
return Offset(0.0, -offset.pixels);
}
return switch (_viewportAxis) {
Axis.horizontal => Offset(-offset.pixels, 0.0),
Axis.vertical => Offset(0.0, -offset.pixels),
};
}
double get _viewportExtent {
assert(hasSize);
switch (_viewportAxis) {
case Axis.horizontal:
return size.width;
case Axis.vertical:
return size.height;
}
return switch (_viewportAxis) {
Axis.horizontal => size.width,
Axis.vertical => size.height,
};
}
double _getMaxScrollExtent(Size contentSize) {
assert(hasSize);
switch (_viewportAxis) {
case Axis.horizontal:
return math.max(0.0, contentSize.width - size.width);
case Axis.vertical:
return math.max(0.0, contentSize.height - size.height);
}
return switch (_viewportAxis) {
Axis.horizontal => math.max(0.0, contentSize.width - size.width),
Axis.vertical => math.max(0.0, contentSize.height - size.height),
};
}
// We need to check the paint offset here because during animation, the start of
......
......@@ -252,42 +252,34 @@ class RenderListBody extends RenderBox
@override
double computeMinIntrinsicWidth(double height) {
switch (mainAxis) {
case Axis.horizontal:
return _getIntrinsicMainAxis((RenderBox child) => child.getMinIntrinsicWidth(height));
case Axis.vertical:
return _getIntrinsicCrossAxis((RenderBox child) => child.getMinIntrinsicWidth(height));
}
return switch (mainAxis) {
Axis.horizontal => _getIntrinsicMainAxis((RenderBox child) => child.getMinIntrinsicWidth(height)),
Axis.vertical => _getIntrinsicCrossAxis((RenderBox child) => child.getMinIntrinsicWidth(height)),
};
}
@override
double computeMaxIntrinsicWidth(double height) {
switch (mainAxis) {
case Axis.horizontal:
return _getIntrinsicMainAxis((RenderBox child) => child.getMaxIntrinsicWidth(height));
case Axis.vertical:
return _getIntrinsicCrossAxis((RenderBox child) => child.getMaxIntrinsicWidth(height));
}
return switch (mainAxis) {
Axis.horizontal => _getIntrinsicMainAxis((RenderBox child) => child.getMaxIntrinsicWidth(height)),
Axis.vertical => _getIntrinsicCrossAxis((RenderBox child) => child.getMaxIntrinsicWidth(height)),
};
}
@override
double computeMinIntrinsicHeight(double width) {
switch (mainAxis) {
case Axis.horizontal:
return _getIntrinsicMainAxis((RenderBox child) => child.getMinIntrinsicHeight(width));
case Axis.vertical:
return _getIntrinsicCrossAxis((RenderBox child) => child.getMinIntrinsicHeight(width));
}
return switch (mainAxis) {
Axis.horizontal => _getIntrinsicMainAxis((RenderBox child) => child.getMinIntrinsicHeight(width)),
Axis.vertical => _getIntrinsicCrossAxis((RenderBox child) => child.getMinIntrinsicHeight(width)),
};
}
@override
double computeMaxIntrinsicHeight(double width) {
switch (mainAxis) {
case Axis.horizontal:
return _getIntrinsicMainAxis((RenderBox child) => child.getMaxIntrinsicHeight(width));
case Axis.vertical:
return _getIntrinsicCrossAxis((RenderBox child) => child.getMaxIntrinsicHeight(width));
}
return switch (mainAxis) {
Axis.horizontal => _getIntrinsicMainAxis((RenderBox child) => child.getMaxIntrinsicHeight(width)),
Axis.vertical => _getIntrinsicCrossAxis((RenderBox child) => child.getMaxIntrinsicHeight(width)),
};
}
@override
......
......@@ -843,15 +843,10 @@ class RenderParagraph extends RenderBox with ContainerRenderObjectMixin<RenderBo
locale: locale,
)..layout();
if (didOverflowWidth) {
double fadeEnd, fadeStart;
switch (textDirection) {
case TextDirection.rtl:
fadeEnd = 0.0;
fadeStart = fadeSizePainter.width;
case TextDirection.ltr:
fadeEnd = size.width;
fadeStart = fadeEnd - fadeSizePainter.width;
}
final (double fadeStart, double fadeEnd) = switch (textDirection) {
TextDirection.rtl => (fadeSizePainter.width, 0.0),
TextDirection.ltr => (size.width - fadeSizePainter.width, size.width),
};
_overflowShader = ui.Gradient.linear(
Offset(fadeStart, 0.0),
Offset(fadeEnd, 0.0),
......
......@@ -1958,12 +1958,10 @@ class RenderPhysicalModel extends _RenderPhysicalModelBase<RRect> {
RRect get _defaultClip {
assert(hasSize);
final Rect rect = Offset.zero & size;
switch (_shape) {
case BoxShape.rectangle:
return (borderRadius ?? BorderRadius.zero).toRRect(rect);
case BoxShape.circle:
return RRect.fromRectXY(rect, rect.width / 2, rect.height / 2);
}
return switch (_shape) {
BoxShape.rectangle => (borderRadius ?? BorderRadius.zero).toRRect(rect),
BoxShape.circle => RRect.fromRectXY(rect, rect.width / 2, rect.height / 2),
};
}
@override
......
......@@ -671,25 +671,21 @@ class RenderConstrainedOverflowBox extends RenderAligningShiftedBox {
@override
bool get sizedByParent {
switch (fit) {
case OverflowBoxFit.max:
return true;
case OverflowBoxFit.deferToChild:
// If deferToChild, the size will be as small as its child when non-overflowing,
// thus it cannot be sizedByParent.
return false;
}
return switch (fit) {
OverflowBoxFit.max => true,
// If deferToChild, the size will be as small as its child when non-overflowing,
// thus it cannot be sizedByParent.
OverflowBoxFit.deferToChild => false,
};
}
@override
@protected
Size computeDryLayout(covariant BoxConstraints constraints) {
switch (fit) {
case OverflowBoxFit.max:
return constraints.biggest;
case OverflowBoxFit.deferToChild:
return child?.getDryLayout(constraints) ?? constraints.smallest;
}
return switch (fit) {
OverflowBoxFit.max => constraints.biggest,
OverflowBoxFit.deferToChild => child?.getDryLayout(constraints) ?? constraints.smallest,
};
}
@override
......
......@@ -150,12 +150,10 @@ enum GrowthDirection {
/// [AxisDirection] and a [GrowthDirection] and wish to compute the
/// [AxisDirection] in which growth will occur.
AxisDirection applyGrowthDirectionToAxisDirection(AxisDirection axisDirection, GrowthDirection growthDirection) {
switch (growthDirection) {
case GrowthDirection.forward:
return axisDirection;
case GrowthDirection.reverse:
return flipAxisDirection(axisDirection);
}
return switch (growthDirection) {
GrowthDirection.forward => axisDirection,
GrowthDirection.reverse => flipAxisDirection(axisDirection),
};
}
/// Flips the [ScrollDirection] if the [GrowthDirection] is
......@@ -169,12 +167,10 @@ AxisDirection applyGrowthDirectionToAxisDirection(AxisDirection axisDirection, G
/// [ScrollDirection] and a [GrowthDirection] and wish to compute the
/// [ScrollDirection] in which growth will occur.
ScrollDirection applyGrowthDirectionToScrollDirection(ScrollDirection scrollDirection, GrowthDirection growthDirection) {
switch (growthDirection) {
case GrowthDirection.forward:
return scrollDirection;
case GrowthDirection.reverse:
return flipScrollDirection(scrollDirection);
}
return switch (growthDirection) {
GrowthDirection.forward => scrollDirection,
GrowthDirection.reverse => flipScrollDirection(scrollDirection),
};
}
/// Immutable layout constraints for [RenderSliver] layout.
......@@ -442,19 +438,13 @@ class SliverConstraints extends Constraints {
/// This can be useful in combination with [axis] to view the [axisDirection]
/// and [growthDirection] in different terms.
GrowthDirection get normalizedGrowthDirection {
switch (axisDirection) {
case AxisDirection.down:
case AxisDirection.right:
return growthDirection;
case AxisDirection.up:
case AxisDirection.left:
switch (growthDirection) {
case GrowthDirection.forward:
return GrowthDirection.reverse;
case GrowthDirection.reverse:
return GrowthDirection.forward;
}
if (axisDirectionIsReversed(axisDirection)) {
return switch (growthDirection) {
GrowthDirection.forward => GrowthDirection.reverse,
GrowthDirection.reverse => GrowthDirection.forward,
};
}
return growthDirection;
}
@override
......@@ -1582,16 +1572,12 @@ abstract class RenderSliver extends RenderObject {
Size getAbsoluteSizeRelativeToOrigin() {
assert(geometry != null);
assert(!debugNeedsLayout);
switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
case AxisDirection.up:
return Size(constraints.crossAxisExtent, -geometry!.paintExtent);
case AxisDirection.right:
return Size(geometry!.paintExtent, constraints.crossAxisExtent);
case AxisDirection.down:
return Size(constraints.crossAxisExtent, geometry!.paintExtent);
case AxisDirection.left:
return Size(-geometry!.paintExtent, constraints.crossAxisExtent);
}
return switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
AxisDirection.up => Size(constraints.crossAxisExtent, -geometry!.paintExtent),
AxisDirection.down => Size(constraints.crossAxisExtent, geometry!.paintExtent),
AxisDirection.left => Size(-geometry!.paintExtent, constraints.crossAxisExtent),
AxisDirection.right => Size(geometry!.paintExtent, constraints.crossAxisExtent),
};
}
/// This returns the absolute [Size] of the sliver.
......@@ -1843,16 +1829,12 @@ abstract class RenderSliverSingleBoxAdapter extends RenderSliver with RenderObje
@protected
void setChildParentData(RenderObject child, SliverConstraints constraints, SliverGeometry geometry) {
final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
case AxisDirection.up:
childParentData.paintOffset = Offset(0.0, -(geometry.scrollExtent - (geometry.paintExtent + constraints.scrollOffset)));
case AxisDirection.right:
childParentData.paintOffset = Offset(-constraints.scrollOffset, 0.0);
case AxisDirection.down:
childParentData.paintOffset = Offset(0.0, -constraints.scrollOffset);
case AxisDirection.left:
childParentData.paintOffset = Offset(-(geometry.scrollExtent - (geometry.paintExtent + constraints.scrollOffset)), 0.0);
}
childParentData.paintOffset = switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
AxisDirection.up => Offset(0.0, geometry.paintExtent + constraints.scrollOffset - geometry.scrollExtent),
AxisDirection.left => Offset(geometry.paintExtent + constraints.scrollOffset - geometry.scrollExtent, 0.0),
AxisDirection.right => Offset(-constraints.scrollOffset, 0.0),
AxisDirection.down => Offset(0.0, -constraints.scrollOffset),
};
}
@override
......@@ -1911,13 +1893,10 @@ class RenderSliverToBoxAdapter extends RenderSliverSingleBoxAdapter {
}
final SliverConstraints constraints = this.constraints;
child!.layout(constraints.asBoxConstraints(), parentUsesSize: true);
final double childExtent;
switch (constraints.axis) {
case Axis.horizontal:
childExtent = child!.size.width;
case Axis.vertical:
childExtent = child!.size.height;
}
final double childExtent = switch (constraints.axis) {
Axis.horizontal => child!.size.width,
Axis.vertical => child!.size.height,
};
final double paintedChildSize = calculatePaintOffset(constraints, from: 0.0, to: childExtent);
final double cacheExtent = calculateCacheOffset(constraints, from: 0.0, to: childExtent);
......
......@@ -138,13 +138,10 @@ class RenderSliverFillRemaining extends RenderSliverSingleBoxAdapter {
double extent = constraints.viewportMainAxisExtent - constraints.precedingScrollExtent;
if (child != null) {
final double childExtent;
switch (constraints.axis) {
case Axis.horizontal:
childExtent = child!.getMaxIntrinsicWidth(constraints.crossAxisExtent);
case Axis.vertical:
childExtent = child!.getMaxIntrinsicHeight(constraints.crossAxisExtent);
}
final double childExtent = switch (constraints.axis) {
Axis.horizontal => child!.getMaxIntrinsicWidth(constraints.crossAxisExtent),
Axis.vertical => child!.getMaxIntrinsicHeight(constraints.crossAxisExtent),
};
// If the childExtent is greater than the computed extent, we want to use
// that instead of potentially cutting off the child. This allows us to
......@@ -213,13 +210,10 @@ class RenderSliverFillRemainingAndOverscroll extends RenderSliverSingleBoxAdapte
double maxExtent = constraints.remainingPaintExtent - math.min(constraints.overlap, 0.0);
if (child != null) {
final double childExtent;
switch (constraints.axis) {
case Axis.horizontal:
childExtent = child!.getMaxIntrinsicWidth(constraints.crossAxisExtent);
case Axis.vertical:
childExtent = child!.getMaxIntrinsicHeight(constraints.crossAxisExtent);
}
final double childExtent = switch (constraints.axis) {
Axis.horizontal => child!.getMaxIntrinsicWidth(constraints.crossAxisExtent),
Axis.vertical => child!.getMaxIntrinsicHeight(constraints.crossAxisExtent),
};
// If the childExtent is greater than the computed extent, we want to use
// that instead of potentially cutting off the child. This allows us to
......
......@@ -110,12 +110,10 @@ class RenderSliverCrossAxisGroup extends RenderSliver with ContainerRenderObject
: 0.0;
final double childExtent = child.geometry!.crossAxisExtent ?? extentPerFlexValue * (childParentData.crossAxisFlex ?? 0);
// Set child parent data.
switch (constraints.axis) {
case Axis.vertical:
childParentData.paintOffset = Offset(offset, -paintCorrection);
case Axis.horizontal:
childParentData.paintOffset = Offset(-paintCorrection, offset);
}
childParentData.paintOffset = switch (constraints.axis) {
Axis.vertical => Offset(offset, -paintCorrection),
Axis.horizontal => Offset(-paintCorrection, offset),
};
offset += childExtent;
child = childAfter(child);
}
......@@ -229,14 +227,11 @@ class RenderSliverMainAxisGroup extends RenderSliver with ContainerRenderObjectM
@override
double childMainAxisPosition(RenderSliver child) {
switch (constraints.axisDirection) {
case AxisDirection.up:
case AxisDirection.down:
return (child.parentData! as SliverPhysicalParentData).paintOffset.dy;
case AxisDirection.left:
case AxisDirection.right:
return (child.parentData! as SliverPhysicalParentData).paintOffset.dx;
}
final Offset paintOffset = (child.parentData! as SliverPhysicalParentData).paintOffset;
return switch (constraints.axis) {
Axis.horizontal => paintOffset.dx,
Axis.vertical => paintOffset.dy,
};
}
@override
......@@ -269,12 +264,10 @@ class RenderSliverMainAxisGroup extends RenderSliver with ContainerRenderObjectM
);
final SliverGeometry childLayoutGeometry = child.geometry!;
final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
switch (constraints.axis) {
case Axis.vertical:
childParentData.paintOffset = Offset(0.0, beforeOffsetPaintExtent);
case Axis.horizontal:
childParentData.paintOffset = Offset(beforeOffsetPaintExtent, 0.0);
}
childParentData.paintOffset = switch (constraints.axis) {
Axis.vertical => Offset(0.0, beforeOffsetPaintExtent),
Axis.horizontal => Offset(beforeOffsetPaintExtent, 0.0),
};
offset += childLayoutGeometry.scrollExtent;
maxPaintExtent += child.geometry!.maxPaintExtent;
child = childAfter(child);
......@@ -304,12 +297,10 @@ class RenderSliverMainAxisGroup extends RenderSliver with ContainerRenderObjectM
final double remainingExtent = totalScrollExtent - constraints.scrollOffset;
if (childLayoutGeometry.paintExtent > remainingExtent) {
final double paintCorrection = childLayoutGeometry.paintExtent - remainingExtent;
switch (constraints.axis) {
case Axis.vertical:
childParentData.paintOffset = Offset(0.0, beforeOffsetPaintExtent - paintCorrection);
case Axis.horizontal:
childParentData.paintOffset = Offset(beforeOffsetPaintExtent - paintCorrection, 0.0);
}
childParentData.paintOffset = switch (constraints.axis) {
Axis.vertical => Offset(0.0, beforeOffsetPaintExtent - paintCorrection),
Axis.horizontal => Offset(beforeOffsetPaintExtent - paintCorrection, 0.0),
};
}
offset += child.geometry!.scrollExtent;
child = childAfter(child);
......
......@@ -602,12 +602,10 @@ abstract class RenderSliverMultiBoxAdaptor extends RenderSliver
@protected
double paintExtentOf(RenderBox child) {
assert(child.hasSize);
switch (constraints.axis) {
case Axis.horizontal:
return child.size.width;
case Axis.vertical:
return child.size.height;
}
return switch (constraints.axis) {
Axis.horizontal => child.size.width,
Axis.vertical => child.size.height,
};
}
@override
......
......@@ -152,12 +152,10 @@ abstract class RenderSliverPersistentHeader extends RenderSliver with RenderObje
return 0.0;
}
assert(child!.hasSize);
switch (constraints.axis) {
case Axis.vertical:
return child!.size.height;
case Axis.horizontal:
return child!.size.width;
}
return switch (constraints.axis) {
Axis.vertical => child!.size.height,
Axis.horizontal => child!.size.width,
};
}
bool _needsUpdateChild = true;
......@@ -298,16 +296,12 @@ abstract class RenderSliverPersistentHeader extends RenderSliver with RenderObje
@override
void paint(PaintingContext context, Offset offset) {
if (child != null && geometry!.visible) {
switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
case AxisDirection.up:
offset += Offset(0.0, geometry!.paintExtent - childMainAxisPosition(child!) - childExtent);
case AxisDirection.down:
offset += Offset(0.0, childMainAxisPosition(child!));
case AxisDirection.left:
offset += Offset(geometry!.paintExtent - childMainAxisPosition(child!) - childExtent, 0.0);
case AxisDirection.right:
offset += Offset(childMainAxisPosition(child!), 0.0);
}
offset += switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
AxisDirection.up => Offset(0.0, geometry!.paintExtent - childMainAxisPosition(child!) - childExtent),
AxisDirection.left => Offset(geometry!.paintExtent - childMainAxisPosition(child!) - childExtent, 0.0),
AxisDirection.right => Offset(childMainAxisPosition(child!), 0.0),
AxisDirection.down => Offset(0.0, childMainAxisPosition(child!)),
};
context.paintChild(child!, offset);
}
}
......@@ -446,17 +440,12 @@ abstract class RenderSliverPinnedPersistentHeader extends RenderSliverPersistent
? MatrixUtils.transformRect(descendant.getTransformTo(this), rect ?? descendant.paintBounds)
: rect;
Rect? newRect;
switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
case AxisDirection.up:
newRect = _trim(localBounds, bottom: childExtent);
case AxisDirection.right:
newRect = _trim(localBounds, left: 0);
case AxisDirection.down:
newRect = _trim(localBounds, top: 0);
case AxisDirection.left:
newRect = _trim(localBounds, right: childExtent);
}
final Rect? newRect = switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
AxisDirection.up => _trim(localBounds, bottom: childExtent),
AxisDirection.left => _trim(localBounds, right: childExtent),
AxisDirection.right => _trim(localBounds, left: 0),
AxisDirection.down => _trim(localBounds, top: 0),
};
super.showOnScreen(
descendant: this,
......
......@@ -66,17 +66,10 @@ class RelativeRect {
required double end,
required double bottom,
}) {
double left;
double right;
switch (textDirection) {
case TextDirection.rtl:
left = end;
right = start;
case TextDirection.ltr:
left = start;
right = end;
}
final (double left, double right) = switch (textDirection) {
TextDirection.rtl => (end, start),
TextDirection.ltr => (start, end),
};
return RelativeRect.fromLTRB(left, top, right, bottom);
}
......
......@@ -69,14 +69,11 @@ enum ScrollDirection {
/// (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 switch (direction) {
ScrollDirection.idle => ScrollDirection.idle,
ScrollDirection.forward => ScrollDirection.reverse,
ScrollDirection.reverse => ScrollDirection.forward,
};
}
/// Which part of the content inside the viewport should be visible.
......
......@@ -454,42 +454,33 @@ class RenderWrap extends RenderBox
}
double _getMainAxisExtent(Size childSize) {
switch (direction) {
case Axis.horizontal:
return childSize.width;
case Axis.vertical:
return childSize.height;
}
return switch (direction) {
Axis.horizontal => childSize.width,
Axis.vertical => childSize.height,
};
}
double _getCrossAxisExtent(Size childSize) {
switch (direction) {
case Axis.horizontal:
return childSize.height;
case Axis.vertical:
return childSize.width;
}
return switch (direction) {
Axis.horizontal => childSize.height,
Axis.vertical => childSize.width,
};
}
Offset _getOffset(double mainAxisOffset, double crossAxisOffset) {
switch (direction) {
case Axis.horizontal:
return Offset(mainAxisOffset, crossAxisOffset);
case Axis.vertical:
return Offset(crossAxisOffset, mainAxisOffset);
}
return switch (direction) {
Axis.horizontal => Offset(mainAxisOffset, crossAxisOffset),
Axis.vertical => Offset(crossAxisOffset, mainAxisOffset),
};
}
double _getChildCrossAxisOffset(bool flipCrossAxis, double runCrossAxisExtent, double childCrossAxisExtent) {
final double freeSpace = runCrossAxisExtent - childCrossAxisExtent;
switch (crossAxisAlignment) {
case WrapCrossAlignment.start:
return flipCrossAxis ? freeSpace : 0.0;
case WrapCrossAlignment.end:
return flipCrossAxis ? 0.0 : freeSpace;
case WrapCrossAlignment.center:
return freeSpace / 2.0;
}
return switch (crossAxisAlignment) {
WrapCrossAlignment.start => flipCrossAxis ? freeSpace : 0.0,
WrapCrossAlignment.end => flipCrossAxis ? 0.0 : freeSpace,
WrapCrossAlignment.center => freeSpace / 2.0,
};
}
bool _hasVisualOverflow = false;
......@@ -501,16 +492,10 @@ class RenderWrap extends RenderBox
}
Size _computeDryLayout(BoxConstraints constraints, [ChildLayouter layoutChild = ChildLayoutHelper.dryLayoutChild]) {
final BoxConstraints childConstraints;
double mainAxisLimit = 0.0;
switch (direction) {
case Axis.horizontal:
childConstraints = BoxConstraints(maxWidth: constraints.maxWidth);
mainAxisLimit = constraints.maxWidth;
case Axis.vertical:
childConstraints = BoxConstraints(maxHeight: constraints.maxHeight);
mainAxisLimit = constraints.maxHeight;
}
final (BoxConstraints childConstraints, double mainAxisLimit) = switch (direction) {
Axis.horizontal => (BoxConstraints(maxWidth: constraints.maxWidth), constraints.maxWidth),
Axis.vertical => (BoxConstraints(maxHeight: constraints.maxHeight), constraints.maxHeight),
};
double mainAxisExtent = 0.0;
double crossAxisExtent = 0.0;
......@@ -541,12 +526,10 @@ class RenderWrap extends RenderBox
crossAxisExtent += runCrossAxisExtent;
mainAxisExtent = math.max(mainAxisExtent, runMainAxisExtent);
switch (direction) {
case Axis.horizontal:
return constraints.constrain(Size(mainAxisExtent, crossAxisExtent));
case Axis.vertical:
return constraints.constrain(Size(crossAxisExtent, mainAxisExtent));
}
return constraints.constrain(switch (direction) {
Axis.horizontal => Size(mainAxisExtent, crossAxisExtent),
Axis.vertical => Size(crossAxisExtent, mainAxisExtent),
});
}
@override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment