diff --git a/examples/api/lib/widgets/focus_manager/focus_node.unfocus.0.dart b/examples/api/lib/widgets/focus_manager/focus_node.unfocus.0.dart index 6005b48cb3..64930d280e 100644 --- a/examples/api/lib/widgets/focus_manager/focus_node.unfocus.0.dart +++ b/examples/api/lib/widgets/focus_manager/focus_node.unfocus.0.dart @@ -74,7 +74,7 @@ class _MyStatefulWidgetState extends State { }, value: UnfocusDisposition.values[index], ), - Text(describeEnum(UnfocusDisposition.values[index])), + Text(UnfocusDisposition.values[index].name), ], ); }), diff --git a/packages/flutter/lib/src/foundation/diagnostics.dart b/packages/flutter/lib/src/foundation/diagnostics.dart index c6b1e0c6a1..e9c0468579 100644 --- a/packages/flutter/lib/src/foundation/diagnostics.dart +++ b/packages/flutter/lib/src/foundation/diagnostics.dart @@ -1568,13 +1568,13 @@ abstract class DiagnosticsNode { if (!showSeparator) 'showSeparator': showSeparator, if (level != DiagnosticLevel.info) - 'level': describeEnum(level), + 'level': level.name, if (showName == false) 'showName': showName, if (emptyBodyDescription != null) 'emptyBodyDescription': emptyBodyDescription, if (style != DiagnosticsTreeStyle.sparse) - 'style': describeEnum(style!), + 'style': style!.name, if (allowTruncate) 'allowTruncate': allowTruncate, if (hasChildren) @@ -2303,6 +2303,12 @@ class IterableProperty extends DiagnosticsProperty> { /// The enum value is displayed with the class name stripped. For example: /// [HitTestBehavior.deferToChild] is shown as `deferToChild`. /// +/// This class can be used with classes that appear like enums but are not +/// "real" enums, so long as their `toString` implementation, in debug mode, +/// returns a string consisting of the class name followed by the value name. It +/// can also be used with nullable properties; the null value is represented as +/// `null`. +/// /// See also: /// /// * [DiagnosticsProperty] which documents named parameters common to all @@ -2702,7 +2708,7 @@ class DiagnosticsProperty extends DiagnosticsNode { if (exception != null) json['exception'] = exception.toString(); json['propertyType'] = propertyType.toString(); - json['defaultLevel'] = describeEnum(_defaultLevel); + json['defaultLevel'] = _defaultLevel.name; if (value is Diagnosticable || value is DiagnosticsNode) json['isDiagnosticableValue'] = true; if (v is num) @@ -3016,11 +3022,19 @@ String shortHash(Object? object) { /// * [Object.runtimeType], the [Type] of an object. String describeIdentity(Object? object) => '${objectRuntimeType(object, '')}#${shortHash(object)}'; -// This method exists as a workaround for https://github.com/dart-lang/sdk/issues/30021 /// Returns a short description of an enum value. /// /// Strips off the enum class name from the `enumEntry.toString()`. /// +/// For real enums, this is redundant with calling the `name` getter on the enum +/// value (see [EnumName.name]), a feature that was added to Dart 2.15. +/// +/// This function can also be used with classes whose `toString` return a value +/// in the same form as an enum (the class name, a dot, then the value name). +/// For example, it's used with [SemanticsAction], which is written to appear to +/// be an enum but is actually a bespoke class so that the index values can be +/// set as powers of two instead of as sequential integers. +/// /// {@tool snippet} /// /// ```dart @@ -3031,10 +3045,13 @@ String describeIdentity(Object? object) => '${objectRuntimeType(object, ' decorationDescription = []; if (decorationStyle != null) - decorationDescription.add(describeEnum(decorationStyle!)); + decorationDescription.add(decorationStyle!.name); // Hide decorationColor from the default text view as it is shown in the // terse decoration summary as well. diff --git a/packages/flutter/lib/src/widgets/media_query.dart b/packages/flutter/lib/src/widgets/media_query.dart index 9da58dd823..c568069e2f 100644 --- a/packages/flutter/lib/src/widgets/media_query.dart +++ b/packages/flutter/lib/src/widgets/media_query.dart @@ -605,7 +605,7 @@ class MediaQueryData { 'disableAnimations: $disableAnimations', 'invertColors: $invertColors', 'boldText: $boldText', - 'navigationMode: ${describeEnum(navigationMode)}', + 'navigationMode: ${navigationMode.name}', 'gestureSettings: $gestureSettings', ]; return '${objectRuntimeType(this, 'MediaQueryData')}(${properties.join(', ')})'; diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index a74039fe0d..c2facf568e 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -717,7 +717,7 @@ void main() { await expectLater( find.byKey(const ValueKey(1)), matchesGoldenFile( - 'text_field_cursor_test.cupertino_${describeEnum(debugDefaultTargetPlatformOverride!).toLowerCase()}.1.png', + 'text_field_cursor_test.cupertino_${debugDefaultTargetPlatformOverride!.name.toLowerCase()}.1.png', ), ); }, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.macOS })); diff --git a/packages/flutter/test/foundation/diagnostics_test.dart b/packages/flutter/test/foundation/diagnostics_test.dart index ebc3672e8b..29eedb0541 100644 --- a/packages/flutter/test/foundation/diagnostics_test.dart +++ b/packages/flutter/test/foundation/diagnostics_test.dart @@ -59,10 +59,10 @@ void validateNodeJsonSerializationHelper(Map json, DiagnosticsN expect(json['name'], equals(node.name)); expect(json['showSeparator'] ?? true, equals(node.showSeparator)); expect(json['description'], equals(node.toDescription())); - expect(json['level'] ?? describeEnum(DiagnosticLevel.info), equals(describeEnum(node.level))); + expect(json['level'] ?? DiagnosticLevel.info.name, equals(node.level.name)); expect(json['showName'] ?? true, equals(node.showName)); expect(json['emptyBodyDescription'], equals(node.emptyBodyDescription)); - expect(json['style'] ?? describeEnum(DiagnosticsTreeStyle.sparse), equals(describeEnum(node.style!))); + expect(json['style'] ?? DiagnosticsTreeStyle.sparse.name, equals(node.style!.name)); expect(json['type'], equals(node.runtimeType.toString())); expect(json['hasChildren'] ?? false, equals(node.getChildren().isNotEmpty)); } diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index b1ee467560..3f78736f69 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_test.dart @@ -100,8 +100,8 @@ void main() { center = tester.getCenter(title); size = tester.getSize(title); - expect(center.dx, greaterThan(400 - size.width / 2.0), reason: 'on ${describeEnum(platform)}'); - expect(center.dx, lessThan(400 + size.width / 2.0), reason: 'on ${describeEnum(platform)}'); + expect(center.dx, greaterThan(400 - size.width / 2.0), reason: 'on ${platform.name}'); + expect(center.dx, lessThan(400 + size.width / 2.0), reason: 'on ${platform.name}'); // One action is still centered. @@ -121,8 +121,8 @@ void main() { center = tester.getCenter(title); size = tester.getSize(title); - expect(center.dx, greaterThan(400 - size.width / 2.0), reason: 'on ${describeEnum(platform)}'); - expect(center.dx, lessThan(400 + size.width / 2.0), reason: 'on ${describeEnum(platform)}'); + expect(center.dx, greaterThan(400 - size.width / 2.0), reason: 'on ${platform.name}'); + expect(center.dx, lessThan(400 + size.width / 2.0), reason: 'on ${platform.name}'); // Two actions is left aligned again. @@ -143,7 +143,7 @@ void main() { center = tester.getCenter(title); size = tester.getSize(title); - expect(center.dx, lessThan(400 - size.width / 2.0), reason: 'on ${describeEnum(platform)}'); + expect(center.dx, lessThan(400 - size.width / 2.0), reason: 'on ${platform.name}'); } }); diff --git a/packages/flutter/test/material/slider_test.dart b/packages/flutter/test/material/slider_test.dart index 8897d5e3e7..4e2b8641a4 100644 --- a/packages/flutter/test/material/slider_test.dart +++ b/packages/flutter/test/material/slider_test.dart @@ -2138,11 +2138,11 @@ void main() { expect(find.byType(Slider), findsOneWidget); expect(find.byType(CupertinoSlider), findsOneWidget); - expect(value, 0.5, reason: 'on ${describeEnum(platform)}'); + expect(value, 0.5, reason: 'on ${platform.name}'); final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(CupertinoSlider))); // Drag to the right end of the track. await gesture.moveBy(const Offset(600.0, 0.0)); - expect(value, 1.0, reason: 'on ${describeEnum(platform)}'); + expect(value, 1.0, reason: 'on ${platform.name}'); await gesture.up(); } @@ -2153,11 +2153,11 @@ void main() { expect(find.byType(Slider), findsOneWidget); expect(find.byType(CupertinoSlider), findsNothing); - expect(value, 0.5, reason: 'on ${describeEnum(platform)}'); + expect(value, 0.5, reason: 'on ${platform.name}'); final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(Slider))); // Drag to the right end of the track. await gesture.moveBy(const Offset(600.0, 0.0)); - expect(value, 1.0, reason: 'on ${describeEnum(platform)}'); + expect(value, 1.0, reason: 'on ${platform.name}'); await gesture.up(); } }); diff --git a/packages/flutter/test/material/switch_list_tile_test.dart b/packages/flutter/test/material/switch_list_tile_test.dart index 6ce3a4aee6..47ce364cf5 100644 --- a/packages/flutter/test/material/switch_list_tile_test.dart +++ b/packages/flutter/test/material/switch_list_tile_test.dart @@ -198,10 +198,10 @@ void main() { value = false; await tester.pumpWidget(buildFrame(platform)); expect(find.byType(CupertinoSwitch), findsOneWidget); - expect(value, isFalse, reason: 'on ${describeEnum(platform)}'); + expect(value, isFalse, reason: 'on ${platform.name}'); await tester.tap(find.byType(SwitchListTile)); - expect(value, isTrue, reason: 'on ${describeEnum(platform)}'); + expect(value, isTrue, reason: 'on ${platform.name}'); } for (final TargetPlatform platform in [ TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows ]) { @@ -210,9 +210,9 @@ void main() { await tester.pumpAndSettle(); // Finish the theme change animation. expect(find.byType(CupertinoSwitch), findsNothing); - expect(value, isFalse, reason: 'on ${describeEnum(platform)}'); + expect(value, isFalse, reason: 'on ${platform.name}'); await tester.tap(find.byType(SwitchListTile)); - expect(value, isTrue, reason: 'on ${describeEnum(platform)}'); + expect(value, isTrue, reason: 'on ${platform.name}'); } }); diff --git a/packages/flutter/test/material/switch_test.dart b/packages/flutter/test/material/switch_test.dart index c15d1dc91e..73c205ae36 100644 --- a/packages/flutter/test/material/switch_test.dart +++ b/packages/flutter/test/material/switch_test.dart @@ -752,14 +752,14 @@ void main() { for (final TargetPlatform platform in [ TargetPlatform.iOS, TargetPlatform.macOS ]) { value = false; await tester.pumpWidget(buildFrame(platform)); - expect(find.byType(CupertinoSwitch), findsOneWidget, reason: 'on ${describeEnum(platform)}'); + expect(find.byType(CupertinoSwitch), findsOneWidget, reason: 'on ${platform.name}'); final CupertinoSwitch adaptiveSwitch = tester.widget(find.byType(CupertinoSwitch)); - expect(adaptiveSwitch.trackColor, inactiveTrackColor, reason: 'on ${describeEnum(platform)}'); + expect(adaptiveSwitch.trackColor, inactiveTrackColor, reason: 'on ${platform.name}'); - expect(value, isFalse, reason: 'on ${describeEnum(platform)}'); + expect(value, isFalse, reason: 'on ${platform.name}'); await tester.tap(find.byType(Switch)); - expect(value, isTrue, reason: 'on ${describeEnum(platform)}'); + expect(value, isTrue, reason: 'on ${platform.name}'); } for (final TargetPlatform platform in [ TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows ]) { @@ -767,9 +767,9 @@ void main() { await tester.pumpWidget(buildFrame(platform)); await tester.pumpAndSettle(); // Finish the theme change animation. expect(find.byType(CupertinoSwitch), findsNothing); - expect(value, isFalse, reason: 'on ${describeEnum(platform)}'); + expect(value, isFalse, reason: 'on ${platform.name}'); await tester.tap(find.byType(Switch)); - expect(value, isTrue, reason: 'on ${describeEnum(platform)}'); + expect(value, isTrue, reason: 'on ${platform.name}'); } }); diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 887481196b..f61765592c 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -641,7 +641,7 @@ void main() { await expectLater( find.byKey(const ValueKey(1)), matchesGoldenFile( - 'text_field_cursor_test_${describeEnum(debugDefaultTargetPlatformOverride!).toLowerCase()}.material.1.png', + 'text_field_cursor_test_${debugDefaultTargetPlatformOverride!.name.toLowerCase()}.material.1.png', ), ); }, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.macOS })); diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 03b0855e1b..30f6cb2b46 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -5054,7 +5054,7 @@ void main() { testWidgets( 'keyboard shortcuts respect read-only', (WidgetTester tester) async { - final String platform = describeEnum(defaultTargetPlatform).toLowerCase(); + final String platform = defaultTargetPlatform.name.toLowerCase(); final TextEditingController controller = TextEditingController(text: testText); controller.selection = const TextSelection( baseOffset: 0, diff --git a/packages/flutter/test/widgets/semantics_tester.dart b/packages/flutter/test/widgets/semantics_tester.dart index 03cc46dae7..65a4164781 100644 --- a/packages/flutter/test/widgets/semantics_tester.dart +++ b/packages/flutter/test/widgets/semantics_tester.dart @@ -823,7 +823,7 @@ class _IncludesNodeWith extends Matcher { if (label != null) 'label "$label"', if (value != null) 'value "$value"', if (hint != null) 'hint "$hint"', - if (textDirection != null) ' (${describeEnum(textDirection!)})', + if (textDirection != null) ' (${textDirection!.name})', if (actions != null) 'actions "${actions!.join(', ')}"', if (flags != null) 'flags "${flags!.join(', ')}"', if (scrollPosition != null) 'scrollPosition "$scrollPosition"',