是否可以在颤振中使用ListView搜索栏?

是否可以在颤振中使用ListView搜索栏?,list,flutter,searchbar,List,Flutter,Searchbar,我想在颤振应用程序中实现一个搜索栏。我必须浏览ListTiles中的listview。这里我想检查listtile的标题是否包含搜索字段中的字母。这可以通过列表实现吗? 它不必与标题一起。可能是我能识别瓷砖的其他东西。但是请不要用索引,用户不会知道的。 列表是正确的小部件还是我必须使用其他工具在我的应用程序中实现搜索引擎 列表数据=[ SearchItem(0,'此'), 搜索项(1,'是'), 搜索项(2,“a”), 搜索项(3,“测试”), 搜索项(4“,”), ]; 搜索面板( 填充:所有

我想在颤振应用程序中实现一个搜索栏。我必须浏览ListTiles中的listview。这里我想检查listtile的标题是否包含搜索字段中的字母。这可以通过列表实现吗? 它不必与标题一起。可能是我能识别瓷砖的其他东西。但是请不要用索引,用户不会知道的。 列表是正确的小部件还是我必须使用其他工具在我的应用程序中实现搜索引擎

列表数据=[
SearchItem(0,'此'),
搜索项(1,'是'),
搜索项(2,“a”),
搜索项(3,“测试”),
搜索项(4“,”),
];
搜索面板(
填充:所有边缘设置(10.0),
选定:3,
标题:“演示搜索页面”,
数据:数据,
图标:新图标(图标。勾选圆圈,颜色:颜色。白色),
颜色:颜色,蓝色,
textStyle:新的textStyle(颜色:Colors.white,fontwweight:fontwweight.bold,fontSize:20.0,装饰样式:TextDecorationStyle.dottered),
onChanged:(int值){
印刷品(价值);
},
),
试试看

列表数据=[
SearchItem(0,'此'),
搜索项(1,'是'),
搜索项(2,“a”),
搜索项(3,“测试”),
搜索项(4“,”),
];
搜索面板(
填充:所有边缘设置(10.0),
选定:3,
标题:“演示搜索页面”,
数据:数据,
图标:新图标(图标。勾选圆圈,颜色:颜色。白色),
颜色:颜色,蓝色,
textStyle:新的textStyle(颜色:Colors.white,fontwweight:fontwweight.bold,fontSize:20.0,装饰样式:TextDecorationStyle.dottered),
onChanged:(int值){
印刷品(价值);
},
),

您可以使用本机功能,而不是使用第三方软件包:

showSearch(context: context, delegate: ListSearchDelegate());
然后是一个类扩展:

类ListSearchDelegate扩展了SearchDelegate{
ListSearchDelegate({Key,}):super();
列表项=['1'、'2'、'3'、'4'、'5'];
@凌驾
列出buildActions(BuildContext上下文){
返回[
图标按钮(
图标:图标(图标。清除),
已按下:(){
查询=“”;
},
),
];
}
@凌驾
小部件buildLeading(BuildContext上下文){
返回图标按钮(
图标:图标(图标。箭头返回),
已按下:(){
关闭(上下文,空);
},
);
}
@凌驾
小部件构建结果(构建上下文){
列表子列表;
子列表=查询!=''?列表项。其中((项)=>item.contains(查询)).toList():
清单项目;
返回ListView.builder(
itemCount:subList.length,
itemBuilder:(上下文,索引){
返回列表块(
标题:文本(子列表[索引]),
);
}
);
}
@凌驾
小部件构建建议(构建上下文){
返回容器();
}
}

您可以使用本机功能,而不是使用第三方软件包:

showSearch(context: context, delegate: ListSearchDelegate());
然后是一个类扩展:

类ListSearchDelegate扩展了SearchDelegate{
ListSearchDelegate({Key,}):super();
列表项=['1'、'2'、'3'、'4'、'5'];
@凌驾
列出buildActions(BuildContext上下文){
返回[
图标按钮(
图标:图标(图标。清除),
已按下:(){
查询=“”;
},
),
];
}
@凌驾
小部件buildLeading(BuildContext上下文){
返回图标按钮(
图标:图标(图标。箭头返回),
已按下:(){
关闭(上下文,空);
},
);
}
@凌驾
小部件构建结果(构建上下文){
列表子列表;
子列表=查询!=''?列表项。其中((项)=>item.contains(查询)).toList():
清单项目;
返回ListView.builder(
itemCount:subList.length,
itemBuilder:(上下文,索引){
返回列表块(
标题:文本(子列表[索引]),
);
}
);
}
@凌驾
小部件构建建议(构建上下文){
返回容器();
}
}
class ListSearchDelegate extends SearchDelegate{

  ListSearchDelegate({Key key,}): super() ;

  List<String> listItems = <String>['One', 'Two', 'Three', 'Four', 'Five'] ;

  @override
  List<Widget> buildActions(BuildContext context) {

    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () {
          query = '';
        },
      ),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: Icon(Icons.arrow_back),
      onPressed: () {
        close(context, null);
      },
    );
  }

  @override
  Widget buildResults(BuildContext context) {

    List<String> subList ;

    subList = query != '' ? listItems.where((item) => item.contains(query)).toList() : 
      listItems ;

    return ListView.builder(
        itemCount: subList.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(subList[index]),
          );
        }
    );
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return Container();
  }

}