Skip to content

Commit 60d28ad

Browse files
Implementing switch expressions in rendering/ (flutter#143812)
This pull request is part of the effort to solve issue flutter#136139. The previous [`switch` expressions PR](flutter#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.
1 parent 469a233 commit 60d28ad

17 files changed

+366
-604
lines changed

packages/flutter/lib/src/rendering/decorated_sliver.dart

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,27 @@ class RenderDecoratedSliver extends RenderProxySliver {
9393

9494
@override
9595
void paint(PaintingContext context, Offset offset) {
96-
if (child != null && child!.geometry!.visible) {
97-
final SliverPhysicalParentData childParentData = child!.parentData! as SliverPhysicalParentData;
98-
final Size childSize;
99-
final Offset scrollOffset;
100-
101-
// In the case where the child sliver has infinite scroll extent, the decoration
102-
// should only extend down to the bottom cache extent.
103-
final double cappedMainAxisExtent = child!.geometry!.scrollExtent.isInfinite
104-
? constraints.scrollOffset + child!.geometry!.cacheExtent + constraints.cacheOrigin
105-
: child!.geometry!.scrollExtent;
106-
switch (constraints.axis) {
107-
case Axis.vertical:
108-
childSize = Size(constraints.crossAxisExtent, cappedMainAxisExtent);
109-
scrollOffset = Offset(0.0, -constraints.scrollOffset);
110-
case Axis.horizontal:
111-
childSize = Size(cappedMainAxisExtent, constraints.crossAxisExtent);
112-
scrollOffset = Offset(-constraints.scrollOffset, 0.0);
113-
}
114-
final Offset childOffset = offset + childParentData.paintOffset;
115-
if (position == DecorationPosition.background) {
116-
_painter!.paint(context.canvas, childOffset + scrollOffset, configuration.copyWith(size: childSize));
117-
}
118-
context.paintChild(child!, childOffset);
119-
if (position == DecorationPosition.foreground) {
120-
_painter!.paint(context.canvas, childOffset + scrollOffset, configuration.copyWith(size: childSize));
121-
}
96+
if (child == null || !child!.geometry!.visible) {
97+
return;
98+
}
99+
// In the case where the child sliver has infinite scroll extent, the decoration
100+
// should only extend down to the bottom cache extent.
101+
final double cappedMainAxisExtent = child!.geometry!.scrollExtent.isInfinite
102+
? constraints.scrollOffset + child!.geometry!.cacheExtent + constraints.cacheOrigin
103+
: child!.geometry!.scrollExtent;
104+
final (Size childSize, Offset scrollOffset) = switch (constraints.axis) {
105+
Axis.horizontal => (Size(cappedMainAxisExtent, constraints.crossAxisExtent), Offset(-constraints.scrollOffset, 0.0)),
106+
Axis.vertical => (Size(constraints.crossAxisExtent, cappedMainAxisExtent), Offset(0.0, -constraints.scrollOffset)),
107+
};
108+
offset += (child!.parentData! as SliverPhysicalParentData).paintOffset;
109+
void paintDecoration() => _painter!.paint(context.canvas, offset + scrollOffset, configuration.copyWith(size: childSize));
110+
switch (position) {
111+
case DecorationPosition.background:
112+
paintDecoration();
113+
context.paintChild(child!, offset);
114+
case DecorationPosition.foreground:
115+
context.paintChild(child!, offset);
116+
paintDecoration();
122117
}
123118
}
124119
}

packages/flutter/lib/src/rendering/editable.dart

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,11 @@ class TextSelectionPoint {
6767

6868
@override
6969
String toString() {
70-
switch (direction) {
71-
case TextDirection.ltr:
72-
return '$point-ltr';
73-
case TextDirection.rtl:
74-
return '$point-rtl';
75-
case null:
76-
return '$point';
77-
}
70+
return switch (direction) {
71+
TextDirection.ltr => '$point-ltr',
72+
TextDirection.rtl => '$point-rtl',
73+
null => '$point',
74+
};
7875
}
7976

8077
@override
@@ -1686,32 +1683,26 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin,
16861683
Axis get _viewportAxis => _isMultiline ? Axis.vertical : Axis.horizontal;
16871684

16881685
Offset get _paintOffset {
1689-
switch (_viewportAxis) {
1690-
case Axis.horizontal:
1691-
return Offset(-offset.pixels, 0.0);
1692-
case Axis.vertical:
1693-
return Offset(0.0, -offset.pixels);
1694-
}
1686+
return switch (_viewportAxis) {
1687+
Axis.horizontal => Offset(-offset.pixels, 0.0),
1688+
Axis.vertical => Offset(0.0, -offset.pixels),
1689+
};
16951690
}
16961691

16971692
double get _viewportExtent {
16981693
assert(hasSize);
1699-
switch (_viewportAxis) {
1700-
case Axis.horizontal:
1701-
return size.width;
1702-
case Axis.vertical:
1703-
return size.height;
1704-
}
1694+
return switch (_viewportAxis) {
1695+
Axis.horizontal => size.width,
1696+
Axis.vertical => size.height,
1697+
};
17051698
}
17061699

17071700
double _getMaxScrollExtent(Size contentSize) {
17081701
assert(hasSize);
1709-
switch (_viewportAxis) {
1710-
case Axis.horizontal:
1711-
return math.max(0.0, contentSize.width - size.width);
1712-
case Axis.vertical:
1713-
return math.max(0.0, contentSize.height - size.height);
1714-
}
1702+
return switch (_viewportAxis) {
1703+
Axis.horizontal => math.max(0.0, contentSize.width - size.width),
1704+
Axis.vertical => math.max(0.0, contentSize.height - size.height),
1705+
};
17151706
}
17161707

17171708
// We need to check the paint offset here because during animation, the start of

0 commit comments

Comments
 (0)