Skip to content

Commit a397a24

Browse files
authored
Replace semantic announcements in expansion tile for Android (#179917)
fixes: #177785 ## 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]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- 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
1 parent 03d6c1d commit a397a24

2 files changed

Lines changed: 103 additions & 27 deletions

File tree

‎packages/flutter/lib/src/material/expansion_tile.dart‎

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,10 @@ class _ExpansionTileState extends State<ExpansionTile> {
550550
_timer?.cancel();
551551
_timer = null;
552552
});
553-
} else {
553+
}
554+
// SemanticsService.sendAnnouncement is deprecated on android.
555+
// We use live region to achieve the announcement effect instead.
556+
else if (defaultTargetPlatform != TargetPlatform.android) {
554557
SemanticsService.sendAnnouncement(View.of(context), stateHint, textDirection);
555558
}
556559
widget.onExpansionChanged?.call(_tileController.isExpanded);
@@ -604,31 +607,40 @@ class _ExpansionTileState extends State<ExpansionTile> {
604607
_ => _tileController.isExpanded ? localizations.collapsedHint : localizations.expandedHint,
605608
};
606609

607-
return Semantics(
608-
hint: semanticsHint,
609-
onTapHint: onTapHint,
610-
child: ListTileTheme.merge(
611-
iconColor: _iconColor.value ?? _expansionTileTheme.iconColor,
612-
textColor: _headerColor.value,
613-
child: ListTile(
614-
enabled: widget.enabled,
615-
onTap: _tileController.isExpanded ? _tileController.collapse : _tileController.expand,
616-
dense: widget.dense,
617-
splashColor: widget.splashColor,
618-
visualDensity: widget.visualDensity,
619-
enableFeedback: widget.enableFeedback,
620-
contentPadding: widget.tilePadding ?? _expansionTileTheme.tilePadding,
621-
leading: widget.leading ?? _buildLeadingIcon(context, animation),
622-
title: widget.title,
623-
subtitle: widget.subtitle,
624-
trailing: widget.showTrailingIcon
625-
? widget.trailing ?? _buildTrailingIcon(context, animation)
626-
: null,
627-
minTileHeight: widget.minTileHeight,
628-
internalAddSemanticForOnTap: widget.internalAddSemanticForOnTap,
629-
),
610+
final Widget child = ListTileTheme.merge(
611+
iconColor: _iconColor.value ?? _expansionTileTheme.iconColor,
612+
textColor: _headerColor.value,
613+
child: ListTile(
614+
enabled: widget.enabled,
615+
onTap: _tileController.isExpanded ? _tileController.collapse : _tileController.expand,
616+
dense: widget.dense,
617+
splashColor: widget.splashColor,
618+
visualDensity: widget.visualDensity,
619+
enableFeedback: widget.enableFeedback,
620+
contentPadding: widget.tilePadding ?? _expansionTileTheme.tilePadding,
621+
leading: widget.leading ?? _buildLeadingIcon(context, animation),
622+
title: widget.title,
623+
subtitle: widget.subtitle,
624+
trailing: widget.showTrailingIcon
625+
? widget.trailing ?? _buildTrailingIcon(context, animation)
626+
: null,
627+
minTileHeight: widget.minTileHeight,
628+
internalAddSemanticForOnTap: widget.internalAddSemanticForOnTap,
630629
),
631630
);
631+
632+
if (defaultTargetPlatform == TargetPlatform.android) {
633+
return Semantics(
634+
// Live region used to announce state changes (e.g., "expanded" or "collapsed")
635+
// without taking focus.
636+
// blockNode prevents this node from being part of the focus traversal.
637+
label: semanticsHint,
638+
liveRegion: true,
639+
accessiblityFocusBlockType: AccessiblityFocusBlockType.blockNode,
640+
child: Semantics(hint: semanticsHint, onTapHint: onTapHint, child: child),
641+
);
642+
}
643+
return Semantics(hint: semanticsHint, onTapHint: onTapHint, child: child);
632644
}
633645

634646
Widget _buildBody(BuildContext context, Animation<double> animation) {

‎packages/flutter/test/material/expansion_tile_test.dart‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,11 @@ void main() {
827827
);
828828
handle.dispose();
829829
},
830-
// [intended] https://github.com/flutter/flutter/issues/122101.
831-
skip: defaultTargetPlatform == TargetPlatform.iOS,
830+
// [intended] iOS: https://github.com/flutter/flutter/issues/122101.
831+
// android: https://github.com/flutter/flutter/issues/165510
832+
skip:
833+
defaultTargetPlatform == TargetPlatform.iOS ||
834+
defaultTargetPlatform == TargetPlatform.android,
832835
);
833836

