@@ -67,6 +67,11 @@ typedef ViewBuilder = Widget Function(Iterable<Widget> suggestions);
6767/// If [builder] returns an Icon, or any un-tappable widgets, we don't have
6868/// to explicitly call [SearchController.openView] .
6969///
70+ /// The search view route will be popped if the window size is changed and the
71+ /// search view route is not in full-screen mode. However, if the search view route
72+ /// is in full-screen mode, changing the window size, such as rotating a mobile
73+ /// device from portrait mode to landscape mode, will not close the search view.
74+ ///
7075/// {@tool dartpad}
7176/// This example shows how to use an IconButton to open a search view in a [SearchAnchor] .
7277/// It also shows how to use [SearchController] to open or close the search view route.
@@ -286,6 +291,7 @@ class SearchAnchor extends StatefulWidget {
286291}
287292
288293class _SearchAnchorState extends State <SearchAnchor > {
294+ Size ? _screenSize;
289295 bool _anchorIsVisible = true ;
290296 final GlobalKey _anchorKey = GlobalKey ();
291297 bool get _viewIsOpen => ! _anchorIsVisible;
@@ -301,6 +307,18 @@ class _SearchAnchorState extends State<SearchAnchor> {
301307 _searchController._attach (this );
302308 }
303309
310+ @override
311+ void didChangeDependencies () {
312+ super .didChangeDependencies ();
313+ final Size updatedScreenSize = MediaQuery .of (context).size;
314+ if (_screenSize != null && _screenSize != updatedScreenSize) {
315+ if (_searchController.isOpen && ! getShowFullScreenView ()) {
316+ _closeView (null );
317+ }
318+ }
319+ _screenSize = updatedScreenSize;
320+ }
321+
304322 @override
305323 void dispose () {
306324 super .dispose ();
@@ -672,45 +690,9 @@ class _ViewContentState extends State<_ViewContent> {
672690 result = widget.suggestionsBuilder (context, _controller);
673691 final Size updatedScreenSize = MediaQuery .of (context).size;
674692
675- if (_screenSize != updatedScreenSize) {
693+ if (_screenSize != updatedScreenSize && widget.showFullScreenView ) {
676694 _screenSize = updatedScreenSize;
677- setState (() {
678- final Rect anchorRect = widget.getRect () ?? _viewRect;
679- final BoxConstraints constraints = widget.viewConstraints ?? widget.viewTheme.constraints ?? widget.viewDefaults.constraints! ;
680- final double viewWidth = clampDouble (anchorRect.width, constraints.minWidth, constraints.maxWidth);
681- final double viewHeight = clampDouble (_screenSize! .height * 2 / 3 , constraints.minHeight, constraints.maxHeight);
682- final Size updatedViewSize = Size (viewWidth, viewHeight);
683-
684- switch (Directionality .of (context)) {
685- case TextDirection .ltr:
686- final double viewLeftToScreenRight = _screenSize! .width - anchorRect.left;
687- final double viewTopToScreenBottom = _screenSize! .height - anchorRect.top;
688-
689- // Make sure the search view doesn't go off the screen when the screen
690- // size is changed. If the search view doesn't fit, move the top-left
691- // corner of the view to fit the window. If the window is smaller than
692- // the view, then we resize the view to fit the window.
693- Offset topLeft = anchorRect.topLeft;
694- if (viewLeftToScreenRight < viewWidth) {
695- topLeft = Offset (_screenSize! .width - math.min (viewWidth, _screenSize! .width), anchorRect.top);
696- }
697- if (viewTopToScreenBottom < viewHeight) {
698- topLeft = Offset (topLeft.dx, _screenSize! .height - math.min (viewHeight, _screenSize! .height));
699- }
700- _viewRect = topLeft & updatedViewSize;
701- return ;
702- case TextDirection .rtl:
703- final double viewTopToScreenBottom = _screenSize! .height - anchorRect.top;
704- Offset topLeft = Offset (
705- math.max (anchorRect.right - viewWidth, 0.0 ),
706- anchorRect.top,
707- );
708- if (viewTopToScreenBottom < viewHeight) {
709- topLeft = Offset (topLeft.dx, _screenSize! .height - math.min (viewHeight, _screenSize! .height));
710- }
711- _viewRect = topLeft & updatedViewSize;
712- }
713- });
695+ _viewRect = Offset .zero & _screenSize! ;
714696 }
715697 }
716698
0 commit comments