mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Implement switch expressions in dev/ (#139048)
I previously made a PR (#136140) that used `switch` expressions to make some parts of the Flutter codebase easier to understand. It was assigned to the framework team, and @christopherfujino let me know that it was too large to effectively review and recommended breaking it up into smaller pull requests. Here's a PR that only targets files in the `dev/` directory. Hopefully this will be easier to work with! (solves issue https://github.com/flutter/flutter/issues/136139)
This commit is contained in:
parent
9c4df1f1f5
commit
3267fbc07a
@ -68,40 +68,28 @@ class ResampleFlagVariant extends TestVariant<TestScenario> {
|
||||
|
||||
late TestScenario currentValue;
|
||||
bool get resample {
|
||||
switch (currentValue) {
|
||||
case TestScenario.resampleOn90Hz:
|
||||
case TestScenario.resampleOn59Hz:
|
||||
return true;
|
||||
case TestScenario.resampleOff90Hz:
|
||||
case TestScenario.resampleOff59Hz:
|
||||
return false;
|
||||
}
|
||||
return switch (currentValue) {
|
||||
TestScenario.resampleOn90Hz || TestScenario.resampleOn59Hz => true,
|
||||
TestScenario.resampleOff90Hz || TestScenario.resampleOff59Hz => false,
|
||||
};
|
||||
}
|
||||
double get frequency {
|
||||
switch (currentValue) {
|
||||
case TestScenario.resampleOn90Hz:
|
||||
case TestScenario.resampleOff90Hz:
|
||||
return 90.0;
|
||||
case TestScenario.resampleOn59Hz:
|
||||
case TestScenario.resampleOff59Hz:
|
||||
return 59.0;
|
||||
}
|
||||
return switch (currentValue) {
|
||||
TestScenario.resampleOn90Hz || TestScenario.resampleOff90Hz => 90.0,
|
||||
TestScenario.resampleOn59Hz || TestScenario.resampleOff59Hz => 59.0,
|
||||
};
|
||||
}
|
||||
|
||||
Map<String, dynamic>? result;
|
||||
|
||||
@override
|
||||
String describeValue(TestScenario value) {
|
||||
switch (value) {
|
||||
case TestScenario.resampleOn90Hz:
|
||||
return 'resample on with 90Hz input';
|
||||
case TestScenario.resampleOn59Hz:
|
||||
return 'resample on with 59Hz input';
|
||||
case TestScenario.resampleOff90Hz:
|
||||
return 'resample off with 90Hz input';
|
||||
case TestScenario.resampleOff59Hz:
|
||||
return 'resample off with 59Hz input';
|
||||
}
|
||||
return switch (value) {
|
||||
TestScenario.resampleOn90Hz => 'resample on with 90Hz input',
|
||||
TestScenario.resampleOn59Hz => 'resample on with 59Hz input',
|
||||
TestScenario.resampleOff90Hz => 'resample off with 90Hz input',
|
||||
TestScenario.resampleOff59Hz => 'resample off with 59Hz input',
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@ -60,12 +60,12 @@ class _FilteredChildAnimationPageState extends State<FilteredChildAnimationPage>
|
||||
}
|
||||
|
||||
String get _title {
|
||||
switch (_filterType) {
|
||||
case FilterType.opacity: return 'Fading Child Animation';
|
||||
case FilterType.rotateTransform: return 'Transformed Child Animation';
|
||||
case FilterType.rotateFilter: return 'Matrix Filtered Child Animation';
|
||||
case null: return 'Static Child';
|
||||
}
|
||||
return switch (_filterType) {
|
||||
FilterType.opacity => 'Fading Child Animation',
|
||||
FilterType.rotateTransform => 'Transformed Child Animation',
|
||||
FilterType.rotateFilter => 'Matrix Filtered Child Animation',
|
||||
null => 'Static Child',
|
||||
};
|
||||
}
|
||||
|
||||
static Widget _makeChild(int rows, int cols, double fontSize, bool complex) {
|
||||
|
||||
@ -109,28 +109,17 @@ Future<void> _setAppVersion(int version) async {
|
||||
}
|
||||
|
||||
String _testTypeToIndexFile(ServiceWorkerTestType type) {
|
||||
late String indexFile;
|
||||
switch (type) {
|
||||
case ServiceWorkerTestType.blockedServiceWorkers:
|
||||
indexFile = 'index_with_blocked_service_workers.html';
|
||||
case ServiceWorkerTestType.withFlutterJs:
|
||||
indexFile = 'index_with_flutterjs.html';
|
||||
case ServiceWorkerTestType.withoutFlutterJs:
|
||||
indexFile = 'index_without_flutterjs.html';
|
||||
case ServiceWorkerTestType.withFlutterJsShort:
|
||||
indexFile = 'index_with_flutterjs_short.html';
|
||||
case ServiceWorkerTestType.withFlutterJsEntrypointLoadedEvent:
|
||||
indexFile = 'index_with_flutterjs_entrypoint_loaded.html';
|
||||
case ServiceWorkerTestType.withFlutterJsTrustedTypesOn:
|
||||
indexFile = 'index_with_flutterjs_el_tt_on.html';
|
||||
case ServiceWorkerTestType.withFlutterJsNonceOn:
|
||||
indexFile = 'index_with_flutterjs_el_nonce.html';
|
||||
case ServiceWorkerTestType.withFlutterJsCustomServiceWorkerVersion:
|
||||
indexFile = 'index_with_flutterjs_custom_sw_version.html';
|
||||
case ServiceWorkerTestType.generatedEntrypoint:
|
||||
indexFile = 'generated_entrypoint.html';
|
||||
}
|
||||
return indexFile;
|
||||
return switch (type) {
|
||||
ServiceWorkerTestType.blockedServiceWorkers => 'index_with_blocked_service_workers.html',
|
||||
ServiceWorkerTestType.withFlutterJs => 'index_with_flutterjs.html',
|
||||
ServiceWorkerTestType.withoutFlutterJs => 'index_without_flutterjs.html',
|
||||
ServiceWorkerTestType.withFlutterJsShort => 'index_with_flutterjs_short.html',
|
||||
ServiceWorkerTestType.withFlutterJsEntrypointLoadedEvent => 'index_with_flutterjs_entrypoint_loaded.html',
|
||||
ServiceWorkerTestType.withFlutterJsTrustedTypesOn => 'index_with_flutterjs_el_tt_on.html',
|
||||
ServiceWorkerTestType.withFlutterJsNonceOn => 'index_with_flutterjs_el_nonce.html',
|
||||
ServiceWorkerTestType.withFlutterJsCustomServiceWorkerVersion => 'index_with_flutterjs_custom_sw_version.html',
|
||||
ServiceWorkerTestType.generatedEntrypoint => 'generated_entrypoint.html',
|
||||
};
|
||||
}
|
||||
|
||||
Future<void> _rebuildApp({ required int version, required ServiceWorkerTestType testType, required String target }) async {
|
||||
|
||||
@ -49,53 +49,39 @@ class UnpublishException implements Exception {
|
||||
enum Channel { dev, beta, stable }
|
||||
|
||||
String getChannelName(Channel channel) {
|
||||
switch (channel) {
|
||||
case Channel.beta:
|
||||
return 'beta';
|
||||
case Channel.dev:
|
||||
return 'dev';
|
||||
case Channel.stable:
|
||||
return 'stable';
|
||||
}
|
||||
return switch (channel) {
|
||||
Channel.beta => 'beta',
|
||||
Channel.dev => 'dev',
|
||||
Channel.stable => 'stable',
|
||||
};
|
||||
}
|
||||
|
||||
Channel fromChannelName(String? name) {
|
||||
switch (name) {
|
||||
case 'beta':
|
||||
return Channel.beta;
|
||||
case 'dev':
|
||||
return Channel.dev;
|
||||
case 'stable':
|
||||
return Channel.stable;
|
||||
default:
|
||||
throw ArgumentError('Invalid channel name.');
|
||||
}
|
||||
return switch (name) {
|
||||
'beta' => Channel.beta,
|
||||
'dev' => Channel.dev,
|
||||
'stable' => Channel.stable,
|
||||
_ => throw ArgumentError('Invalid channel name.'),
|
||||
};
|
||||
}
|
||||
|
||||
enum PublishedPlatform { linux, macos, windows }
|
||||
|
||||
String getPublishedPlatform(PublishedPlatform platform) {
|
||||
switch (platform) {
|
||||
case PublishedPlatform.linux:
|
||||
return 'linux';
|
||||
case PublishedPlatform.macos:
|
||||
return 'macos';
|
||||
case PublishedPlatform.windows:
|
||||
return 'windows';
|
||||
}
|
||||
return switch (platform) {
|
||||
PublishedPlatform.linux => 'linux',
|
||||
PublishedPlatform.macos => 'macos',
|
||||
PublishedPlatform.windows => 'windows',
|
||||
};
|
||||
}
|
||||
|
||||
PublishedPlatform fromPublishedPlatform(String name) {
|
||||
switch (name) {
|
||||
case 'linux':
|
||||
return PublishedPlatform.linux;
|
||||
case 'macos':
|
||||
return PublishedPlatform.macos;
|
||||
case 'windows':
|
||||
return PublishedPlatform.windows;
|
||||
default:
|
||||
throw ArgumentError('Invalid published platform name.');
|
||||
}
|
||||
return switch (name) {
|
||||
'linux' => PublishedPlatform.linux,
|
||||
'macos' => PublishedPlatform.macos,
|
||||
'windows' => PublishedPlatform.windows,
|
||||
_ => throw ArgumentError('Invalid published platform name.'),
|
||||
};
|
||||
}
|
||||
|
||||
/// A helper class for classes that want to run a process, optionally have the
|
||||
|
||||
@ -47,12 +47,10 @@ class Remote {
|
||||
|
||||
/// The name of the remote.
|
||||
String get name {
|
||||
switch (_name) {
|
||||
case RemoteName.upstream:
|
||||
return 'upstream';
|
||||
case RemoteName.mirror:
|
||||
return 'mirror';
|
||||
}
|
||||
return switch (_name) {
|
||||
RemoteName.upstream => 'upstream',
|
||||
RemoteName.mirror => 'mirror',
|
||||
};
|
||||
}
|
||||
|
||||
/// The URL of the remote.
|
||||
|
||||
@ -295,15 +295,11 @@ class Version {
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
switch (type) {
|
||||
case VersionType.stable:
|
||||
return '$x.$y.$z';
|
||||
case VersionType.development:
|
||||
return '$x.$y.$z-$m.$n.pre';
|
||||
case VersionType.latest:
|
||||
return '$x.$y.$z-$m.$n.pre.$commits';
|
||||
case VersionType.gitDescribe:
|
||||
return '$x.$y.$z-$m.$n.pre.$commits';
|
||||
}
|
||||
return switch (type) {
|
||||
VersionType.stable => '$x.$y.$z',
|
||||
VersionType.development => '$x.$y.$z-$m.$n.pre',
|
||||
VersionType.latest => '$x.$y.$z-$m.$n.pre.$commits',
|
||||
VersionType.gitDescribe => '$x.$y.$z-$m.$n.pre.$commits',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -2089,14 +2089,11 @@ enum ReportedDurationTestFlavor {
|
||||
}
|
||||
|
||||
String _reportedDurationTestToString(ReportedDurationTestFlavor flavor) {
|
||||
switch (flavor) {
|
||||
case ReportedDurationTestFlavor.debug:
|
||||
return 'debug';
|
||||
case ReportedDurationTestFlavor.profile:
|
||||
return 'profile';
|
||||
case ReportedDurationTestFlavor.release:
|
||||
return 'release';
|
||||
}
|
||||
return switch (flavor) {
|
||||
ReportedDurationTestFlavor.debug => 'debug',
|
||||
ReportedDurationTestFlavor.profile => 'profile',
|
||||
ReportedDurationTestFlavor.release => 'release',
|
||||
};
|
||||
}
|
||||
|
||||
class ReportedDurationTest {
|
||||
|
||||
@ -32,13 +32,11 @@ class ExtendedStandardMessageCodec extends StandardMessageCodec {
|
||||
|
||||
@override
|
||||
dynamic readValueOfType(int type, ReadBuffer buffer) {
|
||||
switch (type) {
|
||||
case _dateTime:
|
||||
return DateTime.fromMillisecondsSinceEpoch(buffer.getInt64());
|
||||
case _pair:
|
||||
return Pair(readValue(buffer), readValue(buffer));
|
||||
default: return super.readValueOfType(type, buffer);
|
||||
}
|
||||
return switch (type) {
|
||||
_dateTime => DateTime.fromMillisecondsSinceEpoch(buffer.getInt64()),
|
||||
_pair => Pair(readValue(buffer), readValue(buffer)),
|
||||
_ => super.readValueOfType(type, buffer),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -75,16 +75,12 @@ class OperationToken extends ExpressionToken {
|
||||
Operation operation;
|
||||
|
||||
static String? opString(Operation operation) {
|
||||
switch (operation) {
|
||||
case Operation.Addition:
|
||||
return ' + ';
|
||||
case Operation.Subtraction:
|
||||
return ' - ';
|
||||
case Operation.Multiplication:
|
||||
return ' \u00D7 ';
|
||||
case Operation.Division:
|
||||
return ' \u00F7 ';
|
||||
}
|
||||
return switch (operation) {
|
||||
Operation.Addition => ' + ',
|
||||
Operation.Subtraction => ' - ',
|
||||
Operation.Multiplication => ' \u00D7 ',
|
||||
Operation.Division => ' \u00F7 ',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -163,14 +163,11 @@ class ScrollableTabsDemoState extends State<ScrollableTabsDemo> with SingleTicke
|
||||
isScrollable: true,
|
||||
indicator: getIndicator(),
|
||||
tabs: _allPages.map<Tab>((_Page page) {
|
||||
switch (_demoStyle) {
|
||||
case TabsDemoStyle.iconsAndText:
|
||||
return Tab(text: page.text, icon: Icon(page.icon));
|
||||
case TabsDemoStyle.iconsOnly:
|
||||
return Tab(icon: Icon(page.icon));
|
||||
case TabsDemoStyle.textOnly:
|
||||
return Tab(text: page.text);
|
||||
}
|
||||
return switch (_demoStyle) {
|
||||
TabsDemoStyle.iconsAndText => Tab(text: page.text, icon: Icon(page.icon)),
|
||||
TabsDemoStyle.iconsOnly => Tab(icon: Icon(page.icon)),
|
||||
TabsDemoStyle.textOnly => Tab(text: page.text),
|
||||
};
|
||||
}).toList()
|
||||
),
|
||||
),
|
||||
|
||||
@ -221,18 +221,13 @@ class ExpandingBottomSheetState extends State<ExpandingBottomSheet> with TickerP
|
||||
// Returns the correct width of the ExpandingBottomSheet based on the number of
|
||||
// products in the cart.
|
||||
double _widthFor(int numProducts) {
|
||||
switch (numProducts) {
|
||||
case 0:
|
||||
return _kWidthForCartIcon;
|
||||
case 1:
|
||||
return 136.0;
|
||||
case 2:
|
||||
return 192.0;
|
||||
case 3:
|
||||
return 248.0;
|
||||
default:
|
||||
return 278.0;
|
||||
}
|
||||
return switch (numProducts) {
|
||||
0 => _kWidthForCartIcon,
|
||||
1 => 136.0,
|
||||
2 => 192.0,
|
||||
3 => 248.0,
|
||||
_ => 278.0,
|
||||
};
|
||||
}
|
||||
|
||||
// Returns true if the cart is open or opening and false otherwise.
|
||||
|
||||
@ -411,20 +411,14 @@ class _PlatformItem extends StatelessWidget {
|
||||
final ValueChanged<GalleryOptions>? onOptionsChanged;
|
||||
|
||||
String _platformLabel(TargetPlatform platform) {
|
||||
switch (platform) {
|
||||
case TargetPlatform.android:
|
||||
return 'Mountain View';
|
||||
case TargetPlatform.fuchsia:
|
||||
return 'Fuchsia';
|
||||
case TargetPlatform.iOS:
|
||||
return 'Cupertino';
|
||||
case TargetPlatform.linux:
|
||||
return 'Material Desktop (linux)';
|
||||
case TargetPlatform.macOS:
|
||||
return 'Material Desktop (macOS)';
|
||||
case TargetPlatform.windows:
|
||||
return 'Material Desktop (Windows)';
|
||||
}
|
||||
return switch (platform) {
|
||||
TargetPlatform.android => 'Mountain View',
|
||||
TargetPlatform.fuchsia => 'Fuchsia',
|
||||
TargetPlatform.iOS => 'Cupertino',
|
||||
TargetPlatform.linux => 'Material Desktop (linux)',
|
||||
TargetPlatform.macOS => 'Material Desktop (macOS)',
|
||||
TargetPlatform.windows => 'Material Desktop (Windows)',
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@ -174,17 +174,12 @@ class _OptionsState extends State<Options> {
|
||||
}
|
||||
|
||||
VisualDensity _profileToDensity(String? profile) {
|
||||
switch (profile) {
|
||||
case 'standard':
|
||||
return VisualDensity.standard;
|
||||
case 'comfortable':
|
||||
return VisualDensity.comfortable;
|
||||
case 'compact':
|
||||
return VisualDensity.compact;
|
||||
case 'custom':
|
||||
default:
|
||||
return widget.model.density;
|
||||
}
|
||||
return switch (profile) {
|
||||
'standard' => VisualDensity.standard,
|
||||
'comfortable' => VisualDensity.comfortable,
|
||||
'compact' => VisualDensity.compact,
|
||||
'custom' || _ => widget.model.density,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@ -478,14 +478,11 @@ class _TestMenusState extends State<_TestMenus> {
|
||||
void _setCheck(TestMenu item) {
|
||||
debugPrint('App: Set Checkbox item ${item.label}');
|
||||
setState(() {
|
||||
switch (checkboxState) {
|
||||
case false:
|
||||
checkboxState = true;
|
||||
case true:
|
||||
checkboxState = null;
|
||||
case null:
|
||||
checkboxState = false;
|
||||
}
|
||||
checkboxState = switch (checkboxState) {
|
||||
false => true,
|
||||
true => null,
|
||||
null => false,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -152,19 +152,13 @@ class PathCommandAnimation {
|
||||
}
|
||||
|
||||
String toDart() {
|
||||
String dartCommandClass;
|
||||
switch (type) {
|
||||
case 'M':
|
||||
dartCommandClass = '_PathMoveTo';
|
||||
case 'C':
|
||||
dartCommandClass = '_PathCubicTo';
|
||||
case 'L':
|
||||
dartCommandClass = '_PathLineTo';
|
||||
case 'Z':
|
||||
dartCommandClass = '_PathClose';
|
||||
default:
|
||||
throw Exception('unsupported path command: $type');
|
||||
}
|
||||
final String dartCommandClass = switch (type) {
|
||||
'M' => '_PathMoveTo',
|
||||
'C' => '_PathCubicTo',
|
||||
'L' => '_PathLineTo',
|
||||
'Z' => '_PathClose',
|
||||
_ => throw Exception('unsupported path command: $type'),
|
||||
};
|
||||
final StringBuffer sb = StringBuffer();
|
||||
sb.write('${kIndent * 4}const $dartCommandClass(\n');
|
||||
for (final List<Point<double>> pointFrames in points) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user