Unverified Commit 497f912d authored by Nate's avatar Nate Committed by GitHub

Implementing `switch` expressions in the `cupertino/` directory (#141591)

Refactors code to use the new `switch` expressions.
parent f61d2f53
......@@ -904,14 +904,11 @@ class _ContextMenuRoute<T> extends PopupRoute<T> {
// Get the alignment for the _ContextMenuSheet's Transform.scale based on the
// contextMenuLocation.
static AlignmentDirectional getSheetAlignment(_ContextMenuLocation contextMenuLocation) {
switch (contextMenuLocation) {
case _ContextMenuLocation.center:
return AlignmentDirectional.topCenter;
case _ContextMenuLocation.right:
return AlignmentDirectional.topEnd;
case _ContextMenuLocation.left:
return AlignmentDirectional.topStart;
}
return switch (contextMenuLocation) {
_ContextMenuLocation.center => AlignmentDirectional.topCenter,
_ContextMenuLocation.right => AlignmentDirectional.topEnd,
_ContextMenuLocation.left => AlignmentDirectional.topStart,
};
}
// The place to start the sheetRect animation from.
......@@ -1223,20 +1220,14 @@ class _ContextMenuRouteStaticState extends State<_ContextMenuRouteStatic> with T
}
Alignment _getChildAlignment(Orientation orientation, _ContextMenuLocation contextMenuLocation) {
switch (contextMenuLocation) {
case _ContextMenuLocation.center:
return orientation == Orientation.portrait
? Alignment.bottomCenter
: Alignment.topRight;
case _ContextMenuLocation.right:
return orientation == Orientation.portrait
? Alignment.bottomCenter
: Alignment.topLeft;
case _ContextMenuLocation.left:
return orientation == Orientation.portrait
? Alignment.bottomCenter
: Alignment.topRight;
if (orientation == Orientation.portrait) {
return Alignment.bottomCenter;
}
return switch (contextMenuLocation) {
_ContextMenuLocation.left => Alignment.topRight,
_ContextMenuLocation.center => Alignment.topRight,
_ContextMenuLocation.right => Alignment.topLeft,
};
}
void _setDragOffset(Offset dragOffset) {
......@@ -1307,16 +1298,12 @@ class _ContextMenuRouteStaticState extends State<_ContextMenuRouteStatic> with T
),
);
switch (contextMenuLocation) {
case _ContextMenuLocation.center:
return <Widget>[child, spacer, sheet];
case _ContextMenuLocation.right:
return orientation == Orientation.portrait
? <Widget>[child, spacer, sheet]
: <Widget>[sheet, spacer, child];
case _ContextMenuLocation.left:
return <Widget>[child, spacer, sheet];
}
return switch (contextMenuLocation) {
_ContextMenuLocation.right when orientation == Orientation.portrait => <Widget>[child, spacer, sheet],
_ContextMenuLocation.right => <Widget>[sheet, spacer, child],
_ContextMenuLocation.center => <Widget>[child, spacer, sheet],
_ContextMenuLocation.left => <Widget>[child, spacer, sheet],
};
}
// Build the animation for the _ContextMenuSheet.
......@@ -1475,29 +1462,12 @@ class _ContextMenuSheet extends StatelessWidget {
),
);
switch (_contextMenuLocation) {
case _ContextMenuLocation.center:
return _orientation == Orientation.portrait
? <Widget>[
const Spacer(),
menu,
const Spacer(),
]
: <Widget>[
menu,
const Spacer(),
];
case _ContextMenuLocation.right:
return <Widget>[
const Spacer(),
menu,
];
case _ContextMenuLocation.left:
return <Widget>[
menu,
const Spacer(),
];
}
return switch (_contextMenuLocation) {
_ContextMenuLocation.center when _orientation == Orientation.portrait => <Widget>[const Spacer(), menu, const Spacer()],
_ContextMenuLocation.center => <Widget>[menu, const Spacer()],
_ContextMenuLocation.right => <Widget>[const Spacer(), menu],
_ContextMenuLocation.left => <Widget>[menu, const Spacer()],
};
}
@override
......
......@@ -1757,25 +1757,8 @@ class _CupertinoDatePickerMonthYearState extends State<CupertinoDatePicker> {
double totalColumnWidths = 3 * _kDatePickerPadSize;
for (int i = 0; i < columnWidths.length; i++) {
late final double offAxisFraction;
switch (i) {
case 0:
offAxisFraction = -0.3 * textDirectionFactor;
default:
offAxisFraction = 0.5 * textDirectionFactor;
}
EdgeInsets padding = const EdgeInsets.only(right: _kDatePickerPadSize);
if (textDirectionFactor == -1) {
padding = const EdgeInsets.only(left: _kDatePickerPadSize);
}
Widget selectionOverlay = _centerSelectionOverlay;
if (i == 0) {
selectionOverlay = _startSelectionOverlay;
} else if (i == columnWidths.length - 1) {
selectionOverlay = _endSelectionOverlay;
}
final (bool first, bool last) = (i == 0, i == columnWidths.length - 1);
final double offAxisFraction = textDirectionFactor * (first ? -0.3 : 0.5);
totalColumnWidths += columnWidths[i] + (2 * _kDatePickerPadSize);
......@@ -1785,18 +1768,24 @@ class _CupertinoDatePickerMonthYearState extends State<CupertinoDatePicker> {
offAxisFraction,
(BuildContext context, Widget? child) {
return Container(
alignment: i == columnWidths.length - 1
? alignCenterLeft
: alignCenterRight,
padding: i == 0 ? null : padding,
alignment: last ? alignCenterLeft : alignCenterRight,
padding: switch (textDirectionFactor) {
_ when first => null,
-1 => const EdgeInsets.only(left: _kDatePickerPadSize),
_ => const EdgeInsets.only(right: _kDatePickerPadSize),
},
child: Container(
alignment: i == 0 ? alignCenterLeft : alignCenterRight,
alignment: first ? alignCenterLeft : alignCenterRight,
width: columnWidths[i] + _kDatePickerPadSize,
child: child,
),
);
},
selectionOverlay,
switch (last) {
_ when first => _startSelectionOverlay,
false => _centerSelectionOverlay,
true => _endSelectionOverlay,
},
),
));
}
......@@ -1958,12 +1947,10 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
late TextDirection textDirection;
late CupertinoLocalizations localizations;
int get textDirectionFactor {
switch (textDirection) {
case TextDirection.ltr:
return 1;
case TextDirection.rtl:
return -1;
}
return switch (textDirection) {
TextDirection.ltr => 1,
TextDirection.rtl => -1,
};
}
// The currently selected values of the picker.
......
......@@ -140,13 +140,10 @@ Widget _wrapWithBackground({
if (updateSystemUiOverlay) {
final bool isDark = backgroundColor.computeLuminance() < 0.179;
final Brightness newBrightness = brightness ?? (isDark ? Brightness.dark : Brightness.light);
final SystemUiOverlayStyle overlayStyle;
switch (newBrightness) {
case Brightness.dark:
overlayStyle = SystemUiOverlayStyle.light;
case Brightness.light:
overlayStyle = SystemUiOverlayStyle.dark;
}
final SystemUiOverlayStyle overlayStyle = switch (newBrightness) {
Brightness.dark => SystemUiOverlayStyle.light,
Brightness.light => SystemUiOverlayStyle.dark,
};
// [SystemUiOverlayStyle.light] and [SystemUiOverlayStyle.dark] set some system
// navigation bar properties,
// Before https://github.com/flutter/flutter/pull/104827 those properties
......
......@@ -673,12 +673,10 @@ class _CupertinoBackGestureDetectorState<T> extends State<_CupertinoBackGestureD
}
double _convertToLogical(double value) {
switch (Directionality.of(context)) {
case TextDirection.rtl:
return -value;
case TextDirection.ltr:
return value;
}
return switch (Directionality.of(context)) {
TextDirection.rtl => -value,
TextDirection.ltr => value,
};
}
@override
......
......@@ -450,13 +450,10 @@ class _RenderCupertinoSlider extends RenderConstrainedBox implements MouseTracke
double get _trackLeft => _kPadding;
double get _trackRight => size.width - _kPadding;
double get _thumbCenter {
final double visualPosition;
switch (textDirection) {
case TextDirection.rtl:
visualPosition = 1.0 - _value;
case TextDirection.ltr:
visualPosition = _value;
}
final double visualPosition = switch (textDirection) {
TextDirection.rtl => 1.0 - _value,
TextDirection.ltr => _value,
};
return lerpDouble(_trackLeft + CupertinoThumbPainter.radius, _trackRight - CupertinoThumbPainter.radius, visualPosition)!;
}
......@@ -468,12 +465,10 @@ class _RenderCupertinoSlider extends RenderConstrainedBox implements MouseTracke
if (isInteractive) {
final double extent = math.max(_kPadding, size.width - 2.0 * (_kPadding + CupertinoThumbPainter.radius));
final double valueDelta = details.primaryDelta! / extent;
switch (textDirection) {
case TextDirection.rtl:
_currentDragValue -= valueDelta;
case TextDirection.ltr:
_currentDragValue += valueDelta;
}
_currentDragValue += switch (textDirection) {
TextDirection.rtl => -valueDelta,
TextDirection.ltr => valueDelta,
};
onChanged!(_discretizedCurrentDragValue);
}
}
......
......@@ -322,12 +322,10 @@ class _CupertinoSwitchState extends State<CupertinoSwitch> with TickerProviderSt
..curve = Curves.linear
..reverseCurve = Curves.linear;
final double delta = details.primaryDelta! / _kTrackInnerLength;
switch (Directionality.of(context)) {
case TextDirection.rtl:
_positionController.value -= delta;
case TextDirection.ltr:
_positionController.value += delta;
}
_positionController.value += switch (Directionality.of(context)) {
TextDirection.rtl => -delta,
TextDirection.ltr => delta,
};
}
}
......@@ -674,13 +672,10 @@ class _RenderCupertinoSwitch extends RenderConstrainedBox {
final double currentValue = _state.position.value;
final double currentReactionValue = _state._reaction.value;
final double visualPosition;
switch (textDirection) {
case TextDirection.rtl:
visualPosition = 1.0 - currentValue;
case TextDirection.ltr:
visualPosition = currentValue;
}
final double visualPosition = switch (textDirection) {
TextDirection.rtl => 1.0 - currentValue,
TextDirection.ltr => currentValue,
};
final Paint paint = Paint()
..color = Color.lerp(trackColor, activeColor, currentValue)!;
......
......@@ -92,26 +92,18 @@ class CupertinoTextSelectionToolbarButton extends StatefulWidget {
assert(debugCheckHasCupertinoLocalizations(context));
final CupertinoLocalizations localizations = CupertinoLocalizations.of(context);
switch (buttonItem.type) {
case ContextMenuButtonType.cut:
return localizations.cutButtonLabel;
case ContextMenuButtonType.copy:
return localizations.copyButtonLabel;
case ContextMenuButtonType.paste:
return localizations.pasteButtonLabel;
case ContextMenuButtonType.selectAll:
return localizations.selectAllButtonLabel;
case ContextMenuButtonType.lookUp:
return localizations.lookUpButtonLabel;
case ContextMenuButtonType.searchWeb:
return localizations.searchWebButtonLabel;
case ContextMenuButtonType.share:
return localizations.shareButtonLabel;
case ContextMenuButtonType.liveTextInput:
case ContextMenuButtonType.delete:
case ContextMenuButtonType.custom:
return '';
}
return switch (buttonItem.type) {
ContextMenuButtonType.cut => localizations.cutButtonLabel,
ContextMenuButtonType.copy => localizations.copyButtonLabel,
ContextMenuButtonType.paste => localizations.pasteButtonLabel,
ContextMenuButtonType.selectAll => localizations.selectAllButtonLabel,
ContextMenuButtonType.lookUp => localizations.lookUpButtonLabel,
ContextMenuButtonType.searchWeb => localizations.searchWebButtonLabel,
ContextMenuButtonType.share => localizations.shareButtonLabel,
ContextMenuButtonType.liveTextInput
|| ContextMenuButtonType.delete
|| ContextMenuButtonType.custom => '',
};
}
@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