• Victor Ohashi's avatar
    fix: Search anchor box location when used on nested navigator (#127198) · 7e3f1df3
    Victor Ohashi authored
    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
    
    <details>
    <summary>Test case</summary>
    
    ```dart
    void main() => runApp(const NestedSearchBarApp());
    
    class NestedSearchBarApp extends StatefulWidget {
      const NestedSearchBarApp({super.key});
    
      @override
      State<NestedSearchBarApp> createState() => _NestedSearchBarAppState();
    }
    
    class _NestedSearchBarAppState extends State<NestedSearchBarApp> {
      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: <Widget>[
                  NavigationRail(
                    selectedIndex: 1,
                    destinations: const <NavigationRailDestination>[
                      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: <Widget>[
                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<ListTile>.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}'),
                ),
              ],
            ),
          ),
        );
      }
    }
    ```
    </details>
    
    <details>
    <summary>Before fix:</summary>
    
    ![Screenshot 2023-05-19 at 11 55 53](https://github.com/flutter/flutter/assets/38299943/c86747e5-6716-4e87-b3fd-ce7f0f943b92)
    </details>
    
    <details>
    <summary>After fix:</summary>
    
    ![Screenshot 2023-05-19 at 11 53 30](https://github.com/flutter/flutter/assets/38299943/d790ee49-e047-485c-87f4-7254acbdddfa)
    </details>
    7e3f1df3
Name
Last commit
Last update
..
lib Loading commit data...
test Loading commit data...
test_fixes Loading commit data...
test_private Loading commit data...
test_profile Loading commit data...
test_release Loading commit data...
LICENSE Loading commit data...
README.md Loading commit data...
analysis_options.yaml Loading commit data...
build.yaml Loading commit data...
dart_test.yaml Loading commit data...
pubspec.yaml Loading commit data...