From 7e3f1df36c55a533d6229f5af3b13eccba0ef660 Mon Sep 17 00:00:00 2001
From: Victor Ohashi <38299943+VictorOhashi@users.noreply.github.com>
Date: Mon, 22 May 2023 17:21:26 -0300
Subject: [PATCH] fix: Search anchor box location when used on nested navigator
(#127198)
This PR is to fix the position of `SearchAnchor` when used with nested navigator. This solution is based on what `DropdownButton` internally do, looking to the closest `Navigator` to proper calculate the where to render `SearchViewRoute`.
Fixes: https://github.com/flutter/flutter/issues/126435
Test case
```dart
void main() => runApp(const NestedSearchBarApp());
class NestedSearchBarApp extends StatefulWidget {
const NestedSearchBarApp({super.key});
@override
State createState() => _NestedSearchBarAppState();
}
class _NestedSearchBarAppState extends State {
final SearchController controller = SearchController();
@override
Widget build(BuildContext context) {
final ThemeData themeData = ThemeData(useMaterial3: true);
return MaterialApp(
theme: themeData,
builder: (BuildContext context, Widget? child) {
return Scaffold(
body: Row(
children: [
NavigationRail(
selectedIndex: 1,
destinations: const [
NavigationRailDestination(
icon: Icon(Icons.favorite_border),
selectedIcon: Icon(Icons.favorite),
label: Text('First'),
),
NavigationRailDestination(
icon: Icon(Icons.bookmark_border),
selectedIcon: Icon(Icons.book),
label: Text('Second'),
),
],
),
const VerticalDivider(thickness: 1, width: 1),
Expanded(child: child!)
],
),
);
},
home: Scaffold(
appBar: AppBar(title: const Text('Search Anchor Sample')),
body: Column(
children: [
SearchAnchor(
searchController: controller,
builder: (BuildContext context, SearchController controller) {
return IconButton(
icon: const Icon(Icons.search),
onPressed: () {
controller.openView();
},
);
},
suggestionsBuilder:
(BuildContext context, SearchController controller) {
return List.generate(5, (int index) {
final String item = 'item $index';
return ListTile(
title: Text(item),
onTap: () {
setState(() {
controller.closeView(item);
});
},
);
});
}),
Center(
child: controller.text.isEmpty
? const Text('No item selected')
: Text('Selected item: ${controller.value.text}'),
),
],
),
),
);
}
}
```
Before fix:

After fix:

---
.../lib/src/material/search_anchor.dart | 14 +------
.../test/material/search_anchor_test.dart | 37 +++++++++++++++++++
2 files changed, 39 insertions(+), 12 deletions(-)
diff --git a/packages/flutter/lib/src/material/search_anchor.dart b/packages/flutter/lib/src/material/search_anchor.dart
index c2a5c87244..74ace69e66 100644
--- a/packages/flutter/lib/src/material/search_anchor.dart
+++ b/packages/flutter/lib/src/material/search_anchor.dart
@@ -357,17 +357,6 @@ class _SearchAnchorState extends State {
Navigator.of(context).pop();
}
- Rect? getRect(GlobalKey key) {
- final BuildContext? context = key.currentContext;
- if (context != null) {
- final RenderBox searchBarBox = context.findRenderObject()! as RenderBox;
- final Size boxSize = searchBarBox.size;
- final Offset boxLocation = searchBarBox.localToGlobal(Offset.zero);
- return boxLocation & boxSize;
- }
- return null;
- }
-
bool toggleVisibility() {
setState(() {
_anchorIsVisible = !_anchorIsVisible;
@@ -468,7 +457,8 @@ class _SearchViewRoute extends PopupRoute<_SearchViewRoute> {
if (context != null) {
final RenderBox searchBarBox = context.findRenderObject()! as RenderBox;
final Size boxSize = searchBarBox.size;
- final Offset boxLocation = searchBarBox.localToGlobal(Offset.zero);
+ final NavigatorState navigator = Navigator.of(context);
+ final Offset boxLocation = searchBarBox.localToGlobal(Offset.zero, ancestor: navigator.context.findRenderObject());
return boxLocation & boxSize;
}
return null;
diff --git a/packages/flutter/test/material/search_anchor_test.dart b/packages/flutter/test/material/search_anchor_test.dart
index 94ab2e616d..b6ff5b02ce 100644
--- a/packages/flutter/test/material/search_anchor_test.dart
+++ b/packages/flutter/test/material/search_anchor_test.dart
@@ -1679,6 +1679,43 @@ void main() {
// No exception.
});
+
+ testWidgets('Docked search should position itself correctly based on closest navigator', (WidgetTester tester) async {
+ const double rootSpacing = 100.0;
+
+ await tester.pumpWidget(MaterialApp(
+ builder: (BuildContext context, Widget? child) {
+ return Scaffold(
+ body: Padding(
+ padding: const EdgeInsets.all(rootSpacing),
+ child: child,
+ ),
+ );
+ },
+ home: Material(
+ child: SearchAnchor(
+ isFullScreen: false,
+ builder: (BuildContext context, SearchController controller) {
+ return IconButton(
+ icon: const Icon(Icons.search),
+ onPressed: () {
+ controller.openView();
+ },
+ );
+ },
+ suggestionsBuilder: (BuildContext context, SearchController controller) {
+ return [];
+ },
+ ),
+ ),
+ ));
+
+ await tester.tap(find.byIcon(Icons.search));
+ await tester.pumpAndSettle();
+
+ final Rect searchViewRect = tester.getRect(find.descendant(of: findViewContent(), matching: find.byType(SizedBox)).first);
+ expect(searchViewRect.topLeft, equals(const Offset(rootSpacing, rootSpacing)));
+ });
}
TextStyle? _iconStyle(WidgetTester tester, IconData icon) {