Adds aria-controls support (#163894)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

adding a new property in semantics properties called
controlsVisibilityOfNodes, where developer can assign
SemanticsProperties.identifier of other nodes to indicates which nodes'
visibilities this node controls

fixes https://github.com/flutter/flutter/issues/162125

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
chunhtai
2025-03-06 14:41:09 -08:00
committed by GitHub
parent 66e910d27e
commit e0b9869468
18 changed files with 365 additions and 12 deletions

View File

@@ -4540,6 +4540,10 @@ class RenderSemanticsAnnotations extends RenderProxyBox {
if (properties.role != null) {
config.role = _properties.role!;
}
if (_properties.controlsNodes != null) {
config.controlsNodes = _properties.controlsNodes;
}
// Registering _perform* as action handlers instead of the user provided
// ones to ensure that changing a user provided handler from a non-null to
// another non-null value doesn't require a semantics update.

View File

@@ -595,6 +595,7 @@ class SemanticsData with Diagnosticable {
required this.headingLevel,
required this.linkUrl,
required this.role,
required this.controlsNodes,
this.tags,
this.transform,
this.customSemanticsActionIds,
@@ -855,6 +856,11 @@ class SemanticsData with Diagnosticable {
/// {@macro flutter.semantics.SemanticsNode.role}
final SemanticsRole role;
/// {@macro flutter.semantics.SemanticsNode.controlsNodes}
///
/// {@macro flutter.semantics.SemanticsProperties.controlsNodes}
final Set<String>? controlsNodes;
/// Whether [flags] contains the given flag.
bool hasFlag(SemanticsFlag flag) => (flags & flag.index) != 0;
@@ -912,6 +918,9 @@ class SemanticsData with Diagnosticable {
properties.add(DoubleProperty('scrollExtentMax', scrollExtentMax, defaultValue: null));
properties.add(IntProperty('headingLevel', headingLevel, defaultValue: 0));
properties.add(DiagnosticsProperty<Uri>('linkUrl', linkUrl, defaultValue: null));
if (controlsNodes != null) {
properties.add(IterableProperty<String>('controls', controlsNodes, ifEmpty: null));
}
}
@override
@@ -944,7 +953,8 @@ class SemanticsData with Diagnosticable {
other.headingLevel == headingLevel &&
other.linkUrl == linkUrl &&
other.role == role &&
_sortedListsEqual(other.customSemanticsActionIds, customSemanticsActionIds);
_sortedListsEqual(other.customSemanticsActionIds, customSemanticsActionIds) &&
setEquals<String>(controlsNodes, other.controlsNodes);
}
@override
@@ -978,6 +988,7 @@ class SemanticsData with Diagnosticable {
linkUrl,
customSemanticsActionIds == null ? null : Object.hashAll(customSemanticsActionIds!),
role,
controlsNodes == null ? null : Object.hashAll(controlsNodes!),
),
);
@@ -1146,6 +1157,7 @@ class SemanticsProperties extends DiagnosticableTree {
this.onDismiss,
this.customSemanticsActions,
this.role,
this.controlsNodes,
}) : assert(
label == null || attributedLabel == null,
'Only one of label or attributedLabel should be provided',
@@ -1947,6 +1959,17 @@ class SemanticsProperties extends DiagnosticableTree {
/// {@endtemplate}
final SemanticsRole? role;
/// The [SemanticsNode.identifier]s of widgets controlled by this subtree.
///
/// {@template flutter.semantics.SemanticsProperties.controlsNodes}
/// If a widget is controlling the visibility or content of another widget,
/// for example, [Tab]s control child visibilities of [TabBarView] or
/// [ExpansionTile] controls visibility of its expanded content, one must
/// assign a [SemanticsNode.identifier] to the content and also provide a set
/// of identifiers including the content's identifier to this property.
/// {@endtemplate}
final Set<String>? controlsNodes;
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
@@ -2904,6 +2927,14 @@ class SemanticsNode with DiagnosticableTreeMixin {
SemanticsRole get role => _role;
SemanticsRole _role = _kEmptyConfig.role;
/// {@template flutter.semantics.SemanticsNode.controlsNodes}
/// The [SemanticsNode.identifier]s of widgets controlled by this node.
/// {@endtemplate}
///
/// {@macro flutter.semantics.SemanticsProperties.controlsNodes}
Set<String>? get controlsNodes => _controlsNodes;
Set<String>? _controlsNodes = _kEmptyConfig.controlsNodes;
bool _canPerformAction(SemanticsAction action) => _actions.containsKey(action);
static final SemanticsConfiguration _kEmptyConfig = SemanticsConfiguration();
@@ -2970,6 +3001,7 @@ class SemanticsNode with DiagnosticableTreeMixin {
_headingLevel = config._headingLevel;
_linkUrl = config._linkUrl;
_role = config._role;
_controlsNodes = config._controlsNodes;
_replaceChildren(childrenInInversePaintOrder ?? const <SemanticsNode>[]);
if (mergeAllDescendantsIntoThisNodeValueChanged) {
@@ -3019,6 +3051,7 @@ class SemanticsNode with DiagnosticableTreeMixin {
double thickness = _thickness;
Uri? linkUrl = _linkUrl;
SemanticsRole role = _role;
Set<String>? controlsNodes = _controlsNodes;
final Set<int> customSemanticsActionIds = <int>{};
for (final CustomSemanticsAction action in _customSemanticsActions.keys) {
customSemanticsActionIds.add(CustomSemanticsAction.getIdentifier(action));
@@ -3118,6 +3151,12 @@ class SemanticsNode with DiagnosticableTreeMixin {
thickness = math.max(thickness, node._thickness + node._elevation);
if (controlsNodes == null) {
controlsNodes = node._controlsNodes;
} else if (node._controlsNodes != null) {
controlsNodes = <String>{...controlsNodes!, ...node._controlsNodes!};
}
return true;
});
}
@@ -3151,6 +3190,7 @@ class SemanticsNode with DiagnosticableTreeMixin {
headingLevel: headingLevel,
linkUrl: linkUrl,
role: role,
controlsNodes: controlsNodes,
);
}
@@ -3236,6 +3276,7 @@ class SemanticsNode with DiagnosticableTreeMixin {
headingLevel: data.headingLevel,
linkUrl: data.linkUrl?.toString() ?? '',
role: data.role,
controlsNodes: data.controlsNodes?.toList(),
);
_dirty = false;
}
@@ -5379,6 +5420,15 @@ class SemanticsConfiguration {
_hasBeenAnnotated = true;
}
/// The [SemanticsNode.identifier]s of widgets controlled by this node.
Set<String>? get controlsNodes => _controlsNodes;
Set<String>? _controlsNodes;
set controlsNodes(Set<String>? value) {
assert(value != null);
_controlsNodes = value;
_hasBeenAnnotated = true;
}
// TAGS
/// The set of tags that this configuration wants to add to all child
@@ -5543,6 +5593,12 @@ class SemanticsConfiguration {
_thickness = math.max(_thickness, child._thickness + child._elevation);
if (_controlsNodes == null) {
_controlsNodes = child._controlsNodes;
} else if (child._controlsNodes != null) {
_controlsNodes = <String>{..._controlsNodes!, ...child._controlsNodes!};
}
_hasBeenAnnotated = hasBeenAnnotated || child.hasBeenAnnotated;
}
@@ -5584,7 +5640,8 @@ class SemanticsConfiguration {
..isBlockingUserActions = isBlockingUserActions
.._headingLevel = _headingLevel
.._linkUrl = _linkUrl
.._role = _role;
.._role = _role
.._controlsNodes = _controlsNodes;
}
}

View File

@@ -7382,6 +7382,7 @@ class Semantics extends SingleChildRenderObjectWidget {
VoidCallback? onFocus,
Map<CustomSemanticsAction, VoidCallback>? customSemanticsActions,
ui.SemanticsRole? role,
Set<String>? controlsNodes,
}) : this.fromProperties(
key: key,
child: child,
@@ -7457,6 +7458,7 @@ class Semantics extends SingleChildRenderObjectWidget {
? SemanticsHintOverrides(onTapHint: onTapHint, onLongPressHint: onLongPressHint)
: null,
role: role,
controlsNodes: controlsNodes,
),
);

View File

@@ -229,6 +229,7 @@ class SemanticsUpdateBuilderSpy extends Fake implements ui.SemanticsUpdateBuilde
int headingLevel = 0,
String? linkUrl,
ui.SemanticsRole role = ui.SemanticsRole.none,
required List<String>? controlsNodes,
}) {
// Makes sure we don't send the same id twice.
assert(!observations.containsKey(id));

View File

@@ -380,6 +380,47 @@ void main() {
expect(attributedHint.attributes[0].range, const TextRange(start: 1, end: 2));
});
testWidgets('Semantics can set controls visibility of nodes', (WidgetTester tester) async {
final UniqueKey key = UniqueKey();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Semantics(
key: key,
controlsNodes: const <String>{'abc'},
child: const Placeholder(),
),
),
),
);
final SemanticsNode node = tester.getSemantics(find.byKey(key));
final SemanticsData data = node.getSemanticsData();
expect(data.controlsNodes!.length, 1);
expect(data.controlsNodes!.first, 'abc');
});
testWidgets('Semantics can set controls visibility of nodes', (WidgetTester tester) async {
final UniqueKey key = UniqueKey();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Semantics(
key: key,
controlsNodes: const <String>{'abc', 'ghi'},
child: Semantics(
controlsNodes: const <String>{'abc', 'def'},
child: const Placeholder(),
),
),
),
),
);
final SemanticsNode node = tester.getSemantics(find.byKey(key));
final SemanticsData data = node.getSemanticsData();
expect(data.controlsNodes!.length, 3);
expect(data.controlsNodes, <String>{'abc', 'ghi', 'def'});
});
testWidgets('Semantics can merge attributed strings', (WidgetTester tester) async {
final UniqueKey key = UniqueKey();
await tester.pumpWidget(

View File

@@ -730,6 +730,7 @@ void main() {
headingLevel: 0,
linkUrl: Uri(path: 'l'),
role: ui.SemanticsRole.none,
controlsNodes: null,
);
final _FakeSemanticsNode node = _FakeSemanticsNode(data);
@@ -1029,6 +1030,7 @@ void main() {
headingLevel: 0,
linkUrl: Uri(path: 'l'),
role: ui.SemanticsRole.none,
controlsNodes: null,
);
final _FakeSemanticsNode node = _FakeSemanticsNode(data);
@@ -1125,6 +1127,7 @@ void main() {
headingLevel: 0,
linkUrl: null,
role: ui.SemanticsRole.none,
controlsNodes: null,
);
final _FakeSemanticsNode node = _FakeSemanticsNode(data);
@@ -1228,6 +1231,7 @@ void main() {
headingLevel: 0,
linkUrl: null,
role: ui.SemanticsRole.none,
controlsNodes: null,
);
final _FakeSemanticsNode emptyNode = _FakeSemanticsNode(emptyData);
@@ -1259,6 +1263,7 @@ void main() {
headingLevel: 0,
linkUrl: Uri(path: 'l'),
role: ui.SemanticsRole.none,
controlsNodes: null,
);
final _FakeSemanticsNode fullNode = _FakeSemanticsNode(fullData);
@@ -1346,6 +1351,7 @@ void main() {
headingLevel: 0,
linkUrl: null,
role: ui.SemanticsRole.none,
controlsNodes: null,
);
final _FakeSemanticsNode node = _FakeSemanticsNode(data);