diff --git a/packages/flutter/lib/src/material/toggle_buttons.dart b/packages/flutter/lib/src/material/toggle_buttons.dart index 0bed52cd35..2636baf79d 100644 --- a/packages/flutter/lib/src/material/toggle_buttons.dart +++ b/packages/flutter/lib/src/material/toggle_buttons.dart @@ -790,6 +790,7 @@ class ToggleButtons extends StatelessWidget { return MergeSemantics( child: Semantics( container: true, + toggled: isSelected[index], enabled: onPressed != null, child: _InputPadding( minSize: minPaddingSize, diff --git a/packages/flutter/test/material/toggle_buttons_test.dart b/packages/flutter/test/material/toggle_buttons_test.dart index 1d2ad990e3..8607da96ec 100644 --- a/packages/flutter/test/material/toggle_buttons_test.dart +++ b/packages/flutter/test/material/toggle_buttons_test.dart @@ -2006,6 +2006,7 @@ void main() { SemanticsFlag.isButton, SemanticsFlag.isEnabled, SemanticsFlag.hasEnabledState, + SemanticsFlag.hasToggledState, SemanticsFlag.isFocusable, ], actions: [ @@ -2018,6 +2019,7 @@ void main() { SemanticsFlag.isButton, SemanticsFlag.isEnabled, SemanticsFlag.hasEnabledState, + SemanticsFlag.hasToggledState, SemanticsFlag.isFocusable, ], actions: [ @@ -2030,6 +2032,7 @@ void main() { SemanticsFlag.isButton, SemanticsFlag.isEnabled, SemanticsFlag.hasEnabledState, + SemanticsFlag.hasToggledState, SemanticsFlag.isFocusable, ], actions: [ @@ -2045,6 +2048,65 @@ void main() { ), ); + semantics.dispose(); + }); + + testWidgets('Toggle buttons have correct semantics', (WidgetTester tester) async { + final SemanticsTester semantics = SemanticsTester(tester); + + await tester.pumpWidget( + Material( + child: boilerplate( + child: ToggleButtons( + isSelected: const [false, true], + onPressed: (int index) {}, + children: const [ + Icon(Icons.check), + Icon(Icons.access_alarm), + ], + ), + ), + ), + ); + + expect( + semantics, + hasSemantics( + TestSemantics.root( + children: [ + TestSemantics( + flags: [ + SemanticsFlag.isButton, + SemanticsFlag.isEnabled, + SemanticsFlag.hasEnabledState, + SemanticsFlag.hasToggledState, + SemanticsFlag.isFocusable, + ], + actions: [ + SemanticsAction.tap, + ], + ), + TestSemantics( + flags: [ + SemanticsFlag.isButton, + SemanticsFlag.isEnabled, + SemanticsFlag.hasEnabledState, + SemanticsFlag.isToggled, + SemanticsFlag.hasToggledState, + SemanticsFlag.isFocusable, + ], + actions: [ + SemanticsAction.tap, + ], + ), + ], + ), + ignoreId: true, + ignoreRect: true, + ignoreTransform: true, + ), + ); + semantics.dispose(); }); }