• 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
..
animation Loading commit data...
cupertino Loading commit data...
dart Loading commit data...
examples Loading commit data...
foundation Loading commit data...
gestures Loading commit data...
harness Loading commit data...
material Loading commit data...
painting Loading commit data...
physics Loading commit data...
rendering Loading commit data...
scheduler Loading commit data...
semantics Loading commit data...
services Loading commit data...
widgets Loading commit data...
_goldens_io.dart Loading commit data...
_goldens_web.dart Loading commit data...
analysis_options.yaml Loading commit data...
flutter_test_config.dart Loading commit data...
image_data.dart Loading commit data...
impeller_test_helpers.dart Loading commit data...