834837
// This is a regression test for https://github.com/flutter/flutter/issues/132264.
@@ -1917,7 +1920,7 @@ void main() {
19171920
);
19181921

19191922
// Regression test for https://github.com/flutter/flutter/issues/173060
1920-
group('Semantics tests for non-iOS/macOS platforms', () {
1923+
group('Semantics tests for non-iOS/macOS/android platforms', () {
19211924
testWidgets(
19221925
'Semantics hint should show current state',
19231926
(WidgetTester tester) async {
@@ -2009,4 +2012,65 @@ void main() {
20092012
}),
20102013
);
20112014
});
2015+
group('Semantics tests for android platform', () {
2016+
testWidgets(
2017+
'Semantics liveregion updates when expansion state changes',
2018+
(WidgetTester tester) async {
2019+
final SemanticsHandle handle = tester.ensureSemantics();
2020+
const localizations = DefaultMaterialLocalizations();
2021+
await tester.pumpWidget(
2022+
const MaterialApp(
2023+
home: Material(
2024+
child: ExpansionTile(title: Text('Test Tile'), children: <Widget>[Text('Child')]),
2025+
),
2026+
),
2027+
);
2028+
2029+
// Initially collapsed - live region label is "Collapsed".
2030+
2031+
SemanticsNode liveRegionSemantics = tester.getSemantics(
2032+
find.ancestor(
2033+
of: find.byType(ListTile),
2034+
matching: find.byWidgetPredicate(
2035+
(Widget widget) => widget is Semantics && (widget.properties.liveRegion ?? false),
2036+
),
2037+
),
2038+
);
2039+
expect(liveRegionSemantics.label, localizations.expandedHint);
2040+
2041+
// Tap to expand.
2042+
await tester.tap(find.text('Test Tile'));
2043+
await tester.pumpAndSettle();
2044+
2045+
// Now expanded - should show "Expanded".
2046+
liveRegionSemantics = tester.getSemantics(
2047+
find.ancestor(
2048+
of: find.byType(ListTile),
2049+
matching: find.byWidgetPredicate(
2050+
(Widget widget) => widget is Semantics && (widget.properties.liveRegion ?? false),
2051+
),
2052+
),
2053+
);
2054+
expect(liveRegionSemantics.label, localizations.collapsedHint);
2055+
2056+
// Tap to collapse.
2057+
await tester.tap(find.text('Test Tile'));
2058+
await tester.pumpAndSettle();
2059+
2060+
// Back to collapsed - should show "Collapsed" again.
2061+
liveRegionSemantics = tester.getSemantics(
2062+
find.ancestor(
2063+
of: find.byType(ListTile),
2064+
matching: find.byWidgetPredicate(
2065+
(Widget widget) => widget is Semantics && (widget.properties.liveRegion ?? false),
2066+
),
2067+
),
2068+
);
2069+
expect(liveRegionSemantics.label, localizations.expandedHint);
2070+
2071+
handle.dispose();
2072+
},
2073+
variant: const TargetPlatformVariant(<TargetPlatform>{TargetPlatform.android}),
2074+
);
2075+
});
20122076
}

0 commit comments

Comments
 (0)