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