switch expressions: finale (#148711)

### fixes #136139

<br>

<details open> <summary><b>getting sentimental in the PR description</b> (click to collapse)<br><br></summary>

The past 7 months have been quite the journey—I made some huge blunders and some huge accomplishments—a very fun time overall.

I really appreciate the people who took the time to perform code review for my refactoring shenanigans: **christopherfujino**, **andrewkolos**, **LongCatIsLooong**, **gspencergoog**, **loic-sharma**, **Piinks**, **bernaferrari**, **bartekpacia**, **bleroux**, **kevmoo**, **rakudrama**, **XilaiZhang**, **QuncCccccc**, **MominRaza**, and **victorsanni**.

And a huge shoutout to 2 individuals:
- @justinmc, for offering to sponsor me for commit access (words could not describe my excitement)
- @goderbauer, for being super duper proactive and consistent with code review

<br>

</details>

This pull request makes 13 "switch statements → switch expressions" PRs in total, reducing the LOC in this repo by **1,974**!

From now on, I'll make sure to request a test exemption for each refactoring PR 🙂
This commit is contained in:
Nate
2024-05-21 10:18:05 -06:00
committed by GitHub
parent 454dd7e29c
commit 870c5541c3
22 changed files with 214 additions and 466 deletions

View File

@@ -47,20 +47,14 @@ class StocksAppState extends State<StocksApp> {
}
ThemeData get theme {
switch (_configuration.stockMode) {
case StockMode.optimistic:
return ThemeData(
useMaterial3: false,
brightness: Brightness.light,
primarySwatch: Colors.purple,
);
case StockMode.pessimistic:
return ThemeData(
useMaterial3: false,
brightness: Brightness.dark,
primarySwatch: Colors.purple,
);
}
return ThemeData(
useMaterial3: false,
brightness: switch (_configuration.stockMode) {
StockMode.optimistic => Brightness.light,
StockMode.pessimistic => Brightness.dark,
},
primarySwatch: Colors.purple,
);
}
Route<dynamic>? _getRoute(RouteSettings settings) {

View File

@@ -152,21 +152,13 @@ String getNewPrLink({
}) {
assert(state.releaseChannel.isNotEmpty);
assert(state.releaseVersion.isNotEmpty);
late final String candidateBranch;
late final String workingBranch;
late final String repoLabel;
switch (repoName) {
case 'flutter':
candidateBranch = state.framework.candidateBranch;
workingBranch = state.framework.workingBranch;
repoLabel = 'Framework';
case 'engine':
candidateBranch = state.engine.candidateBranch;
workingBranch = state.engine.workingBranch;
repoLabel = 'Engine';
default:
throw ConductorException('Expected repoName to be one of flutter or engine but got $repoName.');
}
final (pb.Repository repository, String repoLabel) = switch (repoName) {
'flutter' => (state.framework, 'Framework'),
'engine' => (state.engine, 'Engine'),
_ => throw ConductorException('Expected repoName to be one of flutter or engine but got $repoName.'),
};
final String candidateBranch = repository.candidateBranch;
final String workingBranch = repository.workingBranch;
assert(candidateBranch.isNotEmpty);
assert(workingBranch.isNotEmpty);
final String title = '[flutter_releases] Flutter ${state.releaseChannel} '

View File

@@ -377,22 +377,17 @@ class _CardsDemoState extends State<CardsDemo> {
child: ListView(
primary: true,
padding: const EdgeInsets.only(top: 8.0, left: 8.0, right: 8.0),
children: destinations.map<Widget>((TravelDestination destination) {
Widget? child;
switch (destination.type) {
case CardDemoType.standard:
child = TravelDestinationItem(destination: destination, shape: _shape);
case CardDemoType.tappable:
child = TappableTravelDestinationItem(destination: destination, shape: _shape);
case CardDemoType.selectable:
child = SelectableTravelDestinationItem(destination: destination, shape: _shape);
}
return Container(
margin: const EdgeInsets.only(bottom: 8.0),
child: child,
);
}).toList(),
children: <Widget>[
for (final TravelDestination destination in destinations)
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: switch (destination.type) {
CardDemoType.standard => TravelDestinationItem(destination: destination, shape: _shape),
CardDemoType.tappable => TappableTravelDestinationItem(destination: destination, shape: _shape),
CardDemoType.selectable => SelectableTravelDestinationItem(destination: destination, shape: _shape),
},
),
],
),
),
);

View File

@@ -100,20 +100,13 @@ class CutCornersBorder extends OutlineInputBorder {
if (gapStart == null || gapExtent <= 0.0 || gapPercentage == 0.0) {
canvas.drawPath(_notchedCornerPath(outer.middleRect), paint);
} else {
final double? extent = lerpDouble(0.0, gapExtent + gapPadding * 2.0, gapPercentage);
switch (textDirection) {
case TextDirection.rtl: {
final Path path = _notchedCornerPath(outer.middleRect, gapStart + gapPadding - extent!, extent);
canvas.drawPath(path, paint);
break;
}
case TextDirection.ltr: {
final Path path = _notchedCornerPath(outer.middleRect, gapStart - gapPadding, extent);
canvas.drawPath(path, paint);
break;
}
case null:
break;
final double extent = lerpDouble(0.0, gapExtent + gapPadding * 2.0, gapPercentage)!;
if (textDirection != null) {
final double start = switch (textDirection) {
TextDirection.rtl => gapStart + gapPadding - extent,
TextDirection.ltr => gapStart - gapPadding,
};
canvas.drawPath(_notchedCornerPath(outer.middleRect, start, extent), paint);
}
}
}

View File

@@ -298,37 +298,26 @@ class _CupertinoAlertDemoState extends State<CupertinoAlertDemo>
),
child: Builder(
builder: (BuildContext context) {
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
final Widget showAlertButton = CupertinoButton.filled(
onPressed: () => switch (widget.type) {
AlertDemoType.alert => _alertDialogRoute,
AlertDemoType.alertTitle => _alertWithTitleDialogRoute,
AlertDemoType.alertButtons => _alertWithButtonsDialogRoute,
AlertDemoType.alertButtonsOnly => _alertWithButtonsOnlyDialogRoute,
AlertDemoType.actionSheet => _modalPopupRoute,
}.present(),
child: Text(localizations.cupertinoShowAlert),
);
return Column(
children: <Widget>[
Expanded(
child: Center(
child: CupertinoButton.filled(
onPressed: () {
switch (widget.type) {
case AlertDemoType.alert:
_alertDialogRoute.present();
case AlertDemoType.alertTitle:
_alertWithTitleDialogRoute.present();
case AlertDemoType.alertButtons:
_alertWithButtonsDialogRoute.present();
case AlertDemoType.alertButtonsOnly:
_alertWithButtonsOnlyDialogRoute.present();
case AlertDemoType.actionSheet:
_modalPopupRoute.present();
}
},
child: Text(
GalleryLocalizations.of(context)!.cupertinoShowAlert,
),
),
),
),
Expanded(child: Center(child: showAlertButton)),
if (lastSelectedValue.value != null)
Padding(
padding: const EdgeInsets.all(16),
child: Text(
GalleryLocalizations.of(context)!
.dialogSelectedOption(lastSelectedValue.value!),
localizations.dialogSelectedOption(lastSelectedValue.value!),
style: CupertinoTheme.of(context).textTheme.textStyle,
textAlign: TextAlign.center,
),

View File

@@ -24,26 +24,18 @@ class ButtonDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
Widget? buttons;
switch (type) {
case ButtonDemoType.text:
buttons = _TextButtonDemo();
case ButtonDemoType.elevated:
buttons = _ElevatedButtonDemo();
case ButtonDemoType.outlined:
buttons = _OutlinedButtonDemo();
case ButtonDemoType.toggle:
buttons = _ToggleButtonsDemo();
case ButtonDemoType.floating:
buttons = _FloatingActionButtonDemo();
}
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text(_title(context)),
),
body: buttons,
body: switch (type) {
ButtonDemoType.text => _TextButtonDemo(),
ButtonDemoType.elevated => _ElevatedButtonDemo(),
ButtonDemoType.outlined => _OutlinedButtonDemo(),
ButtonDemoType.toggle => _ToggleButtonsDemo(),
ButtonDemoType.floating => _FloatingActionButtonDemo(),
},
);
}
}

View File

@@ -26,24 +26,17 @@ class ChipDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
Widget? buttons;
switch (type) {
case ChipDemoType.action:
buttons = _ActionChipDemo();
case ChipDemoType.choice:
buttons = _ChoiceChipDemo();
case ChipDemoType.filter:
buttons = _FilterChipDemo();
case ChipDemoType.input:
buttons = _InputChipDemo();
}
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text(_title(context)),
),
body: buttons,
body: switch (type) {
ChipDemoType.action => _ActionChipDemo(),
ChipDemoType.choice => _ChoiceChipDemo(),
ChipDemoType.filter => _FilterChipDemo(),
ChipDemoType.input => _InputChipDemo(),
},
);
}
}

