Reland "if chains → switch expressions" (#148634)

I did a goof a while back:

![late initialization error](https://github.com/flutter/flutter/assets/10457200/47dc423f-0dd0-4869-9a11-59cb7d1ea46e)

Now that [the bug is fixed](e9d403541f), I think we're good to re-implement the if-chains cleanup!

Related:

- https://github.com/flutter/flutter/pull/147793
- https://github.com/flutter/flutter/pull/148556
- https://github.com/flutter/flutter/issues/148548
This commit is contained in:
Nate
2024-05-20 11:43:00 -06:00
committed by GitHub
parent 333c076e53
commit 73ecc8a9b2
33 changed files with 194 additions and 297 deletions

View File

@@ -307,13 +307,11 @@ class _BackdropDemoState extends State<BackdropDemo> with SingleTickerProviderSt
}
final double flingVelocity = details.velocity.pixelsPerSecond.dy / _backdropHeight;
if (flingVelocity < 0.0) {
_controller.fling(velocity: math.max(2.0, -flingVelocity));
} else if (flingVelocity > 0.0) {
_controller.fling(velocity: math.min(-2.0, -flingVelocity));
} else {
_controller.fling(velocity: _controller.value < 0.5 ? -2.0 : 2.0);
}
_controller.fling(velocity: switch (flingVelocity) {
< 0.0 => math.max(2.0, -flingVelocity),
> 0.0 => math.min(-2.0, -flingVelocity),
_ => _controller.value < 0.5 ? -2.0 : 2.0,
});
}
// Stacks a BackdropPanel, which displays the selected category, on top

View File

@@ -184,21 +184,18 @@ class _ListDemoState extends State<ListDemo> {
}
Widget buildListTile(BuildContext context, String item) {
Widget? secondary;
if (_itemType == _MaterialListType.twoLine) {
secondary = const Text('Additional item information.');
} else if (_itemType == _MaterialListType.threeLine) {
secondary = const Text(
'Even more additional list item information appears on line three.',
);
}
final String? subtitle = switch (_itemType) {
_MaterialListType.oneLine || _MaterialListType.oneLineWithAvatar || null => null,
_MaterialListType.twoLine => 'Additional item information.',
_MaterialListType.threeLine => 'Even more additional list item information appears on line three.',
};
return MergeSemantics(
child: ListTile(
isThreeLine: _itemType == _MaterialListType.threeLine,
dense: _dense,
leading: _showAvatars != null ? ExcludeSemantics(child: CircleAvatar(child: Text(item))) : null,
title: Text('This item represents $item.'),
subtitle: secondary,
subtitle: subtitle != null ? Text(subtitle) : null,
trailing: _showIcons != null ? Icon(Icons.info, color: Theme.of(context).disabledColor) : null,
),
);

View File

@@ -246,13 +246,11 @@ class _BackdropState extends State<Backdrop> with SingleTickerProviderStateMixin
}
final double flingVelocity = details.velocity.pixelsPerSecond.dy / _backdropHeight;
if (flingVelocity < 0.0) {
_controller!.fling(velocity: math.max(2.0, -flingVelocity));
} else if (flingVelocity > 0.0) {
_controller!.fling(velocity: math.min(-2.0, -flingVelocity));
} else {
_controller!.fling(velocity: _controller!.value < 0.5 ? -2.0 : 2.0);
}
_controller!.fling(velocity: switch (flingVelocity) {
< 0.0 => math.max(2.0, -flingVelocity),
> 0.0 => math.min(-2.0, -flingVelocity),
_ => _controller!.value < 0.5 ? -2.0 : 2.0,
});
}
void _toggleFrontLayer() {

View File

@@ -337,22 +337,14 @@ class _HighlightSpan {
}
TextStyle? textStyle(SyntaxHighlighterStyle? style) {
if (type == _HighlightType.number) {
return style!.numberStyle;
} else if (type == _HighlightType.comment) {
return style!.commentStyle;
} else if (type == _HighlightType.keyword) {
return style!.keywordStyle;
} else if (type == _HighlightType.string) {
return style!.stringStyle;
} else if (type == _HighlightType.punctuation) {
return style!.punctuationStyle;
} else if (type == _HighlightType.klass) {
return style!.classStyle;
} else if (type == _HighlightType.constant) {
return style!.constantStyle;
} else {
return style!.baseStyle;
}
return switch (type) {
_HighlightType.number => style!.numberStyle,
_HighlightType.comment => style!.commentStyle,
_HighlightType.keyword => style!.keywordStyle,
_HighlightType.string => style!.stringStyle,
_HighlightType.punctuation => style!.punctuationStyle,
_HighlightType.klass => style!.classStyle,
_HighlightType.constant => style!.constantStyle,
};
}
}

