• Taha Tesser's avatar
    Fix `SearchAnchor` triggers unnecessary suggestions builder calls (#143443) · 8363e782
    Taha Tesser authored
    fixes [`SearchAnchor` triggers extra search operations](https://github.com/flutter/flutter/issues/139880)
    
    ### Code sample
    
    <details>
    <summary>expand to view the code sample</summary> 
    
    ```dart
    import 'package:flutter/material.dart';
    
    Future<List<String>> createFuture() async {
      return List.generate(1000, (index) => "Hello World!");
    }
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          title: 'Flutter Demo',
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
    
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      final SearchController controller = SearchController();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                SearchAnchor(
                  searchController: controller,
                  suggestionsBuilder: (suggestionsContext, controller) {
                    final resultFuture = createFuture();
                    return [
                      FutureBuilder(
                        future: resultFuture,
                        builder: ((context, snapshot) {
                          if (snapshot.connectionState != ConnectionState.done) {
                            return const LinearProgressIndicator();
                          }
                          final result = snapshot.data;
                          if (result == null) {
                            return const LinearProgressIndicator();
                          }
                          return ListView.builder(
                            shrinkWrap: true,
                            physics: const NeverScrollableScrollPhysics(),
                            itemCount: result.length,
                            itemBuilder: (BuildContext context, int index) {
                              final root = result[index];
                              return ListTile(
                                leading: const Icon(Icons.article),
                                title: Text(root),
                                subtitle: Text(
                                  root,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: Theme.of(suggestionsContext)
                                        .colorScheme
                                        .onSurfaceVariant,
                                  ),
                                ),
                                onTap: () {},
                              );
                            },
                          );
                        }),
                      ),
                    ];
                  },
                  builder: (BuildContext context, SearchController controller) {
                    return IconButton(
                      onPressed: () {
                        controller.openView();
                      },
                      icon: const Icon(Icons.search),
                    );
                  },
                ),
              ],
            ),
          ),
        );
      }
    }
    
    ```
    
    </details>
    
    ### Before
    
    https://github.com/flutter/flutter/assets/48603081/69f6dfdc-9f92-4d2e-8a3e-984fce25f9e4
    
    ### After
    
    https://github.com/flutter/flutter/assets/48603081/be105e2c-51d8-4cb0-a75b-f5f41d948e5e
    8363e782
Name
Last commit
Last update
.github Loading commit data...
.vscode Loading commit data...
bin Loading commit data...
dev Loading commit data...
examples Loading commit data...
packages Loading commit data...
.ci.yaml Loading commit data...
.gitattributes Loading commit data...
.gitignore Loading commit data...
AUTHORS Loading commit data...
CODEOWNERS Loading commit data...
CODE_OF_CONDUCT.md Loading commit data...
CONTRIBUTING.md Loading commit data...
LICENSE Loading commit data...
PATENT_GRANT Loading commit data...
README.md Loading commit data...
TESTOWNERS Loading commit data...
analysis_options.yaml Loading commit data...
dartdoc_options.yaml Loading commit data...
flutter_console.bat Loading commit data...