Skip to content

Commit 497b0b1

Browse files
authored
[material_ui] add a barFocusNode to SearchAnchor.bar (ref: flutter#186073) (flutter#12863)
Ports flutter#186073 to `material_ui`, as per flutter#188444. The old PR (and tagged as `Decoupling: Port to flutter/packages`) was reviewed and approved by @QuncCccccc and @justinmc. --- _quick recap_. Fixes flutter#177454. Adds a `barFocusNode` parameter to `SearchAnchor.bar` (and its internal `_SearchAnchorWithSearchBar` implementation), allowing external code to control the focus of the underlying `SearchBar`. --- ## Pre-Review Checklist
1 parent 7e1d1c6 commit 497b0b1

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ class SearchAnchor extends StatefulWidget {
242242
/// All the barX parameters are used to customize the anchor. Similarly, all the
243243
/// viewX parameters are used to override the view's defaults.
244244
///
245+
/// If `barFocusNode` is non-null, it is passed to the search bar as
246+
/// [SearchBar.focusNode]. The search bar does not dispose it, so the caller
247+
/// is responsible for calling [FocusNode.dispose] when it is no longer
248+
/// needed. If null, the search bar creates and manages its own focus node.
249+
/// The text field in the search view always uses its own focus node.
250+
///
245251
/// <callout-box>
246252
///
247253
/// This example shows how to use a [SearchAnchor.bar] which uses a default search
@@ -299,6 +305,7 @@ class SearchAnchor extends StatefulWidget {
299305
bool enabled,
300306
SmartDashesType? smartDashesType,
301307
SmartQuotesType? smartQuotesType,
308+
FocusNode? barFocusNode,
302309
}) = _SearchAnchorWithSearchBar;
303310

304311
/// Whether the search view grows to fill the entire screen when the
@@ -1378,6 +1385,7 @@ class _SearchAnchorWithSearchBar extends SearchAnchor {
13781385
super.enabled,
13791386
super.smartDashesType,
13801387
super.smartQuotesType,
1388+
FocusNode? barFocusNode,
13811389
}) : super(
13821390
enableTapHandling: false,
13831391
viewHintText: viewHintText ?? barHintText,
@@ -1392,6 +1400,7 @@ class _SearchAnchorWithSearchBar extends SearchAnchor {
13921400
return SearchBar(
13931401
constraints: constraints,
13941402
controller: controller,
1403+
focusNode: barFocusNode,
13951404
onTap: () {
13961405
controller.openView();
13971406
onTap?.call();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Adds a `barFocusNode` parameter to `SearchAnchor.bar` to allow external control over the focus of the underlying `SearchBar`.
3+
version: minor

‎packages/material_ui/test/search_anchor_test.dart‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,6 +3323,59 @@ void main() {
33233323
expect(textField.textInputAction, TextInputAction.previous);
33243324
});
33253325

3326+
testWidgets('SearchAnchor.bar respects barFocusNode property', (WidgetTester tester) async {
3327+
final focusNode = FocusNode();
3328+
addTearDown(focusNode.dispose);
3329+
3330+
await tester.pumpWidget(
3331+
MaterialApp(
3332+
home: Center(
3333+
child: Material(
3334+
child: SearchAnchor.bar(
3335+
barFocusNode: focusNode,
3336+
suggestionsBuilder: (context, controller) {
3337+
return <Widget>[];
3338+
},
3339+
),
3340+
),
3341+
),
3342+
),
3343+
);
3344+
3345+
final TextField textField = tester.widget(find.byType(TextField));
3346+
expect(textField.focusNode, focusNode);
3347+
});
3348+
3349+
testWidgets('SearchAnchor.bar barFocusNode can be used to control focus', (
3350+
WidgetTester tester,
3351+
) async {
3352+
final focusNode = FocusNode();
3353+
addTearDown(focusNode.dispose);
3354+
3355+
await tester.pumpWidget(
3356+
MaterialApp(
3357+
home: Center(
3358+
child: Material(
3359+
child: SearchAnchor.bar(
3360+
barFocusNode: focusNode,
3361+
suggestionsBuilder: (context, controller) {
3362+
return <Widget>[];
3363+
},
3364+
),
3365+
),
3366+
),
3367+
),
3368+
);
3369+
3370+
expect(focusNode.hasFocus, isFalse);
3371+
focusNode.requestFocus();
3372+
await tester.pump();
3373+
expect(focusNode.hasFocus, isTrue);
3374+
focusNode.unfocus();
3375+
await tester.pump();
3376+
expect(focusNode.hasFocus, isFalse);
3377+
});
3378+
33263379
testWidgets('Block entering text on disabled widget', (WidgetTester tester) async {
33273380
const initValue = 'init';
33283381
final controller = TextEditingController(text: initValue);

0 commit comments

Comments
 (0)