flutterdartuisearchbardelegate

Implementation of basic flutter search bar failed


So I followed a tutorial on how to implement a basic Flutter search bar with search Delegation. You can find the tutorial on this link: https://www.youtube.com/watch?v=FPcl1tu0gDs

class DataSearch extends SearchDelegate<String>{


    final wordssuggest=["Word1","Word2"];

    final recentwords=["Word1"];


    @override 
    List<Widget> buildActions(BuildContext context){
      return [
        IconButton(onPressed: (){
          query=" ";


        }, icon: Icon(Icons.clear))
      ];
        //actions for appbar
    }

    @override 
    Widget buildLeading(BuildContext context){
      


      return IconButton(onPressed: (){
        close(context, null);
      }, icon: Icon(Icons.search));
      //leasding icon on the left of the app bar
    }

    @override 
    Widget buildResults(BuildContext context){
      //show some result
      return Container(
        color: Colors.grey,
        height: 200,
        width: 200,
        child: Center(child: Text(query),)
      );

    }

    @override 
    Widget buildSuggestions(BuildContext context){
      //show suggestions
      final suggestionList =query.isEmpty?
      recentwords:wordssuggest.where((p)=>p.startsWith(query)).toList();
      return ListView.builder(itemBuilder: (context, index)=>ListTile(
        onTap:(){
          showResults(context);
        } ,
        leading: Icon(Icons.work_rounded),
        title: RichText(text: TextSpan(text: suggestionList[index].substring(0, query.length), 
        style: TextStyle(color:Colors.blue, fontWeight: FontWeight.bold),
        children: [TextSpan(
          text:suggestionList[index].substring(query.length),
          style:TextStyle(color:Colors.grey)
        )]),
        )
        
      ),
      itemCount: suggestionList.length,);

    }
  }

However, what is not working for me:

'Methods must have an explicit list of parameters.Try adding a parameter list.dart(missing_method_parameters)'

For buildActions, buildLeading, builduggestions and buildResults Widgets:

'The declaration 'buildActions' isn't referenced.'

Inside buildSuggestions:

The method 'showResults' isn't defined for the type '_MainPageState'.

Inside buildLeading:

The method 'close' isn't defined for the type '_MainPageState'.

Please help


Solution

  • Maybe your problem is occur because of calling the search delegate class. this code solve your problem!

    import 'package:flutter/material.dart';
    
      void main() {
      runApp(MyApp());
      }
    
     class MyApp extends StatelessWidget {
    
      @override
     Widget build(BuildContext context) {
      return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Secondpage(title: 'Flutter Demo Home Page'),
       );
      }
    }
      class Secondpage extends StatelessWidget {
     final String title;
    
     Secondpage({required this.title});
    
    @override
     Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(title),
    
          actions: [
            IconButton(icon: Icon(Icons.add), onPressed: () async {
              await showSearch<String>(
                context: context,
                delegate: DataSearch(),
              );
            },
    
            )
    
    
          ],
        ));
    
     }
    }
    

    Search Delegate

        class DataSearch extends SearchDelegate<String>{
        
         final wordSuggest=["Word1","Word2","Word3","Word4", "Word5","Word6",  ];
         final recentWords=["Word1"];
    
       @override
       List<Widget> buildActions(BuildContext context) {
       return [
         IconButton(
          icon: Icon(Icons.clear),
          onPressed: () {
            query = "";
          //  showSuggestions(context);
          },
        ),
      ];
     }
    
      @override
     Widget buildLeading(BuildContext context) {
      return IconButton(
          onPressed: () {
            close(context, 'null');
          },
          icon: AnimatedIcon(
          icon: AnimatedIcons.menu_arrow,
          progress: transitionAnimation,
          ),
         );
      }
    
        @override
       Widget buildResults(BuildContext context){
    //show some result
      return Container(
        color: Colors.grey,
        height: 200,
        width: 200,
        child: Center(child: Text(query),)
       );
    
     }
    
       @override
      Widget buildSuggestions(BuildContext context){
       //show suggestions
          final suggestionList =query.isEmpty?
          recentWords:wordSuggest.where((p)=>p.startsWith(query)).toList();
          return ListView.builder(itemBuilder: (context, index)=>ListTile(
            onTap:(){
              showResults(context);
             } ,
            leading: Icon(Icons.work_rounded),
             title: RichText(text: TextSpan(text: suggestionList[index].substring(0,   query.length),
            style: TextStyle(color:Colors.blue, fontWeight: FontWeight.bold),
            children: [TextSpan(
                text:suggestionList[index].substring(query.length),
                style:TextStyle(color:Colors.grey)
            )]),
        )
    
    ),
      itemCount: suggestionList.length,);
    
     }
    }