View File

@@ -416,20 +416,15 @@ class _CardsDemoState extends State<CardsDemo> with RestorationMixin {
for (final TravelDestination destination in destinations(context))
Container(
margin: const EdgeInsets.only(bottom: 8),
child: (destination.cardType == CardType.standard)
? TravelDestinationItem(destination: destination)
: destination.cardType == CardType.tappable
? TappableTravelDestinationItem(
destination: destination)
: SelectableTravelDestinationItem(
destination: destination,
isSelected: _isSelected.value,
onSelected: () {
setState(() {
_isSelected.value = !_isSelected.value;
});
},
),
child: switch (destination.cardType) {
CardType.standard => TravelDestinationItem(destination: destination),
CardType.tappable => TappableTravelDestinationItem(destination: destination),
CardType.selectable => SelectableTravelDestinationItem(
destination: destination,
isSelected: _isSelected.value,
onSelected: () => setState(() { _isSelected.value = !_isSelected.value; }),
),
},
),
],
),

View File

@@ -69,13 +69,10 @@ class TwoPaneDemoState extends State<TwoPaneDemo> with RestorationMixin {
),
endPane: DetailsPane(
selectedIndex: _currentIndex.value,
onClose: widget.type == TwoPaneDemoType.smallScreen
? () {
setState(() {
_currentIndex.value = -1;
});
}
: null,
onClose: switch (widget.type) {
TwoPaneDemoType.smallScreen => () => setState(() { _currentIndex.value = -1; }),
TwoPaneDemoType.foldable || TwoPaneDemoType.tablet => null,
},
),
),
);
@@ -190,11 +187,11 @@ class SimulateScreen extends StatelessWidget {
),
padding: const EdgeInsets.all(14),
child: AspectRatio(
aspectRatio: type == TwoPaneDemoType.foldable
? foldableAspectRatio
: type == TwoPaneDemoType.tablet
? tabletAspectRatio
: singleScreenAspectRatio,
aspectRatio: switch (type) {
TwoPaneDemoType.foldable => foldableAspectRatio,
TwoPaneDemoType.tablet => tabletAspectRatio,
TwoPaneDemoType.smallScreen => singleScreenAspectRatio,
},
child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
final Size size = Size(constraints.maxWidth, constraints.maxHeight);
final Size hingeSize = Size(size.width * hingeProportion, size.height);

View File

@@ -35,11 +35,11 @@ class _SettingsIconPainter extends CustomPainter {
/// The icon is aligned to the bottom-start corner.
void _computeCenterAndScaling(Size size) {
_scaling = min(size.width / unitWidth, size.height / unitHeight);
_center = Directionality.of(context) == TextDirection.ltr
? Offset(
unitWidth * _scaling / 2, size.height - unitHeight * _scaling / 2)
: Offset(size.width - unitWidth * _scaling / 2,
size.height - unitHeight * _scaling / 2);
final double dx = switch (Directionality.of(context)) {
TextDirection.rtl => size.width - unitWidth * _scaling / 2,
TextDirection.ltr => unitWidth * _scaling / 2,
};
_center = Offset(dx, size.height - unitHeight * _scaling / 2);
}
/// Transforms an offset in relative units into an offset in logical pixels.

View File

@@ -103,21 +103,16 @@ bool _findBGR10Color(
return foundDeepRed;
}
bool _findColor(List<Object?> result, List<double> color) {
bool _findColor(List<dynamic> result, List<double> color) {
expect(result, isNotNull);
expect(result.length, 4);
final int width = (result[0] as int?)!;
final int height = (result[1] as int?)!;
final String format = (result[2] as String?)!;
if (format == 'MTLPixelFormatBGR10_XR') {
return _findBGR10Color((result[3] as Uint8List?)!, width, height, color);
} else if (format == 'MTLPixelFormatBGRA10_XR') {
return _findBGRA10Color((result[3] as Uint8List?)!, width, height, color);
} else if (format == 'MTLPixelFormatRGBA16Float') {
return _findRGBAF16Color((result[3] as Uint8List?)!, width, height, color);
} else {
fail('Unsupported pixel format: $format');
}
final [int width, int height, String format, Uint8List bytes] = result;
return switch (format) {
'MTLPixelFormatBGR10_XR' => _findBGR10Color(bytes, width, height, color),
'MTLPixelFormatBGRA10_XR' => _findBGRA10Color(bytes, width, height, color),
'MTLPixelFormatRGBA16Float' => _findRGBAF16Color(bytes, width, height, color),
_ => fail('Unsupported pixel format: $format'),
};
}
void main() {