Skip to content

Commit 10a178c

Browse files
authored
[material_ui] Fixes SearchAnchor and SearchBar semantics (#12680)
fixes flutter/flutter#190884 Too many inkwell and gesture recognizer wrapping the searchAnchor subtree that all tries to provide the same semantics action. This is ok in in renderobject because gesture arena will pick a winer, but not going to work in semantics ## Pre-Review Checklist **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. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 0259ad7 commit 10a178c

7 files changed

Lines changed: 211 additions & 4 deletions

File tree

‎packages/material_ui/example/lib/search_anchor/search_anchor.2.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class _SearchBarAppState extends State<SearchBarApp> {
2727
body: Column(
2828
children: <Widget>[
2929
SearchAnchor(
30+
enableTapHandling: false,
3031
searchController: controller,
3132
builder: (BuildContext context, SearchController controller) {
3233
return IconButton(

‎packages/material_ui/example/lib/search_anchor/search_anchor.3.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class _AsyncSearchAnchorState extends State<_AsyncSearchAnchor> {
4343
@override
4444
Widget build(BuildContext context) {
4545
return SearchAnchor(
46+
enableTapHandling: false,
4647
builder: (BuildContext context, SearchController controller) {
4748
return IconButton(
4849
icon: const Icon(Icons.search),

‎packages/material_ui/example/lib/search_anchor/search_anchor.4.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class _AsyncSearchAnchorState extends State<_AsyncSearchAnchor> {
7373
@override
7474
Widget build(BuildContext context) {
7575
return SearchAnchor(
76+
enableTapHandling: false,
7677
builder: (BuildContext context, SearchController controller) {
7778
return IconButton(
7879
icon: const Icon(Icons.search),

‎packages/material_ui/example/lib/search_anchor/search_bar.0.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class _SearchBarAppState extends State<SearchBarApp> {
3030
body: Padding(
3131
padding: const .all(8.0),
3232
child: SearchAnchor(
33+
enableTapHandling: false,
3334
builder: (BuildContext context, SearchController controller) {
3435
return SearchBar(
3536
controller: controller,

‎packages/material_ui/lib/src/search_anchor.dart‎

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,55 @@ typedef ViewBuilder = Widget Function(Iterable<Widget> suggestions);
8787
/// If [builder] returns an Icon, or any un-tappable widgets, we don't have
8888
/// to explicitly call [SearchController.openView].
8989
///
90+
/// ## Tap handling on the anchor
91+
///
92+
/// By default, [SearchAnchor] wraps the widget returned by [builder] in a
93+
/// [GestureDetector] that calls [SearchController.openView] on tap. This
94+
/// behavior is controlled by [enableTapHandling], which defaults to true.
95+
///
96+
/// ### When to enable tap handling
97+
///
98+
/// Keep [enableTapHandling] set to true (the default) when [builder]
99+
/// returns a passive widget, such as an [Icon] or [Text]. The outer
100+
/// [GestureDetector] handles taps and provides tap semantics for accessibility
101+
/// services.
102+
///
103+
/// ### When to disable tap handling
104+
///
105+
/// Set [enableTapHandling] to false when the widget returned by [builder]
106+
/// handles its own tap events or provides its own semantics. For example:
107+
///
108+
/// * When returning a [SearchBar] that handles taps through its own callbacks.
109+
/// * When returning buttons like [IconButton] or other interactive widgets that
110+
/// already call [SearchController.openView].
111+
///
112+
/// Setting [enableTapHandling] to false avoids duplicate gesture recognizers
113+
/// in the gesture arena and prevents duplicate tap semantics (which can cause
114+
/// accessibility guideline violations, such as unlabeled tap targets).
115+
///
116+
/// When using [SearchAnchor.bar], [enableTapHandling] is already set to
117+
/// false because the factory constructor's [SearchBar] handles its own taps.
118+
///
119+
/// ```dart
120+
/// SearchAnchor(
121+
/// enableTapHandling: false,
122+
/// builder: (BuildContext context, SearchController controller) {
123+
/// return SearchBar(
124+
/// controller: controller,
125+
/// onTap: () {
126+
/// controller.openView();
127+
/// },
128+
/// onChanged: (_) {
129+
/// controller.openView();
130+
/// },
131+
/// );
132+
/// },
133+
/// suggestionsBuilder: (BuildContext context, SearchController controller) {
134+
/// return const <Widget>[];
135+
/// },
136+
/// )
137+
/// ```
138+
///
90139
/// The search view route will be popped if the window size is changed and the
91140
/// search view route is not in full-screen mode. However, if the search view route
92141
/// is in full-screen mode, changing the window size, such as rotating a mobile
@@ -185,6 +234,7 @@ class SearchAnchor extends StatefulWidget {
185234
this.enabled = true,
186235
this.smartDashesType,
187236
this.smartQuotesType,
237+
this.enableTapHandling = true,
188238
});
189239

190240
/// Create a [SearchAnchor] that has a [SearchBar] which opens a search view.
@@ -476,6 +526,16 @@ class SearchAnchor extends StatefulWidget {
476526
/// configuration option on a standalone [TextField].
477527
final SmartQuotesType? smartQuotesType;
478528

529+
/// Whether to wrap the widget returned by [builder] with a [GestureDetector]
530+
/// that opens the search view route when tapped.
531+
///
532+
/// Defaults to true.
533+
///
534+
/// Set this to false if the widget returned by [builder] handles its own
535+
/// tap gestures (such as a [SearchBar] or [IconButton]) to avoid duplicate
536+
/// gestures and semantics actions.
537+
final bool enableTapHandling;
538+
479539
@override
480540
State<SearchAnchor> createState() => _SearchAnchorState();
481541
}
@@ -604,14 +664,15 @@ class _SearchAnchorState extends State<SearchAnchor> {
604664

605665
@override
606666
Widget build(BuildContext context) {
667+
Widget child = widget.builder(context, _searchController);
668+
if (widget.enableTapHandling) {
669+
child = GestureDetector(onTap: _openView, child: child);
670+
}
607671
return AnimatedOpacity(
608672
key: _anchorKey,
609673
opacity: _getOpacity(),
610674
duration: _kAnchorFadeDuration,
611-
child: IgnorePointer(
612-
ignoring: !widget.enabled,
613-
child: GestureDetector(onTap: _openView, child: widget.builder(context, _searchController)),
614-
),
675+
child: IgnorePointer(ignoring: !widget.enabled, child: child),
615676
);
616677
}
617678
}
@@ -1318,6 +1379,7 @@ class _SearchAnchorWithSearchBar extends SearchAnchor {
13181379
super.smartDashesType,
13191380
super.smartQuotesType,
13201381
}) : super(
1382+
enableTapHandling: false,
13211383
viewHintText: viewHintText ?? barHintText,
13221384
headerHeight: viewHeaderHeight,
13231385
headerTextStyle: viewHeaderTextStyle,
@@ -1829,6 +1891,11 @@ class _SearchBarState extends State<SearchBar> {
18291891
child: IgnorePointer(
18301892
ignoring: !widget.enabled,
18311893
child: InkWell(
1894+
// Avoid creating a redundant focus stop and focusable/tappable
1895+
// semantics node on the InkWell, since the inner TextField
1896+
// already handles focus and semantics for the SearchBar.
1897+
canRequestFocus: false,
1898+
excludeFromSemantics: true,
18321899
onTap: () {
18331900
widget.onTap?.call();
18341901
if (!_focusNode.hasFocus) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
changelog: |
2+
- Fixes unlabeled tap target semantics in `SearchBar`.
3+
- Adds `enableTapHandling` to `SearchAnchor`.
4+
version: minor

‎packages/material_ui/test/search_anchor_test.dart‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3407,6 +3407,138 @@ void main() {
34073407
semantics.dispose();
34083408
});
34093409

3410+
testWidgets('SearchBar meets labeledTapTargetGuideline', (WidgetTester tester) async {
3411+
final semantics = SemanticsTester(tester);
3412+
await tester.pumpWidget(
3413+
MaterialApp(
3414+
home: Material(
3415+
child: Center(
3416+
child: SearchBar(
3417+
hintText: 'Search...',
3418+
trailing: <Widget>[
3419+
IconButton(tooltip: 'Clear', icon: const Icon(Icons.clear), onPressed: () {}),
3420+
],
3421+
),
3422+
),
3423+
),
3424+
),
3425+
);
3426+
3427+
await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));
3428+
// The outer InkWell should not create an intermediate unlabeled focusable
3429+
// semantics node wrapping the TextField and trailing IconButton.
3430+
expect(
3431+
semantics,
3432+
isNot(
3433+
includesNodeWith(
3434+
actions: <SemanticsAction>[SemanticsAction.focus],
3435+
flags: <SemanticsFlag>[SemanticsFlag.isFocusable],
3436+
),
3437+
),
3438+
);
3439+
semantics.dispose();
3440+
});
3441+
3442+
testWidgets('SearchAnchor.bar meets labeledTapTargetGuideline', (WidgetTester tester) async {
3443+
final semantics = SemanticsTester(tester);
3444+
await tester.pumpWidget(
3445+
MaterialApp(
3446+
home: Scaffold(
3447+
body: SearchAnchor.bar(
3448+
barHintText: 'Search...',
3449+
barTrailing: <Widget>[
3450+
IconButton(tooltip: 'Clear', icon: const Icon(Icons.clear), onPressed: () {}),
3451+
],
3452+
suggestionsBuilder: (BuildContext context, SearchController controller) {
3453+
return <Widget>[];
3454+
},
3455+
),
3456+
),
3457+
),
3458+
);
3459+
3460+
await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));
3461+
semantics.dispose();
3462+
});
3463+
3464+
testWidgets('SearchAnchor with enableTapHandling: false meets labeledTapTargetGuideline', (
3465+
WidgetTester tester,
3466+
) async {
3467+
final semantics = SemanticsTester(tester);
3468+
await tester.pumpWidget(
3469+
MaterialApp(
3470+
home: Scaffold(
3471+
body: SearchAnchor(
3472+
enableTapHandling: false,
3473+
builder: (BuildContext context, SearchController controller) {
3474+
return SearchBar(
3475+
controller: controller,
3476+
hintText: 'Search...',
3477+
trailing: <Widget>[
3478+
IconButton(tooltip: 'Clear', icon: const Icon(Icons.clear), onPressed: () {}),
3479+
],
3480+
);
3481+
},
3482+
suggestionsBuilder: (BuildContext context, SearchController controller) {
3483+
return <Widget>[];
3484+
},
3485+
),
3486+
),
3487+
),
3488+
);
3489+
3490+
await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));
3491+
semantics.dispose();
3492+
});
3493+
3494+
testWidgets('SearchAnchor with enableTapHandling provides tap semantics for passive child', (
3495+
WidgetTester tester,
3496+
) async {
3497+
final semantics = SemanticsTester(tester);
3498+
await tester.pumpWidget(
3499+
MaterialApp(
3500+
home: Scaffold(
3501+
body: SearchAnchor(
3502+
builder: (BuildContext context, SearchController controller) {
3503+
return const Icon(Icons.search);
3504+
},
3505+
suggestionsBuilder: (BuildContext context, SearchController controller) {
3506+
return <Widget>[];
3507+
},
3508+
),
3509+
),
3510+
),
3511+
);
3512+
3513+
expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.tap]));
3514+
semantics.dispose();
3515+
});
3516+
3517+
testWidgets('SearchAnchor with enableTapHandling: false does not build GestureDetector', (
3518+
WidgetTester tester,
3519+
) async {
3520+
await tester.pumpWidget(
3521+
MaterialApp(
3522+
home: Scaffold(
3523+
body: SearchAnchor(
3524+
enableTapHandling: false,
3525+
builder: (BuildContext context, SearchController controller) {
3526+
return const Icon(Icons.search);
3527+
},
3528+
suggestionsBuilder: (BuildContext context, SearchController controller) {
3529+
return <Widget>[];
3530+
},
3531+
),
3532+
),
3533+
),
3534+
);
3535+
3536+
expect(
3537+
find.descendant(of: find.byType(SearchAnchor), matching: find.byType(GestureDetector)),
3538+
findsNothing,
3539+
);
3540+
});
3541+
34103542
testWidgets('Check SearchBar opacity when disabled', (WidgetTester tester) async {
34113543
await tester.pumpWidget(
34123544
const MaterialApp(

0 commit comments

Comments
 (0)