Skip to content

Commit 870c554

Browse files
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 �
1 parent 454dd7e commit 870c554

File tree

22 files changed

+214
-466
lines changed

22 files changed

+214
-466
lines changed

dev/benchmarks/test_apps/stocks/lib/main.dart

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,14 @@ class StocksAppState extends State<StocksApp> {
4747
}
4848

4949
ThemeData get theme {
50-
switch (_configuration.stockMode) {
51-
case StockMode.optimistic:
52-
return ThemeData(
53-
useMaterial3: false,
54-
brightness: Brightness.light,
55-
primarySwatch: Colors.purple,
56-
);
57-
case StockMode.pessimistic:
58-
return ThemeData(
59-
useMaterial3: false,
60-
brightness: Brightness.dark,
61-
primarySwatch: Colors.purple,
62-
);
63-
}
50+
return ThemeData(
51+
useMaterial3: false,
52+
brightness: switch (_configuration.stockMode) {
53+
StockMode.optimistic => Brightness.light,
54+
StockMode.pessimistic => Brightness.dark,
55+
},
56+
primarySwatch: Colors.purple,
57+
);
6458
}
6559

6660
Route<dynamic>? _getRoute(RouteSettings settings) {

dev/conductor/core/lib/src/globals.dart

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,13 @@ String getNewPrLink({
152152
}) {
153153
assert(state.releaseChannel.isNotEmpty);
154154
assert(state.releaseVersion.isNotEmpty);
155-
late final String candidateBranch;
156-
late final String workingBranch;
157-
late final String repoLabel;
158-
switch (repoName) {
159-
case 'flutter':
160-
candidateBranch = state.framework.candidateBranch;
161-
workingBranch = state.framework.workingBranch;
162-
repoLabel = 'Framework';
163-
case 'engine':
164-
candidateBranch = state.engine.candidateBranch;
165-
workingBranch = state.engine.workingBranch;
166-
repoLabel = 'Engine';
167-
default:
168-
throw ConductorException('Expected repoName to be one of flutter or engine but got $repoName.');
169-
}
155+
final (pb.Repository repository, String repoLabel) = switch (repoName) {
156+
'flutter' => (state.framework, 'Framework'),
157+
'engine' => (state.engine, 'Engine'),
158+
_ => throw ConductorException('Expected repoName to be one of flutter or engine but got $repoName.'),
159+
};
160+
final String candidateBranch = repository.candidateBranch;
161+
final String workingBranch = repository.workingBranch;
170162
assert(candidateBranch.isNotEmpty);
171163
assert(workingBranch.isNotEmpty);
172164
final String title = '[flutter_releases] Flutter ${state.releaseChannel} '

dev/integration_tests/flutter_gallery/lib/demo/material/cards_demo.dart

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -377,22 +377,17 @@ class _CardsDemoState extends State<CardsDemo> {
377377
child: ListView(
378378
primary: true,
379379
padding: const EdgeInsets.only(top: 8.0, left: 8.0, right: 8.0),
380-
children: destinations.map<Widget>((TravelDestination destination) {
381-
Widget? child;
382-
switch (destination.type) {
383-
case CardDemoType.standard:
384-
child = TravelDestinationItem(destination: destination, shape: _shape);
385-
case CardDemoType.tappable:
386-
child = TappableTravelDestinationItem(destination: destination, shape: _shape);
387-
case CardDemoType.selectable:
388-
child = SelectableTravelDestinationItem(destination: destination, shape: _shape);
389-
}
390-
391-
return Container(
392-
margin: const EdgeInsets.only(bottom: 8.0),
393-
child: child,
394-
);
395-
}).toList(),
380+
children: <Widget>[
381+
for (final TravelDestination destination in destinations)
382+
Padding(
383+
padding: const EdgeInsets.only(bottom: 8.0),
384+
child: switch (destination.type) {
385+
CardDemoType.standard => TravelDestinationItem(destination: destination, shape: _shape),
386+
CardDemoType.tappable => TappableTravelDestinationItem(destination: destination, shape: _shape),
387+
CardDemoType.selectable => SelectableTravelDestinationItem(destination: destination, shape: _shape),
388+
},
389+
),
390+
],
396391
),
397392
),
398393
);

dev/integration_tests/flutter_gallery/lib/demo/shrine/supplemental/cut_corners_border.dart

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,13 @@ class CutCornersBorder extends OutlineInputBorder {
100100
if (gapStart == null || gapExtent <= 0.0 || gapPercentage == 0.0) {
101101
canvas.drawPath(_notchedCornerPath(outer.middleRect), paint);
102102
} else {
103-
final double? extent = lerpDouble(0.0, gapExtent + gapPadding * 2.0, gapPercentage);
104-
switch (textDirection) {
105-
case TextDirection.rtl: {
106-
final Path path = _notchedCornerPath(outer.middleRect, gapStart + gapPadding - extent!, extent);
107-
canvas.drawPath(path, paint);
108-
break;
109-
}
110-
case TextDirection.ltr: {
111-
final Path path = _notchedCornerPath(outer.middleRect, gapStart - gapPadding, extent);
112-
canvas.drawPath(path, paint);
113-
break;
114-
}
115-
case null:
116-
break;
103+
final double extent = lerpDouble(0.0, gapExtent + gapPadding * 2.0, gapPercentage)!;
104+
if (textDirection != null) {
105+
final double start = switch (textDirection) {
106+
TextDirection.rtl => gapStart + gapPadding - extent,
107+
TextDirection.ltr => gapStart - gapPadding,
108+
};
109+
canvas.drawPath(_notchedCornerPath(outer.middleRect, start, extent), paint);
117110
}
118111
}
119112
}

dev/integration_tests/new_gallery/lib/demos/cupertino/cupertino_alert_demo.dart

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -298,37 +298,26 @@ class _CupertinoAlertDemoState extends State<CupertinoAlertDemo>
298298
),
299299
child: Builder(
300300
builder: (BuildContext context) {
301+
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
302+
final Widget showAlertButton = CupertinoButton.filled(
303+
onPressed: () => switch (widget.type) {
304+
AlertDemoType.alert => _alertDialogRoute,
305+
AlertDemoType.alertTitle => _alertWithTitleDialogRoute,
306+
AlertDemoType.alertButtons => _alertWithButtonsDialogRoute,
307+
AlertDemoType.alertButtonsOnly => _alertWithButtonsOnlyDialogRoute,
308+
AlertDemoType.actionSheet => _modalPopupRoute,
309+
}.present(),
310+
child: Text(localizations.cupertinoShowAlert),
311+
);
312+
301313
return Column(
302314
children: <Widget>[
303-
Expanded(
304-
child: Center(
305-
child: CupertinoButton.filled(
306-
onPressed: () {
307-
switch (widget.type) {
308-
case AlertDemoType.alert:
309-
_alertDialogRoute.present();
310-
case AlertDemoType.alertTitle:
311-
_alertWithTitleDialogRoute.present();
312-
case AlertDemoType.alertButtons:
313-
_alertWithButtonsDialogRoute.present();
314-
case AlertDemoType.alertButtonsOnly:
315-
_alertWithButtonsOnlyDialogRoute.present();
316-
case AlertDemoType.actionSheet:
317-
_modalPopupRoute.present();
318-
}
319-
},
320-
child: Text(
321-
GalleryLocalizations.of(context)!.cupertinoShowAlert,
322-
),
323-
),
324-
),
325-
),
315+
Expanded(child: Center(child: showAlertButton)),
326316
if (lastSelectedValue.value != null)
327317
Padding(
328318
padding: const EdgeInsets.all(16),
329319
child: Text(
330-
GalleryLocalizations.of(context)!
331-
.dialogSelectedOption(lastSelectedValue.value!),
320+
localizations.dialogSelectedOption(lastSelectedValue.value!),
332321
style: CupertinoTheme.of(context).textTheme.textStyle,
333322
textAlign: TextAlign.center,
334323
),

dev/integration_tests/new_gallery/lib/demos/material/button_demo.dart

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,18 @@ class ButtonDemo extends StatelessWidget {
2424

2525
@override
2626
Widget build(BuildContext context) {
27-
Widget? buttons;
28-
switch (type) {
29-
case ButtonDemoType.text:
30-
buttons = _TextButtonDemo();
31-
case ButtonDemoType.elevated:
32-
buttons = _ElevatedButtonDemo();
33-
case ButtonDemoType.outlined:
34-
buttons = _OutlinedButtonDemo();
35-
case ButtonDemoType.toggle:
36-
buttons = _ToggleButtonsDemo();
37-
case ButtonDemoType.floating:
38-
buttons = _FloatingActionButtonDemo();
39-
}
40-
4127
return Scaffold(
4228
appBar: AppBar(
4329
automaticallyImplyLeading: false,
4430
title: Text(_title(context)),
4531
),
46-
body: buttons,
32+
body: switch (type) {
33+
ButtonDemoType.text => _TextButtonDemo(),
34+
ButtonDemoType.elevated => _ElevatedButtonDemo(),
35+
ButtonDemoType.outlined => _OutlinedButtonDemo(),
36+
ButtonDemoType.toggle => _ToggleButtonsDemo(),
37+
ButtonDemoType.floating => _FloatingActionButtonDemo(),
38+
},
4739
);
4840
}
4941
}

dev/integration_tests/new_gallery/lib/demos/material/chip_demo.dart

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,17 @@ class ChipDemo extends StatelessWidget {
2626

2727
@override
2828
Widget build(BuildContext context) {
29-
Widget? buttons;
30-
switch (type) {
31-
case ChipDemoType.action:
32-
buttons = _ActionChipDemo();
33-
case ChipDemoType.choice:
34-
buttons = _ChoiceChipDemo();
35-
case ChipDemoType.filter:
36-
buttons = _FilterChipDemo();
37-
case ChipDemoType.input:
38-
buttons = _InputChipDemo();
39-
}
40-
4129
return Scaffold(
4230
appBar: AppBar(
4331
automaticallyImplyLeading: false,
4432
title: Text(_title(context)),
4533
),
46-
body: buttons,
34+
body: switch (type) {
35+
ChipDemoType.action => _ActionChipDemo(),
36+
ChipDemoType.choice => _ChoiceChipDemo(),
37+
ChipDemoType.filter => _FilterChipDemo(),
38+
ChipDemoType.input => _InputChipDemo(),
39+
},
4740
);
4841
}
4942
}

dev/integration_tests/new_gallery/lib/demos/material/divider_demo.dart

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,23 @@ class DividerDemo extends StatelessWidget {
1111

1212
final DividerDemoType type;
1313

14-
String _title(BuildContext context) {
15-
switch (type) {
16-
case DividerDemoType.horizontal:
17-
return GalleryLocalizations.of(context)!.demoDividerTitle;
18-
case DividerDemoType.vertical:
19-
return GalleryLocalizations.of(context)!.demoVerticalDividerTitle;
20-
}
21-
}
22-
2314
@override
2415
Widget build(BuildContext context) {
25-
late Widget dividers;
26-
switch (type) {
27-
case DividerDemoType.horizontal:
28-
dividers = _HorizontalDividerDemo();
29-
case DividerDemoType.vertical:
30-
dividers = _VerticalDividerDemo();
31-
}
32-
16+
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
3317
return Scaffold(
3418
appBar: AppBar(
3519
automaticallyImplyLeading: false,
3620
title: Text(
37-
_title(context),
21+
switch (type) {
22+
DividerDemoType.horizontal => localizations.demoDividerTitle,
23+
DividerDemoType.vertical => localizations.demoVerticalDividerTitle,
24+
},
3825
),
3926
),
40-
body: dividers,
27+
body: switch (type) {
28+
DividerDemoType.horizontal => _HorizontalDividerDemo(),
29+
DividerDemoType.vertical => _VerticalDividerDemo(),
30+
},
4131
);
4232
}
4333
}

dev/integration_tests/new_gallery/lib/demos/material/picker_demo.dart

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,12 @@ class _PickerDemoState extends State<PickerDemo> with RestorationMixin {
172172
}
173173

174174
String get _labelText {
175-
switch (widget.type) {
176-
case PickerDemoType.date:
177-
return DateFormat.yMMMd().format(_fromDate.value);
178-
case PickerDemoType.time:
179-
return _fromTime.value.format(context);
180-
case PickerDemoType.range:
181-
return '${DateFormat.yMMMd().format(_startDate.value)} - ${DateFormat.yMMMd().format(_endDate.value)}';
182-
}
175+
final DateFormat yMMMd = DateFormat.yMMMd();
176+
return switch (widget.type) {
177+
PickerDemoType.date => yMMMd.format(_fromDate.value),
178+
PickerDemoType.time => _fromTime.value.format(context),
179+
PickerDemoType.range => '${yMMMd.format(_startDate.value)} - ${yMMMd.format(_endDate.value)}',
180+
};
183181
}
184182

185183
@override
@@ -199,16 +197,11 @@ class _PickerDemoState extends State<PickerDemo> with RestorationMixin {
199197
Text(_labelText),
200198
const SizedBox(height: 16),
201199
ElevatedButton(
202-
onPressed: () {
203-
switch (widget.type) {
204-
case PickerDemoType.date:
205-
_restorableDatePickerRouteFuture.present();
206-
case PickerDemoType.time:
207-
_restorableTimePickerRouteFuture.present();
208-
case PickerDemoType.range:
209-
_restorableDateRangePickerRouteFuture.present();
210-
}
211-
},
200+
onPressed: () => switch (widget.type) {
201+
PickerDemoType.date => _restorableDatePickerRouteFuture,
202+
PickerDemoType.time => _restorableTimePickerRouteFuture,
203+
PickerDemoType.range => _restorableDateRangePickerRouteFuture,
204+
}.present(),
212205
child: Text(
213206
GalleryLocalizations.of(context)!.demoPickersShowPicker,
214207
),

dev/integration_tests/new_gallery/lib/demos/material/selection_controls_demo.dart

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,16 @@ class SelectionControlsDemo extends StatelessWidget {
2727

2828
@override
2929
Widget build(BuildContext context) {
30-
Widget? controls;
31-
switch (type) {
32-
case SelectionControlsDemoType.checkbox:
33-
controls = _CheckboxDemo();
34-
case SelectionControlsDemoType.radio:
35-
controls = _RadioDemo();
36-
case SelectionControlsDemoType.switches:
37-
controls = _SwitchDemo();
38-
}
39-
4030
return Scaffold(
4131
appBar: AppBar(
4232
automaticallyImplyLeading: false,
4333
title: Text(_title(context)),
4434
),
45-
body: controls,
35+
body: switch (type) {
36+
SelectionControlsDemoType.checkbox => _CheckboxDemo(),
37+
SelectionControlsDemoType.radio => _RadioDemo(),
38+
SelectionControlsDemoType.switches => _SwitchDemo(),
39+
},
4640
);
4741
}
4842
}

0 commit comments

Comments
 (0)