基本颤动搜索栏的实现失败

Implementation of basic flutter search bar failed

所以我遵循了有关如何使用搜索委派实现基本的 Flutter 搜索栏的教程。您可以在 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,);

    }
  }

然而,什么对我不起作用:

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

对于 buildActionsbuildLeadingbuilduggestionsbuildResults Widgets

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

里面 buildSuggestions:

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

里面 buildLeading:

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

请帮忙

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,);

 }
}