View File

@@ -11,33 +11,23 @@ class DividerDemo extends StatelessWidget {
final DividerDemoType type;
String _title(BuildContext context) {
switch (type) {
case DividerDemoType.horizontal:
return GalleryLocalizations.of(context)!.demoDividerTitle;
case DividerDemoType.vertical:
return GalleryLocalizations.of(context)!.demoVerticalDividerTitle;
}
}
@override
Widget build(BuildContext context) {
late Widget dividers;
switch (type) {
case DividerDemoType.horizontal:
dividers = _HorizontalDividerDemo();
case DividerDemoType.vertical:
dividers = _VerticalDividerDemo();
}
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text(
_title(context),
switch (type) {
DividerDemoType.horizontal => localizations.demoDividerTitle,
DividerDemoType.vertical => localizations.demoVerticalDividerTitle,
},
),
),
body: dividers,
body: switch (type) {
DividerDemoType.horizontal => _HorizontalDividerDemo(),
DividerDemoType.vertical => _VerticalDividerDemo(),
},
);
}
}

View File

@@ -172,14 +172,12 @@ class _PickerDemoState extends State<PickerDemo> with RestorationMixin {
}
String get _labelText {
switch (widget.type) {
case PickerDemoType.date:
return DateFormat.yMMMd().format(_fromDate.value);
case PickerDemoType.time:
return _fromTime.value.format(context);
case PickerDemoType.range:
return '${DateFormat.yMMMd().format(_startDate.value)} - ${DateFormat.yMMMd().format(_endDate.value)}';
}
final DateFormat yMMMd = DateFormat.yMMMd();
return switch (widget.type) {
PickerDemoType.date => yMMMd.format(_fromDate.value),
PickerDemoType.time => _fromTime.value.format(context),
PickerDemoType.range => '${yMMMd.format(_startDate.value)} - ${yMMMd.format(_endDate.value)}',
};
}
@override
@@ -199,16 +197,11 @@ class _PickerDemoState extends State<PickerDemo> with RestorationMixin {
Text(_labelText),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
switch (widget.type) {
case PickerDemoType.date:
_restorableDatePickerRouteFuture.present();
case PickerDemoType.time:
_restorableTimePickerRouteFuture.present();
case PickerDemoType.range:
_restorableDateRangePickerRouteFuture.present();
}
},
onPressed: () => switch (widget.type) {
PickerDemoType.date => _restorableDatePickerRouteFuture,
PickerDemoType.time => _restorableTimePickerRouteFuture,
PickerDemoType.range => _restorableDateRangePickerRouteFuture,
}.present(),
child: Text(
GalleryLocalizations.of(context)!.demoPickersShowPicker,
),

