diff --git a/packages/flutter/lib/src/cupertino/radio.dart b/packages/flutter/lib/src/cupertino/radio.dart index 67898be89f..342b38b44e 100644 --- a/packages/flutter/lib/src/cupertino/radio.dart +++ b/packages/flutter/lib/src/cupertino/radio.dart @@ -2,6 +2,7 @@ // 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 'colors.dart'; @@ -232,9 +233,24 @@ class _CupertinoRadioState extends State> with TickerProvid final Color effectiveFillColor = widget.fillColor ?? CupertinoColors.white; + final bool? accessibilitySelected; + // Apple devices also use `selected` to annotate radio button's semantics + // state. + switch(defaultTargetPlatform) { + case TargetPlatform.android: + case TargetPlatform.fuchsia: + case TargetPlatform.linux: + case TargetPlatform.windows: + accessibilitySelected = null; + case TargetPlatform.iOS: + case TargetPlatform.macOS: + accessibilitySelected = widget._selected; + } + return Semantics( inMutuallyExclusiveGroup: true, checked: widget._selected, + selected: accessibilitySelected, child: buildToggleable( focusNode: widget.focusNode, autofocus: widget.autofocus, diff --git a/packages/flutter/lib/src/material/radio.dart b/packages/flutter/lib/src/material/radio.dart index 7a7622edf3..0978000f2c 100644 --- a/packages/flutter/lib/src/material/radio.dart +++ b/packages/flutter/lib/src/material/radio.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; import 'color_scheme.dart'; import 'colors.dart'; @@ -498,10 +499,24 @@ class _RadioState extends State> with TickerProviderStateMixin, Togg ? effectiveActivePressedOverlayColor : effectiveInactivePressedOverlayColor; } + final bool? accessibilitySelected; + // Apple devices also use `selected` to annotate radio button's semantics + // state. + switch(defaultTargetPlatform) { + case TargetPlatform.android: + case TargetPlatform.fuchsia: + case TargetPlatform.linux: + case TargetPlatform.windows: + accessibilitySelected = null; + case TargetPlatform.iOS: + case TargetPlatform.macOS: + accessibilitySelected = widget._selected; + } return Semantics( inMutuallyExclusiveGroup: true, checked: widget._selected, + selected: accessibilitySelected, child: buildToggleable( focusNode: widget.focusNode, autofocus: widget.autofocus, diff --git a/packages/flutter/test/cupertino/radio_test.dart b/packages/flutter/test/cupertino/radio_test.dart index f5a4e45e2f..e1dd7565ca 100644 --- a/packages/flutter/test/cupertino/radio_test.dart +++ b/packages/flutter/test/cupertino/radio_test.dart @@ -118,6 +118,41 @@ void main() { expect(log, equals([1])); }); + testWidgets('Radio selected semantics - platform adaptive', (WidgetTester tester) async { + final SemanticsTester semantics = SemanticsTester(tester); + + await tester.pumpWidget(CupertinoApp( + home: Center( + child: CupertinoRadio( + value: 1, + groupValue: 1, + onChanged: (int? i) { }, + ), + ), + )); + + final bool isApple = defaultTargetPlatform == TargetPlatform.iOS || + defaultTargetPlatform == TargetPlatform.macOS; + expect( + semantics, + includesNodeWith( + flags: [ + SemanticsFlag.isInMutuallyExclusiveGroup, + SemanticsFlag.hasCheckedState, + SemanticsFlag.hasEnabledState, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, + SemanticsFlag.isChecked, + if (isApple) SemanticsFlag.isSelected, + ], + actions: [ + SemanticsAction.tap, + ], + ), + ); + semantics.dispose(); + }, variant: TargetPlatformVariant.all()); + testWidgets('Radio semantics', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester); diff --git a/packages/flutter/test/material/radio_test.dart b/packages/flutter/test/material/radio_test.dart index f10efde59a..e78cfde123 100644 --- a/packages/flutter/test/material/radio_test.dart +++ b/packages/flutter/test/material/radio_test.dart @@ -194,6 +194,40 @@ void main() { expect(tester.getSize(find.byKey(key2)), const Size(40.0, 40.0)); }); + testWidgets('Radio selected semantics - platform adaptive', (WidgetTester tester) async { + final SemanticsTester semantics = SemanticsTester(tester); + + await tester.pumpWidget(Theme( + data: theme, + child: Material( + child: Radio( + value: 1, + groupValue: 1, + onChanged: (int? i) {}, + ), + ), + )); + final bool isApple = defaultTargetPlatform == TargetPlatform.iOS || + defaultTargetPlatform == TargetPlatform.macOS; + expect( + semantics, + includesNodeWith( + flags: [ + SemanticsFlag.isInMutuallyExclusiveGroup, + SemanticsFlag.hasCheckedState, + SemanticsFlag.hasEnabledState, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, + SemanticsFlag.isChecked, + if (isApple) SemanticsFlag.isSelected, + ], + actions: [ + SemanticsAction.tap, + ], + ), + ); + semantics.dispose(); + }, variant: TargetPlatformVariant.all()); testWidgets('Radio semantics', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester);