diff --git a/packages/flutter/lib/src/material/back_button.dart b/packages/flutter/lib/src/material/back_button.dart index 2921780b2e..4d1f1c874c 100644 --- a/packages/flutter/lib/src/material/back_button.dart +++ b/packages/flutter/lib/src/material/back_button.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'debug.dart'; @@ -28,39 +27,22 @@ class BackButtonIcon extends StatelessWidget { /// the current platform (as obtained from the [Theme]). const BackButtonIcon({ super.key }); - @override - Widget build(BuildContext context) { - final String? semanticsLabel; - final IconData data; - switch (Theme.of(context).platform) { + /// Returns the appropriate "back" icon for the given `platform`. + static IconData _getIconData(TargetPlatform platform) { + switch (platform) { case TargetPlatform.android: case TargetPlatform.fuchsia: case TargetPlatform.linux: case TargetPlatform.windows: - data = Icons.arrow_back; - break; + return Icons.arrow_back; case TargetPlatform.iOS: case TargetPlatform.macOS: - data = Icons.arrow_back_ios; - break; + return Icons.arrow_back_ios; } - // This can't use the platform from Theme because it is the Android OS that - // expects the duplicated tooltip and label. - switch (defaultTargetPlatform) { - case TargetPlatform.android: - semanticsLabel = MaterialLocalizations.of(context).backButtonTooltip; - break; - case TargetPlatform.fuchsia: - case TargetPlatform.linux: - case TargetPlatform.windows: - case TargetPlatform.iOS: - case TargetPlatform.macOS: - semanticsLabel = null; - break; - } - - return Icon(data, semanticLabel: semanticsLabel); } + + @override + Widget build(BuildContext context) => Icon(_getIconData(Theme.of(context).platform)); } /// A Material Design back button. @@ -167,23 +149,8 @@ class CloseButton extends StatelessWidget { @override Widget build(BuildContext context) { assert(debugCheckHasMaterialLocalizations(context)); - final String? semanticsLabel; - // This can't use the platform from Theme because it is the Android OS that - // expects the duplicated tooltip and label. - switch (defaultTargetPlatform) { - case TargetPlatform.android: - semanticsLabel = MaterialLocalizations.of(context).closeButtonTooltip; - break; - case TargetPlatform.fuchsia: - case TargetPlatform.linux: - case TargetPlatform.windows: - case TargetPlatform.iOS: - case TargetPlatform.macOS: - semanticsLabel = null; - break; - } return IconButton( - icon: Icon(Icons.close, semanticLabel: semanticsLabel), + icon: const Icon(Icons.close), color: color, tooltip: MaterialLocalizations.of(context).closeButtonTooltip, onPressed: () { diff --git a/packages/flutter/lib/src/widgets/semantics_debugger.dart b/packages/flutter/lib/src/widgets/semantics_debugger.dart index 1fe00962c3..2d3acbfdc9 100644 --- a/packages/flutter/lib/src/widgets/semantics_debugger.dart +++ b/packages/flutter/lib/src/widgets/semantics_debugger.dart @@ -288,14 +288,10 @@ class _SemanticsDebuggerPainter extends CustomPainter { assert(data.attributedLabel != null); final String message; - // Android will avoid pronouncing duplicating tooltip and label. - // Therefore, having two identical strings is the same as having a single - // string. - final bool shouldIgnoreDuplicatedLabel = defaultTargetPlatform == TargetPlatform.android && data.attributedLabel.string == data.tooltip; final String tooltipAndLabel = [ if (data.tooltip.isNotEmpty) data.tooltip, - if (data.attributedLabel.string.isNotEmpty && !shouldIgnoreDuplicatedLabel) + if (data.attributedLabel.string.isNotEmpty) data.attributedLabel.string, ].join('\n'); if (tooltipAndLabel.isEmpty) { diff --git a/packages/flutter/test/material/back_button_test.dart b/packages/flutter/test/material/back_button_test.dart index 09d9fd1d66..d4ff9a92ff 100644 --- a/packages/flutter/test/material/back_button_test.dart +++ b/packages/flutter/test/material/back_button_test.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -153,21 +152,9 @@ void main() { tester.state(find.byType(Navigator)).pushNamed('/next'); await tester.pumpAndSettle(); - final String? expectedLabel; - switch(defaultTargetPlatform) { - case TargetPlatform.android: - expectedLabel = 'Back'; - break; - case TargetPlatform.fuchsia: - case TargetPlatform.iOS: - case TargetPlatform.linux: - case TargetPlatform.macOS: - case TargetPlatform.windows: - expectedLabel = null; - } + expect(tester.getSemantics(find.byType(BackButton)), matchesSemantics( tooltip: 'Back', - label: expectedLabel, isButton: true, hasEnabledState: true, isEnabled: true, @@ -175,51 +162,7 @@ void main() { isFocusable: true, )); handle.dispose(); - }, variant: TargetPlatformVariant.all()); - - testWidgets('CloseButton semantics', (WidgetTester tester) async { - final SemanticsHandle handle = tester.ensureSemantics(); - await tester.pumpWidget( - MaterialApp( - home: const Material(child: Text('Home')), - routes: { - '/next': (BuildContext context) { - return const Material( - child: Center( - child: CloseButton(), - ), - ); - }, - }, - ), - ); - - tester.state(find.byType(Navigator)).pushNamed('/next'); - - await tester.pumpAndSettle(); - final String? expectedLabel; - switch(defaultTargetPlatform) { - case TargetPlatform.android: - expectedLabel = 'Close'; - break; - case TargetPlatform.fuchsia: - case TargetPlatform.iOS: - case TargetPlatform.linux: - case TargetPlatform.macOS: - case TargetPlatform.windows: - expectedLabel = null; - } - expect(tester.getSemantics(find.byType(CloseButton)), matchesSemantics( - tooltip: 'Close', - label: expectedLabel, - isButton: true, - hasEnabledState: true, - isEnabled: true, - hasTapAction: true, - isFocusable: true, - )); - handle.dispose(); - }, variant: TargetPlatformVariant.all()); + }); testWidgets('CloseButton color', (WidgetTester tester) async { await tester.pumpWidget( diff --git a/packages/flutter/test/widgets/semantics_debugger_test.dart b/packages/flutter/test/widgets/semantics_debugger_test.dart index 4fbbeb8a1f..7dd740abe8 100644 --- a/packages/flutter/test/widgets/semantics_debugger_test.dart +++ b/packages/flutter/test/widgets/semantics_debugger_test.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -439,33 +438,6 @@ void main() { ); }); - testWidgets('SemanticsDebugger ignores duplicated label and tooltip for Android', (WidgetTester tester) async { - final Key child = UniqueKey(); - final Key debugger = UniqueKey(); - final bool isPlatformAndroid = defaultTargetPlatform == TargetPlatform.android; - await tester.pumpWidget( - Directionality( - textDirection: TextDirection.ltr, - child: SemanticsDebugger( - key: debugger, - child: Material( - child: Semantics( - container: true, - key: child, - label: 'text', - tooltip: 'text', - ), - ), - ), - ), - ); - - expect( - _getMessageShownInSemanticsDebugger(widgetKey: child, debuggerKey: debugger, tester: tester), - isPlatformAndroid ? 'text' : 'text\ntext', - ); - }, variant: TargetPlatformVariant.all()); - testWidgets('SemanticsDebugger textfield', (WidgetTester tester) async { final UniqueKey textField = UniqueKey(); final UniqueKey debugger = UniqueKey();