Skip to content

Commit

Permalink
flutter/lib/src/: refactoring if-chains into switch expressions (#1…
Browse files Browse the repository at this point in the history
…47472)

Cleans up some code by porting if chains to switch statements.
  • Loading branch information
nate-thegrate committed May 1, 2024
1 parent 5a41e1e commit 586e1fc
Show file tree
Hide file tree
Showing 25 changed files with 195 additions and 275 deletions.
12 changes: 5 additions & 7 deletions packages/flutter/lib/src/animation/animation_controller.dart
Expand Up @@ -886,13 +886,11 @@ class _InterpolationSimulation extends Simulation {
@override
double x(double timeInSeconds) {
final double t = clampDouble(timeInSeconds / _durationInSeconds, 0.0, 1.0);
if (t == 0.0) {
return _begin;
} else if (t == 1.0) {
return _end;
} else {
return _begin + (_end - _begin) * _curve.transform(t);
}
return switch (t) {
0.0 => _begin,
1.0 => _end,
_ => _begin + (_end - _begin) * _curve.transform(t),
};
}

@override
Expand Down
17 changes: 8 additions & 9 deletions packages/flutter/lib/src/cupertino/context_menu.dart
Expand Up @@ -1298,15 +1298,14 @@ class _ContextMenuRouteStaticState extends State<_ContextMenuRouteStatic> with T
child: AnimatedBuilder(
animation: _moveController,
builder: _buildAnimation,
child: widget.orientation == Orientation.portrait
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
)
: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
),
child: Flex(
direction: switch (widget.orientation) {
Orientation.portrait => Axis.vertical,
Orientation.landscape => Axis.horizontal,
},
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
),
),
),
),
Expand Down
94 changes: 34 additions & 60 deletions packages/flutter/lib/src/cupertino/list_tile.dart
Expand Up @@ -251,37 +251,20 @@ class _CupertinoListTileState extends State<CupertinoListTile> {

@override
Widget build(BuildContext context) {
final TextStyle titleTextStyle =
widget._type == _CupertinoListTileType.base || widget.subtitle == null
? CupertinoTheme.of(context).textTheme.textStyle
: CupertinoTheme.of(context).textTheme.textStyle.merge(
TextStyle(
fontWeight: FontWeight.w600,
fontSize: widget.leading == null ? _kNotchedTitleWithSubtitleFontSize : null,
),
);

final TextStyle subtitleTextStyle = widget._type == _CupertinoListTileType.base
? CupertinoTheme.of(context).textTheme.textStyle.merge(
TextStyle(
fontSize: _kSubtitleFontSize,
color: CupertinoColors.secondaryLabel.resolveFrom(context),
),
)
: CupertinoTheme.of(context).textTheme.textStyle.merge(
TextStyle(
fontSize: _kNotchedSubtitleFontSize,
color: CupertinoColors.secondaryLabel.resolveFrom(context),
),
);

final TextStyle? additionalInfoTextStyle = widget.additionalInfo != null
? CupertinoTheme.of(context).textTheme.textStyle.merge(
TextStyle(color: CupertinoColors.secondaryLabel.resolveFrom(context)))
: null;
final TextStyle textStyle = CupertinoTheme.of(context).textTheme.textStyle;
final TextStyle coloredStyle = textStyle.copyWith(
color: CupertinoColors.secondaryLabel.resolveFrom(context),
);

final bool baseType = switch (widget._type) {
_CupertinoListTileType.base => true,
_CupertinoListTileType.notched => false,
};
final Widget title = DefaultTextStyle(
style: titleTextStyle,
style: baseType || widget.subtitle == null ? textStyle : textStyle.copyWith(
fontWeight: FontWeight.w600,
fontSize: widget.leading == null ? _kNotchedTitleWithSubtitleFontSize : null,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
child: widget.title,
Expand All @@ -294,25 +277,6 @@ class _CupertinoListTileState extends State<CupertinoListTile> {
_CupertinoListTileType.notched => _kNotchedPaddingWithoutLeading,
};

Widget? subtitle;
if (widget.subtitle != null) {
subtitle = DefaultTextStyle(
style: subtitleTextStyle,
maxLines: 1,
overflow: TextOverflow.ellipsis,
child: widget.subtitle!,
);
}

Widget? additionalInfo;
if (widget.additionalInfo != null) {
additionalInfo = DefaultTextStyle(
style: additionalInfoTextStyle!,
maxLines: 1,
child: widget.additionalInfo!,
);
}

// The color for default state tile is set to either what user provided or
// null and it will resolve to the correct color provided by context. But if
// the tile was tapped, it is set to what user provided or if null to the
Expand All @@ -323,7 +287,7 @@ class _CupertinoListTileState extends State<CupertinoListTile> {
}

final double minHeight = switch (widget._type) {
_CupertinoListTileType.base when subtitle != null => _kMinHeightWithSubtitle,
_CupertinoListTileType.base when widget.subtitle != null => _kMinHeightWithSubtitle,
_CupertinoListTileType.notched when widget.leading != null => _kNotchedMinHeight,
_CupertinoListTileType.base => _kMinHeight,
_CupertinoListTileType.notched => _kNotchedMinHeightWithoutLeading,
Expand All @@ -336,13 +300,10 @@ class _CupertinoListTileState extends State<CupertinoListTile> {
padding: padding,
child: Row(
children: <Widget>[
if (widget.leading != null) ...<Widget>[
SizedBox(
width: widget.leadingSize,
height: widget.leadingSize,
child: Center(
child: widget.leading,
),
if (widget.leading case final Widget leading) ...<Widget>[
SizedBox.square(
dimension: widget.leadingSize,
child: Center(child: leading),
),
SizedBox(width: widget.leadingToTitle),
] else
Expand All @@ -353,15 +314,28 @@ class _CupertinoListTileState extends State<CupertinoListTile> {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
title,
if (subtitle != null) ...<Widget>[
if (widget.subtitle case final Widget subtitle) ...<Widget>[
const SizedBox(height: _kNotchedTitleToSubtitle),
subtitle,
DefaultTextStyle(
style: coloredStyle.copyWith(
fontSize: baseType
? _kSubtitleFontSize
: _kNotchedSubtitleFontSize,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
child: subtitle,
),
],
],
),
),
if (additionalInfo != null) ...<Widget>[
additionalInfo,
if (widget.additionalInfo case final Widget additionalInfo) ...<Widget>[
DefaultTextStyle(
style: coloredStyle,
maxLines: 1,
child: additionalInfo,
),
if (widget.trailing != null)
const SizedBox(width: _kAdditionalInfoToTrailing),
],
Expand Down
10 changes: 5 additions & 5 deletions packages/flutter/lib/src/cupertino/route.dart
Expand Up @@ -718,17 +718,17 @@ class _CupertinoBackGestureDetectorState<T> extends State<_CupertinoBackGestureD
assert(debugCheckHasDirectionality(context));
// For devices with notches, the drag area needs to be larger on the side
// that has the notch.
double dragAreaWidth = Directionality.of(context) == TextDirection.ltr ?
MediaQuery.paddingOf(context).left :
MediaQuery.paddingOf(context).right;
dragAreaWidth = max(dragAreaWidth, _kBackGestureWidth);
final double dragAreaWidth = switch (Directionality.of(context)) {
TextDirection.rtl => MediaQuery.paddingOf(context).right,
TextDirection.ltr => MediaQuery.paddingOf(context).left,
};
return Stack(
fit: StackFit.passthrough,
children: <Widget>[
widget.child,
PositionedDirectional(
start: 0.0,
width: dragAreaWidth,
width: max(dragAreaWidth, _kBackGestureWidth),
top: 0.0,
bottom: 0.0,
child: Listener(
Expand Down
14 changes: 6 additions & 8 deletions packages/flutter/lib/src/gestures/monodrag.dart
Expand Up @@ -607,14 +607,12 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer {
event is PointerMoveEvent ||
event is PointerPanZoomStartEvent ||
event is PointerPanZoomUpdateEvent)) {
final VelocityTracker tracker = _velocityTrackers[event.pointer]!;
if (event is PointerPanZoomStartEvent) {
tracker.addPosition(event.timeStamp, Offset.zero);
} else if (event is PointerPanZoomUpdateEvent) {
tracker.addPosition(event.timeStamp, event.pan);
} else {
tracker.addPosition(event.timeStamp, event.localPosition);
}
final Offset position = switch (event) {
PointerPanZoomStartEvent() => Offset.zero,
PointerPanZoomUpdateEvent() => event.pan,
_ => event.localPosition,
};
_velocityTrackers[event.pointer]!.addPosition(event.timeStamp, position);
}
if (event is PointerMoveEvent && event.buttons != _initialButtons) {
_giveUpPointer(event.pointer);
Expand Down
8 changes: 4 additions & 4 deletions packages/flutter/lib/src/material/calendar_date_picker.dart
Expand Up @@ -325,10 +325,10 @@ class _CalendarDatePickerState extends State<CalendarDatePicker> {
_DatePickerModeToggleButton(
mode: _mode,
title: _localizations.formatMonthYear(_currentDisplayedMonthDate),
onTitlePressed: () {
// Toggle the day/year mode.
_handleModeChanged(_mode == DatePickerMode.day ? DatePickerMode.year : DatePickerMode.day);
},
onTitlePressed: () => _handleModeChanged(switch (_mode) {
DatePickerMode.day => DatePickerMode.year,
DatePickerMode.year => DatePickerMode.day,
}),
),
],
);
Expand Down
8 changes: 4 additions & 4 deletions packages/flutter/lib/src/material/checkbox.dart
Expand Up @@ -765,10 +765,10 @@ class _CheckboxPainter extends ToggleablePainter {

final Paint strokePaint = _createStrokePaint();
final Offset origin = size / 2.0 - const Size.square(_kEdgeSize) / 2.0 as Offset;
final AnimationStatus status = position.status;
final double tNormalized = status == AnimationStatus.forward || status == AnimationStatus.completed
? position.value
: 1.0 - position.value;
final double tNormalized = switch (position.status) {
AnimationStatus.forward || AnimationStatus.completed => position.value,
AnimationStatus.reverse || AnimationStatus.dismissed => 1.0 - position.value,
};

// Four cases: false to null, false to true, null to false, true to false
if (previousValue == false || value == false) {
Expand Down
9 changes: 4 additions & 5 deletions packages/flutter/lib/src/material/date_picker.dart
Expand Up @@ -2824,11 +2824,10 @@ class _InputDateRangePickerDialog extends StatelessWidget {
if (start == null || end == null) {
return localizations.unspecifiedDateRange;
}
if (Directionality.of(context) == TextDirection.ltr) {
return '$startText$endText';
} else {
return '$endText$startText';
}
return switch (Directionality.of(context)) {
TextDirection.rtl => '$endText$startText',
TextDirection.ltr => '$startText$endText',
};
}

@override
Expand Down
16 changes: 7 additions & 9 deletions packages/flutter/lib/src/material/icon_button.dart
Expand Up @@ -970,16 +970,14 @@ class _IconButtonM3 extends ButtonStyleButton {
@override
ButtonStyle? themeStyleOf(BuildContext context) {
final IconThemeData iconTheme = IconTheme.of(context);
final bool isDark = Theme.of(context).brightness == Brightness.dark;

bool isIconThemeDefault(Color? color) {
if (isDark) {
return identical(color, kDefaultIconLightColor);
}
return identical(color, kDefaultIconDarkColor);
}
final bool isDefaultColor = isIconThemeDefault(iconTheme.color);
final bool isDefaultSize = iconTheme.size == const IconThemeData.fallback().size;
final bool isDefaultColor = identical(
iconTheme.color,
switch (Theme.of(context).brightness) {
Brightness.light => kDefaultIconDarkColor,
Brightness.dark => kDefaultIconLightColor,
},
);

final ButtonStyle iconThemeStyle = IconButton.styleFrom(
foregroundColor: isDefaultColor ? null : iconTheme.color,
Expand Down
14 changes: 6 additions & 8 deletions packages/flutter/lib/src/material/ink_decoration.dart
Expand Up @@ -236,14 +236,12 @@ class Ink extends StatefulWidget {
final double? height;

EdgeInsetsGeometry get _paddingIncludingDecoration {
if (decoration == null) {
return padding ?? EdgeInsets.zero;
}
final EdgeInsetsGeometry decorationPadding = decoration!.padding;
if (padding == null) {
return decorationPadding;
}
return padding!.add(decorationPadding);
return switch ((padding, decoration?.padding)) {
(null, null) => EdgeInsets.zero,
(null, final EdgeInsetsGeometry padding) => padding,
(final EdgeInsetsGeometry padding, null) => padding,
_ => padding!.add(decoration!.padding),
};
}

@override
Expand Down
12 changes: 5 additions & 7 deletions packages/flutter/lib/src/material/input_decorator.dart
Expand Up @@ -287,13 +287,11 @@ class _Shaker extends AnimatedWidget {
double get translateX {
const double shakeDelta = 4.0;
final double t = animation.value;
if (t <= 0.25) {
return -t * shakeDelta;
} else if (t < 0.75) {
return (t - 0.5) * shakeDelta;
} else {
return (1.0 - t) * 4.0 * shakeDelta;
}
return shakeDelta * switch (t) {
<= 0.25 => -t,
< 0.75 => t - 0.5,
_ => (1.0 - t) * 4.0,
};
}

@override
Expand Down
16 changes: 8 additions & 8 deletions packages/flutter/lib/src/material/scrollbar.dart
Expand Up @@ -271,10 +271,10 @@ class _MaterialScrollbarState extends RawScrollbarState<_MaterialScrollbar> {
final Brightness brightness = _colorScheme.brightness;
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
if (showScrollbar && _trackVisibility.resolve(states)) {
return _scrollbarTheme.trackColor?.resolve(states)
?? (brightness == Brightness.light
? onSurface.withOpacity(0.03)
: onSurface.withOpacity(0.05));
return _scrollbarTheme.trackColor?.resolve(states) ?? switch (brightness) {
Brightness.light => onSurface.withOpacity(0.03),
Brightness.dark => onSurface.withOpacity(0.05),
};
}
return const Color(0x00000000);
});
Expand All @@ -285,10 +285,10 @@ class _MaterialScrollbarState extends RawScrollbarState<_MaterialScrollbar> {
final Brightness brightness = _colorScheme.brightness;
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
if (showScrollbar && _trackVisibility.resolve(states)) {
return _scrollbarTheme.trackBorderColor?.resolve(states)
?? (brightness == Brightness.light
? onSurface.withOpacity(0.1)
: onSurface.withOpacity(0.25));
return _scrollbarTheme.trackBorderColor?.resolve(states) ?? switch (brightness) {
Brightness.light => onSurface.withOpacity(0.1),
Brightness.dark => onSurface.withOpacity(0.25),
};
}
return const Color(0x00000000);
});
Expand Down

0 comments on commit 586e1fc

Please sign in to comment.