flutter 搜索功能未过滤回原始数据

vwkv1x7d  于 2023-02-05  发布在  Flutter
关注(0)|答案(2)|浏览(132)

如果我们在搜索栏中搜索任何文本...我们会得到过滤后的搜索结果...但问题是所有其他待办事项都消失了...

我不能回到所有其他托多斯。
搜索功能码

List<Todos> todList =[];
searchTodo(String enterdText) {
    final search = todList.where((txt) {
      final inputtext = txt.todoText.toLowerCase();
      final input = enterdText.toLowerCase();
      return inputtext.contains(input);
    }).toList();
    setState(() => {todList = search});
  }

搜索栏

TextField(
              onChanged: searchTodo,
              decoration: InputDecoration(
                  hintText: 'Search',
                  prefixIcon: Icon(Icons.search_rounded),
                  //  enabledBorder: InputBorder.none,
                  iconColor: Colors.grey.shade400,
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(20))),
            ),
x0fgdtte

x0fgdtte1#

将列表保存到其他变量,搜索结束后,用保存的列表替换。

ql3eal8s

ql3eal8s2#

Something like this... little bit confuse on list size if not replace with length

List<Todos> todList =[];
List<Todos> tmpList =[];
searchTodo(String enterdText){
If(tmpList.size<todList.size){

tmpList=todList;
}
if(enterdText.trim().isEmpty()){
final search=tmpList;

}else{
final search = todList.where((txt) {
  final inputtext = txt.todoText.toLowerCase();
  final input = enterdText.toLowerCase();
  return inputtext.contains(input);
}).toList();}
setState(() => {
If(tmpList.size<todList.size){

tmpList=todList;
}

todList = search});
}

相关问题