From 39bf968168f29a0728edd45c4ab374d04ff6b353 Mon Sep 17 00:00:00 2001 From: Victor Ohashi <38299943+VictorOhashi@users.noreply.github.com> Date: Thu, 22 Jun 2023 18:20:41 -0300 Subject: [PATCH] fix: SearchAnchor View not resizing when in nested navigator (#128357) Similar to what was done on https://github.com/flutter/flutter/pull/127198, look for the closest navigator instead of screen size. Fixes: https://github.com/flutter/flutter/issues/128344 --- .../lib/src/material/search_anchor.dart | 3 +- .../test/material/search_anchor_test.dart | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/search_anchor.dart b/packages/flutter/lib/src/material/search_anchor.dart index ed3b347ac1..e9b3037ffc 100644 --- a/packages/flutter/lib/src/material/search_anchor.dart +++ b/packages/flutter/lib/src/material/search_anchor.dart @@ -489,7 +489,8 @@ class _SearchViewRoute extends PopupRoute<_SearchViewRoute> { } void updateTweens(BuildContext context) { - final Size screenSize = MediaQuery.of(context).size; + final RenderBox navigator = Navigator.of(context).context.findRenderObject()! as RenderBox; + final Size screenSize = navigator.size; final Rect anchorRect = getRect() ?? Rect.zero; final BoxConstraints effectiveConstraints = viewConstraints ?? viewTheme.constraints ?? viewDefaults.constraints!; diff --git a/packages/flutter/test/material/search_anchor_test.dart b/packages/flutter/test/material/search_anchor_test.dart index b86f69a675..d30cd8e0a6 100644 --- a/packages/flutter/test/material/search_anchor_test.dart +++ b/packages/flutter/test/material/search_anchor_test.dart @@ -1789,6 +1789,50 @@ void main() { expect(searchViewRect.topLeft, equals(const Offset(rootSpacing, rootSpacing))); }); + testWidgets('Docked search view with nested navigator does not go off the screen', (WidgetTester tester) async { + addTearDown(tester.view.reset); + tester.view.physicalSize = const Size(400.0, 400.0); + tester.view.devicePixelRatio = 1.0; + + 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: Align( + alignment: Alignment.bottomRight, + 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.bottomRight, equals(const Offset(300.0, 300.0))); + }); + // Regression tests for https://github.com/flutter/flutter/issues/126623 group('Overall InputDecorationTheme does not impact SearchBar and SearchView', () {