forked from firka/flutter
switch statement cleanup (#148382)
This PR is step 12 in the journey to solve issue #136139 and make the entire Flutter repo more readable. Most of it involves implementing switch expressions, and there's also a few other random things that I wanted to clean up a bit.
This commit is contained in:
@@ -37,19 +37,12 @@ class TestStepResult {
|
||||
});
|
||||
|
||||
factory TestStepResult.fromSnapshot(AsyncSnapshot<TestStepResult> snapshot) {
|
||||
switch (snapshot.connectionState) {
|
||||
case ConnectionState.none:
|
||||
return const TestStepResult('Not started', nothing, TestStatus.ok);
|
||||
case ConnectionState.waiting:
|
||||
return const TestStepResult('Executing', nothing, TestStatus.pending);
|
||||
case ConnectionState.done:
|
||||
if (snapshot.hasData) {
|
||||
return snapshot.data!;
|
||||
}
|
||||
return snapshot.error! as TestStepResult;
|
||||
case ConnectionState.active:
|
||||
throw 'Unsupported state ${snapshot.connectionState}';
|
||||
}
|
||||
return switch (snapshot.connectionState) {
|
||||
ConnectionState.none => const TestStepResult('Not started', nothing, TestStatus.ok),
|
||||
ConnectionState.waiting => const TestStepResult('Executing', nothing, TestStatus.pending),
|
||||
ConnectionState.done => snapshot.data ?? snapshot.error! as TestStepResult,
|
||||
ConnectionState.active => throw 'Unsupported state: ConnectionState.active',
|
||||
};
|
||||
}
|
||||
|
||||
final String name;
|
||||
|
||||
@@ -204,18 +204,12 @@ class _ListDemoState extends State<ListDemo> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final String layoutText = _dense != null ? ' \u2013 Dense' : '';
|
||||
String? itemTypeText;
|
||||
switch (_itemType) {
|
||||
case _MaterialListType.oneLine:
|
||||
case _MaterialListType.oneLineWithAvatar:
|
||||
itemTypeText = 'Single-line';
|
||||
case _MaterialListType.twoLine:
|
||||
itemTypeText = 'Two-line';
|
||||
case _MaterialListType.threeLine:
|
||||
itemTypeText = 'Three-line';
|
||||
case null:
|
||||
break;
|
||||
}
|
||||
final String? itemTypeText = switch (_itemType) {
|
||||
_MaterialListType.oneLine || _MaterialListType.oneLineWithAvatar => 'Single-line',
|
||||
_MaterialListType.twoLine => 'Two-line',
|
||||
_MaterialListType.threeLine => 'Three-line',
|
||||
null => null,
|
||||
};
|
||||
|
||||
Iterable<Widget> listTiles = items.map<Widget>((String item) => buildListTile(context, item));
|
||||
if (_showDividers != null) {
|
||||
|
||||
@@ -80,29 +80,12 @@ class _CustomRangeThumbShape extends RangeSliderThumbShape {
|
||||
);
|
||||
|
||||
final double size = _thumbSize * sizeTween.evaluate(enableAnimation);
|
||||
late Path thumbPath;
|
||||
switch (textDirection) {
|
||||
case TextDirection.rtl:
|
||||
switch (thumb) {
|
||||
case Thumb.start:
|
||||
thumbPath = _rightTriangle(size, center);
|
||||
case Thumb.end:
|
||||
thumbPath = _leftTriangle(size, center);
|
||||
case null:
|
||||
break;
|
||||
}
|
||||
case TextDirection.ltr:
|
||||
switch (thumb) {
|
||||
case Thumb.start:
|
||||
thumbPath = _leftTriangle(size, center);
|
||||
case Thumb.end:
|
||||
thumbPath = _rightTriangle(size, center);
|
||||
case null:
|
||||
break;
|
||||
}
|
||||
case null:
|
||||
break;
|
||||
}
|
||||
final Path 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),
|
||||
};
|
||||
canvas.drawPath(thumbPath, Paint()..color = colorTween.evaluate(enableAnimation)!);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,15 +50,11 @@ enum GalleryDemoCategory {
|
||||
}
|
||||
|
||||
String? displayTitle(GalleryLocalizations localizations) {
|
||||
switch (this) {
|
||||
case GalleryDemoCategory.other:
|
||||
return localizations.homeCategoryReference;
|
||||
case GalleryDemoCategory.material:
|
||||
case GalleryDemoCategory.cupertino:
|
||||
return toString();
|
||||
case GalleryDemoCategory.study:
|
||||
}
|
||||
return null;
|
||||
return switch (this) {
|
||||
study => null,
|
||||
material || cupertino => toString(),
|
||||
other => localizations.homeCategoryReference,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -96,22 +96,15 @@ class GalleryOptions {
|
||||
/// In other words, if the theme is dark, returns light; if the theme is
|
||||
/// light, returns dark.
|
||||
SystemUiOverlayStyle resolvedSystemUiOverlayStyle() {
|
||||
Brightness brightness;
|
||||
switch (themeMode) {
|
||||
case ThemeMode.light:
|
||||
brightness = Brightness.light;
|
||||
case ThemeMode.dark:
|
||||
brightness = Brightness.dark;
|
||||
case ThemeMode.system:
|
||||
brightness =
|
||||
WidgetsBinding.instance.platformDispatcher.platformBrightness;
|
||||
}
|
||||
|
||||
final SystemUiOverlayStyle overlayStyle = brightness == Brightness.dark
|
||||
? SystemUiOverlayStyle.light
|
||||
: SystemUiOverlayStyle.dark;
|
||||
|
||||
return overlayStyle;
|
||||
final Brightness brightness = switch (themeMode) {
|
||||
ThemeMode.light => Brightness.light,
|
||||
ThemeMode.dark => Brightness.dark,
|
||||
ThemeMode.system => WidgetsBinding.instance.platformDispatcher.platformBrightness,
|
||||
};
|
||||
return switch (brightness) {
|
||||
Brightness.light => SystemUiOverlayStyle.dark,
|
||||
Brightness.dark => SystemUiOverlayStyle.light,
|
||||
};
|
||||
}
|
||||
|
||||
GalleryOptions copyWith({
|
||||
|
||||
@@ -105,18 +105,13 @@ class _CupertinoAlertDemoState extends State<CupertinoAlertDemo>
|
||||
|
||||
String _title(BuildContext context) {
|
||||
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
|
||||
switch (widget.type) {
|
||||
case AlertDemoType.alert:
|
||||
return localizations.demoCupertinoAlertTitle;
|
||||
case AlertDemoType.alertTitle:
|
||||
return localizations.demoCupertinoAlertWithTitleTitle;
|
||||
case AlertDemoType.alertButtons:
|
||||
return localizations.demoCupertinoAlertButtonsTitle;
|
||||
case AlertDemoType.alertButtonsOnly:
|
||||
return localizations.demoCupertinoAlertButtonsOnlyTitle;
|
||||
case AlertDemoType.actionSheet:
|
||||
return localizations.demoCupertinoActionSheetTitle;
|
||||
}
|
||||
return switch (widget.type) {
|
||||
AlertDemoType.alert => localizations.demoCupertinoAlertTitle,
|
||||
AlertDemoType.alertTitle => localizations.demoCupertinoAlertWithTitleTitle,
|
||||
AlertDemoType.alertButtons => localizations.demoCupertinoAlertButtonsTitle,
|
||||
AlertDemoType.alertButtonsOnly => localizations.demoCupertinoAlertButtonsOnlyTitle,
|
||||
AlertDemoType.actionSheet => localizations.demoCupertinoActionSheetTitle,
|
||||
};
|
||||
}
|
||||
|
||||
static Route<String> _alertDemoDialog(
|
||||
|
||||
@@ -17,21 +17,17 @@ class BottomSheetDemo extends StatelessWidget {
|
||||
|
||||
String _title(BuildContext context) {
|
||||
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
|
||||
switch (type) {
|
||||
case BottomSheetDemoType.persistent:
|
||||
return localizations.demoBottomSheetPersistentTitle;
|
||||
case BottomSheetDemoType.modal:
|
||||
return localizations.demoBottomSheetModalTitle;
|
||||
}
|
||||
return switch (type) {
|
||||
BottomSheetDemoType.persistent => localizations.demoBottomSheetPersistentTitle,
|
||||
BottomSheetDemoType.modal => localizations.demoBottomSheetModalTitle,
|
||||
};
|
||||
}
|
||||
|
||||
Widget _bottomSheetDemo(BuildContext context) {
|
||||
switch (type) {
|
||||
case BottomSheetDemoType.persistent:
|
||||
return _PersistentBottomSheetDemo();
|
||||
case BottomSheetDemoType.modal:
|
||||
return _ModalBottomSheetDemo();
|
||||
}
|
||||
return switch (type) {
|
||||
BottomSheetDemoType.persistent => _PersistentBottomSheetDemo(),
|
||||
BottomSheetDemoType.modal => _ModalBottomSheetDemo(),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -13,18 +13,13 @@ class ButtonDemo extends StatelessWidget {
|
||||
|
||||
String _title(BuildContext context) {
|
||||
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
|
||||
switch (type) {
|
||||
case ButtonDemoType.text:
|
||||
return localizations.demoTextButtonTitle;
|
||||
case ButtonDemoType.elevated:
|
||||
return localizations.demoElevatedButtonTitle;
|
||||
case ButtonDemoType.outlined:
|
||||
return localizations.demoOutlinedButtonTitle;
|
||||
case ButtonDemoType.toggle:
|
||||
return localizations.demoToggleButtonTitle;
|
||||
case ButtonDemoType.floating:
|
||||
return localizations.demoFloatingButtonTitle;
|
||||
}
|
||||
return switch (type) {
|
||||
ButtonDemoType.text => localizations.demoTextButtonTitle,
|
||||
ButtonDemoType.elevated => localizations.demoElevatedButtonTitle,
|
||||
ButtonDemoType.outlined => localizations.demoOutlinedButtonTitle,
|
||||
ButtonDemoType.toggle => localizations.demoToggleButtonTitle,
|
||||
ButtonDemoType.floating => localizations.demoFloatingButtonTitle,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -16,16 +16,12 @@ class ChipDemo extends StatelessWidget {
|
||||
|
||||
String _title(BuildContext context) {
|
||||
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
|
||||
switch (type) {
|
||||
case ChipDemoType.action:
|
||||
return localizations.demoActionChipTitle;
|
||||
case ChipDemoType.choice:
|
||||
return localizations.demoChoiceChipTitle;
|
||||
case ChipDemoType.filter:
|
||||
return localizations.demoFilterChipTitle;
|
||||
case ChipDemoType.input:
|
||||
return localizations.demoInputChipTitle;
|
||||
}
|
||||
return switch (type) {
|
||||
ChipDemoType.action => localizations.demoActionChipTitle,
|
||||
ChipDemoType.choice => localizations.demoChoiceChipTitle,
|
||||
ChipDemoType.filter => localizations.demoFilterChipTitle,
|
||||
ChipDemoType.input => localizations.demoInputChipTitle,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -81,16 +81,12 @@ class _DialogDemoState extends State<DialogDemo> with RestorationMixin {
|
||||
|
||||
String _title(BuildContext context) {
|
||||
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
|
||||
switch (widget.type) {
|
||||
case DialogDemoType.alert:
|
||||
return localizations.demoAlertDialogTitle;
|
||||
case DialogDemoType.alertTitle:
|
||||
return localizations.demoAlertTitleDialogTitle;
|
||||
case DialogDemoType.simple:
|
||||
return localizations.demoSimpleDialogTitle;
|
||||
case DialogDemoType.fullscreen:
|
||||
return localizations.demoFullscreenDialogTitle;
|
||||
}
|
||||
return switch (widget.type) {
|
||||
DialogDemoType.alert => localizations.demoAlertDialogTitle,
|
||||
DialogDemoType.alertTitle => localizations.demoAlertTitleDialogTitle,
|
||||
DialogDemoType.simple => localizations.demoSimpleDialogTitle,
|
||||
DialogDemoType.fullscreen => localizations.demoFullscreenDialogTitle,
|
||||
};
|
||||
}
|
||||
|
||||
static Route<String> _alertDialogDemoRoute(
|
||||
|
||||
@@ -164,14 +164,11 @@ class _PickerDemoState extends State<PickerDemo> with RestorationMixin {
|
||||
|
||||
String get _title {
|
||||
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
|
||||
switch (widget.type) {
|
||||
case PickerDemoType.date:
|
||||
return localizations.demoDatePickerTitle;
|
||||
case PickerDemoType.time:
|
||||
return localizations.demoTimePickerTitle;
|
||||
case PickerDemoType.range:
|
||||
return localizations.demoDateRangePickerTitle;
|
||||
}
|
||||
return switch (widget.type) {
|
||||
PickerDemoType.date => localizations.demoDatePickerTitle,
|
||||
PickerDemoType.time => localizations.demoTimePickerTitle,
|
||||
PickerDemoType.range => localizations.demoDateRangePickerTitle,
|
||||
};
|
||||
}
|
||||
|
||||
String get _labelText {
|
||||
|
||||
@@ -13,14 +13,10 @@ class TabsDemo extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget tabs;
|
||||
switch (type) {
|
||||
case TabsDemoType.scrollable:
|
||||
tabs = _TabsScrollableDemo();
|
||||
case TabsDemoType.nonScrollable:
|
||||
tabs = _TabsNonScrollableDemo();
|
||||
}
|
||||
return tabs;
|
||||
return switch (type) {
|
||||
TabsDemoType.scrollable => _TabsScrollableDemo(),
|
||||
TabsDemoType.nonScrollable => _TabsNonScrollableDemo(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -433,16 +433,11 @@ class _GalleryDemoPageState extends State<GalleryDemoPage>
|
||||
page = AnimatedBuilder(
|
||||
animation: _codeBackgroundColorController,
|
||||
builder: (BuildContext context, Widget? child) {
|
||||
Brightness themeBrightness;
|
||||
|
||||
switch (GalleryOptions.of(context).themeMode) {
|
||||
case ThemeMode.system:
|
||||
themeBrightness = MediaQuery.of(context).platformBrightness;
|
||||
case ThemeMode.light:
|
||||
themeBrightness = Brightness.light;
|
||||
case ThemeMode.dark:
|
||||
themeBrightness = Brightness.dark;
|
||||
}
|
||||
final Brightness themeBrightness = switch (GalleryOptions.of(context).themeMode) {
|
||||
ThemeMode.system => MediaQuery.of(context).platformBrightness,
|
||||
ThemeMode.light => Brightness.light,
|
||||
ThemeMode.dark => Brightness.dark,
|
||||
};
|
||||
|
||||
Widget contents = Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: horizontalPadding),
|
||||
|
||||
@@ -89,17 +89,13 @@ class _ReplyAppState extends State<ReplyApp> with RestorationMixin {
|
||||
supportedLocales: GalleryLocalizations.supportedLocales,
|
||||
locale: GalleryOptions.of(context).locale,
|
||||
initialRoute: ReplyApp.homeRoute,
|
||||
onGenerateRoute: (RouteSettings settings) {
|
||||
switch (settings.name) {
|
||||
case ReplyApp.homeRoute:
|
||||
return MaterialPageRoute<void>(
|
||||
builder: (BuildContext context) => const AdaptiveNav(),
|
||||
settings: settings,
|
||||
);
|
||||
case ReplyApp.composeRoute:
|
||||
return ReplyApp.createComposeRoute(settings);
|
||||
}
|
||||
return null;
|
||||
onGenerateRoute: (RouteSettings settings) => switch (settings.name) {
|
||||
ReplyApp.homeRoute => MaterialPageRoute<void>(
|
||||
builder: (BuildContext context) => const AdaptiveNav(),
|
||||
settings: settings,
|
||||
),
|
||||
ReplyApp.composeRoute => ReplyApp.createComposeRoute(settings),
|
||||
_ => null,
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@@ -34,40 +34,15 @@ class MailboxBody extends StatelessWidget {
|
||||
final String destinationString = destination
|
||||
.toString()
|
||||
.substring(destination.toString().indexOf('.') + 1);
|
||||
late List<Email> emails;
|
||||
|
||||
switch (destination) {
|
||||
case MailboxPageType.inbox:
|
||||
{
|
||||
emails = model.inboxEmails;
|
||||
break;
|
||||
}
|
||||
case MailboxPageType.sent:
|
||||
{
|
||||
emails = model.outboxEmails;
|
||||
break;
|
||||
}
|
||||
case MailboxPageType.starred:
|
||||
{
|
||||
emails = model.starredEmails;
|
||||
break;
|
||||
}
|
||||
case MailboxPageType.trash:
|
||||
{
|
||||
emails = model.trashEmails;
|
||||
break;
|
||||
}
|
||||
case MailboxPageType.spam:
|
||||
{
|
||||
emails = model.spamEmails;
|
||||
break;
|
||||
}
|
||||
case MailboxPageType.drafts:
|
||||
{
|
||||
emails = model.draftEmails;
|
||||
break;
|
||||
}
|
||||
}
|
||||
final List<Email> emails = switch (destination) {
|
||||
MailboxPageType.inbox => model.inboxEmails,
|
||||
MailboxPageType.sent => model.outboxEmails,
|
||||
MailboxPageType.starred => model.starredEmails,
|
||||
MailboxPageType.trash => model.trashEmails,
|
||||
MailboxPageType.spam => model.spamEmails,
|
||||
MailboxPageType.drafts => model.draftEmails,
|
||||
};
|
||||
|
||||
return SafeArea(
|
||||
bottom: false,
|
||||
|
||||
@@ -16,21 +16,12 @@ class TestStepResult {
|
||||
const TestStepResult(this.name, this.description, this.status);
|
||||
|
||||
factory TestStepResult.fromSnapshot(AsyncSnapshot<TestStepResult> snapshot) {
|
||||
switch (snapshot.connectionState) {
|
||||
case ConnectionState.none:
|
||||
return const TestStepResult('Not started', nothing, TestStatus.ok);
|
||||
case ConnectionState.waiting:
|
||||
return const TestStepResult('Executing', nothing, TestStatus.pending);
|
||||
case ConnectionState.done:
|
||||
if (snapshot.hasData) {
|
||||
return snapshot.data!;
|
||||
} else {
|
||||
final Object? result = snapshot.error;
|
||||
return result! as TestStepResult;
|
||||
}
|
||||
case ConnectionState.active:
|
||||
throw 'Unsupported state ${snapshot.connectionState}';
|
||||
}
|
||||
return switch (snapshot.connectionState) {
|
||||
ConnectionState.none => const TestStepResult('Not started', nothing, TestStatus.ok),
|
||||
ConnectionState.waiting => const TestStepResult('Executing', nothing, TestStatus.pending),
|
||||
ConnectionState.done => snapshot.data ?? snapshot.error! as TestStepResult,
|
||||
ConnectionState.active => throw 'Unsupported state: ConnectionState.active',
|
||||
};
|
||||
}
|
||||
|
||||
final String name;
|
||||
|
||||
Reference in New Issue
Block a user