View File

@@ -27,22 +27,16 @@ class SelectionControlsDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
Widget? controls;
switch (type) {
case SelectionControlsDemoType.checkbox:
controls = _CheckboxDemo();
case SelectionControlsDemoType.radio:
controls = _RadioDemo();
case SelectionControlsDemoType.switches:
controls = _SwitchDemo();
}
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text(_title(context)),
),
body: controls,
body: switch (type) {
SelectionControlsDemoType.checkbox => _CheckboxDemo(),
SelectionControlsDemoType.radio => _RadioDemo(),
SelectionControlsDemoType.switches => _SwitchDemo(),
},
);
}
}

View File

@@ -27,21 +27,16 @@ class SlidersDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
Widget sliders;
switch (type) {
case SlidersDemoType.sliders:
sliders = _Sliders();
case SlidersDemoType.rangeSliders:
sliders = _RangeSliders();
case SlidersDemoType.customSliders:
sliders = _CustomSliders();
}
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text(_title(context)),
),
body: sliders,
body: switch (type) {
SlidersDemoType.sliders => _Sliders(),
SlidersDemoType.rangeSliders => _RangeSliders(),
SlidersDemoType.customSliders => _CustomSliders(),
},
);
}
}
@@ -345,25 +340,13 @@ class _CustomRangeThumbShape extends RangeSliderThumbShape {
);
final double size = _thumbSize * sizeTween.evaluate(enableAnimation);
Path thumbPath;
switch (textDirection!) {
case TextDirection.rtl:
switch (thumb!) {
case Thumb.start:
thumbPath = _rightTriangle(size, center);
case Thumb.end:
thumbPath = _leftTriangle(size, center);
}
case TextDirection.ltr:
switch (thumb!) {
case Thumb.start:
thumbPath = _leftTriangle(size, center);
case Thumb.end:
thumbPath = _rightTriangle(size, center);
}
}
canvas.drawPath(
thumbPath,
switch ((textDirection!, thumb!)) {
(TextDirection.rtl, Thumb.start) => _rightTriangle(size, center),
(TextDirection.rtl, Thumb.end) => _leftTriangle(size, center),
(TextDirection.ltr, Thumb.start) => _leftTriangle(size, center),
(TextDirection.ltr, Thumb.end) => _rightTriangle(size, center),
},
Paint()..color = colorTween.evaluate(enableAnimation)!,
);
}

