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

@@ -35,33 +35,17 @@ class _MyWidgetState extends State<MyWidget> {
AxisDirection _axisDirection = AxisDirection.down;
Widget _getArrows() {
final Widget arrow;
switch (_axisDirection) {
case AxisDirection.up:
arrow = const Icon(Icons.arrow_upward_rounded);
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
case AxisDirection.down:
arrow = const Icon(Icons.arrow_downward_rounded);
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
case AxisDirection.left:
arrow = const Icon(Icons.arrow_back_rounded);
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
case AxisDirection.right:
arrow = const Icon(Icons.arrow_forward_rounded);
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
}
final Widget arrow = switch (_axisDirection) {
AxisDirection.up => const Icon(Icons.arrow_upward_rounded),
AxisDirection.down => const Icon(Icons.arrow_downward_rounded),
AxisDirection.left => const Icon(Icons.arrow_back_rounded),
AxisDirection.right => const Icon(Icons.arrow_forward_rounded),
};
return Flex(
direction: flipAxis(axisDirectionToAxis(_axisDirection)),
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
}
void _onAxisDirectionChanged(AxisDirection? axisDirection) {

View File

@@ -37,33 +37,17 @@ class _MyWidgetState extends State<MyWidget> {
AxisDirection _axisDirection = AxisDirection.down;
Widget _getArrows(AxisDirection axisDirection) {
final Widget arrow;
switch (axisDirection) {
case AxisDirection.up:
arrow = const Icon(Icons.arrow_upward_rounded);
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
case AxisDirection.down:
arrow = const Icon(Icons.arrow_downward_rounded);
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
case AxisDirection.left:
arrow = const Icon(Icons.arrow_back_rounded);
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
case AxisDirection.right:
arrow = const Icon(Icons.arrow_forward_rounded);
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
}
final Widget arrow = switch (axisDirection) {
AxisDirection.up => const Icon(Icons.arrow_upward_rounded),
AxisDirection.down => const Icon(Icons.arrow_downward_rounded),
AxisDirection.left => const Icon(Icons.arrow_back_rounded),
AxisDirection.right => const Icon(Icons.arrow_forward_rounded),
};
return Flex(
direction: flipAxis(axisDirectionToAxis(axisDirection)),
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
}
void _onAxisDirectionChanged(AxisDirection? axisDirection) {

View File

@@ -37,33 +37,17 @@ class _MyWidgetState extends State<MyWidget> {
AxisDirection _axisDirection = AxisDirection.down;
Widget _getArrows() {
final Widget arrow;
switch (_axisDirection) {
case AxisDirection.up:
arrow = const Icon(Icons.arrow_upward_rounded);
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
case AxisDirection.down:
arrow = const Icon(Icons.arrow_downward_rounded);
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
case AxisDirection.left:
arrow = const Icon(Icons.arrow_back_rounded);
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
case AxisDirection.right:
arrow = const Icon(Icons.arrow_forward_rounded);
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
}
final Widget arrow = switch (_axisDirection) {
AxisDirection.up => const Icon(Icons.arrow_upward_rounded),
AxisDirection.down => const Icon(Icons.arrow_downward_rounded),
AxisDirection.left => const Icon(Icons.arrow_back_rounded),
AxisDirection.right => const Icon(Icons.arrow_forward_rounded),
};
return Flex(
direction: flipAxis(axisDirectionToAxis(_axisDirection)),
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[arrow, arrow],
);
}
void _onAxisDirectionChanged(AxisDirection? axisDirection) {

View File

@@ -39,6 +39,24 @@ class _OverlayExampleState extends State<OverlayExample> {
assert(overlayEntry == null);
Widget builder(BuildContext context) {
final (String label, Color? color) = switch (currentPageIndex) {
0 => ('Explore page', Colors.red),
1 => ('Commute page', Colors.green),
2 => ('Saved page', Colors.orange),
_ => ('No page selected.', null),
};
if (color == null) {
return Text(label);
}
return Column(
children: <Widget>[
Text(label, style: TextStyle(color: color)),
Icon(Icons.arrow_downward, color: color),
],
);
}
overlayEntry = OverlayEntry(
// Create a new OverlayEntry.
builder: (BuildContext context) {
@@ -58,57 +76,7 @@ class _OverlayExampleState extends State<OverlayExample> {
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Tap here for'),
Builder(builder: (BuildContext context) {
switch (currentPageIndex) {
case 0:
return const Column(
children: <Widget>[
Text(
'Explore page',
style: TextStyle(
color: Colors.red,
),
),
Icon(
Icons.arrow_downward,
color: Colors.red,
),
],
);
case 1:
return const Column(
children: <Widget>[
Text(
'Commute page',
style: TextStyle(
color: Colors.green,
),
),
Icon(
Icons.arrow_downward,
color: Colors.green,
),
],
);
case 2:
return const Column(
children: <Widget>[
Text(
'Saved page',
style: TextStyle(
color: Colors.orange,
),
),
Icon(
Icons.arrow_downward,
color: Colors.orange,
),
],
);
default:
return const Text('No page selected.');
}
}),
Builder(builder: builder),
SizedBox(
width: MediaQuery.of(context).size.width / 3,
height: 80.0,