Add toggled semantics to `ToggleButtons` (#106096)
diff --git a/packages/flutter/lib/src/material/toggle_buttons.dart b/packages/flutter/lib/src/material/toggle_buttons.dart
index 0bed52c..2636baf 100644
--- a/packages/flutter/lib/src/material/toggle_buttons.dart
+++ b/packages/flutter/lib/src/material/toggle_buttons.dart
@@ -790,6 +790,7 @@
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 1d2ad99..8607da9 100644
--- a/packages/flutter/test/material/toggle_buttons_test.dart
+++ b/packages/flutter/test/material/toggle_buttons_test.dart
@@ -2006,6 +2006,7 @@
SemanticsFlag.isButton,
SemanticsFlag.isEnabled,
SemanticsFlag.hasEnabledState,
+ SemanticsFlag.hasToggledState,
SemanticsFlag.isFocusable,
],
actions: <SemanticsAction>[
@@ -2018,6 +2019,7 @@
SemanticsFlag.isButton,
SemanticsFlag.isEnabled,
SemanticsFlag.hasEnabledState,
+ SemanticsFlag.hasToggledState,
SemanticsFlag.isFocusable,
],
actions: <SemanticsAction>[
@@ -2030,6 +2032,7 @@
SemanticsFlag.isButton,
SemanticsFlag.isEnabled,
SemanticsFlag.hasEnabledState,
+ SemanticsFlag.hasToggledState,
SemanticsFlag.isFocusable,
],
actions: <SemanticsAction>[
@@ -2047,4 +2050,63 @@
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 <bool>[false, true],
+ onPressed: (int index) {},
+ children: const <Widget>[
+ Icon(Icons.check),
+ Icon(Icons.access_alarm),
+ ],
+ ),
+ ),
+ ),
+ );
+
+ expect(
+ semantics,
+ hasSemantics(
+ TestSemantics.root(
+ children: <TestSemantics>[
+ TestSemantics(
+ flags: <SemanticsFlag>[
+ SemanticsFlag.isButton,
+ SemanticsFlag.isEnabled,
+ SemanticsFlag.hasEnabledState,
+ SemanticsFlag.hasToggledState,
+ SemanticsFlag.isFocusable,
+ ],
+ actions: <SemanticsAction>[
+ SemanticsAction.tap,
+ ],
+ ),
+ TestSemantics(
+ flags: <SemanticsFlag>[
+ SemanticsFlag.isButton,
+ SemanticsFlag.isEnabled,
+ SemanticsFlag.hasEnabledState,
+ SemanticsFlag.isToggled,
+ SemanticsFlag.hasToggledState,
+ SemanticsFlag.isFocusable,
+ ],
+ actions: <SemanticsAction>[
+ SemanticsAction.tap,
+ ],
+ ),
+ ],
+ ),
+ ignoreId: true,
+ ignoreRect: true,
+ ignoreTransform: true,
+ ),
+ );
+
+ semantics.dispose();
+ });
}