View File

@@ -102,23 +102,12 @@ class CutCornersBorder extends OutlineInputBorder {
if (gapStart == null || gapExtent <= 0 || gapPercentage == 0) {
canvas.drawPath(_notchedCornerPath(outer.middleRect), paint);
} else {
final double? extent = lerpDouble(0.0, gapExtent + gapPadding * 2, gapPercentage);
switch (textDirection!) {
case TextDirection.rtl:
{
final Path path = _notchedCornerPath(
outer.middleRect, gapStart + gapPadding - extent!, extent);
canvas.drawPath(path, paint);
break;
}
case TextDirection.ltr:
{
final Path path = _notchedCornerPath(
outer.middleRect, gapStart - gapPadding, extent!);
canvas.drawPath(path, paint);
break;
}
}
final double extent = lerpDouble(0.0, gapExtent + gapPadding * 2, gapPercentage)!;
final double start = switch (textDirection!) {
TextDirection.rtl => gapStart + gapPadding - extent,
TextDirection.ltr => gapStart - gapPadding,
};
canvas.drawPath(_notchedCornerPath(outer.middleRect, start, extent), paint);
}
}
}

View File

@@ -400,17 +400,12 @@ $factoryDeclaration
///
/// Used by [generateGetter] below.
String generateType(Map<String, dynamic>? attributes) {
bool optional = false;
String type = 'String';
if (attributes != null) {
optional = attributes.containsKey('optional');
switch (attributes['x-flutter-type'] as String?) {
case 'icuShortTimePattern':
type = 'TimeOfDayFormat';
case 'scriptCategory':
type = 'ScriptCategory';
}
}
final bool optional = attributes?.containsKey('optional') ?? false;
final String type = switch (attributes?['x-flutter-type']) {
'icuShortTimePattern' => 'TimeOfDayFormat',
'scriptCategory' => 'ScriptCategory',
_ => 'String',
};
return type + (optional ? '?' : '');
}