ListView在搜索栏上键入时重复的数据

时间:2020-03-08 07:34:10

标签: listview flutter search

我的Listview在搜索栏上搜索时复制了数据,几天之内我都找不到问题。这是我的完整代码。

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:atrack/commonimplentations/groupmembers.dart';
import 'package:atrack/commonimplentations/membersdetails.dart';

//HERE IS THE MAIN CLASS TO DISPLAY DATA FROM A FIRESTORE
class SearchMembers extends StatelessWidget {

  final groupName;
  List<GroupMembers> list=List();

  SearchMembers({this.groupName });

  @override
  Widget build(BuildContext context) {

    final groupUsers = Provider.of<List<Members>>(context);
    groupUsers.forEach((groupUser) {

      list.add(GroupMembers(groupUser.username,groupName));

    });

    return Scaffold(
      appBar: new AppBar(
        title:Text('Search Member'),
        backgroundColor:Colors.redAccent,
        actions:<Widget>[
          IconButton(
            icon:Icon(Icons.search),onPressed:(){
              showSearch(context: context, delegate: MembersData(membersList:list,GroupName: groupName));

          },
          )

        ]
      ),

      body: new ListView(
        padding:EdgeInsets.symmetric(vertical:8.0),
        children:_buildList()
      ),

    );

  }

  List<GroupMembers> _buildList() {
    return list;
  }

  }

  class MembersData extends SearchDelegate<String>{

  List membersList;
  final String GroupName;

  MembersData({this.membersList,this.GroupName});

  List<GroupMembers> returnList(){
    return membersList;
  }

  @override
  List<Widget> buildActions(BuildContext context) {
    // ACTIONS ON THE APPBAR
    return[
      IconButton(
        icon:Icon(Icons.clear),onPressed:(){
          query="";
        }
      )
    ];

  }

  @override
  Widget buildLeading(BuildContext context) {
    // LEADING ICON OF THE APPBAR
    return IconButton(
      icon:AnimatedIcon(
        icon:AnimatedIcons.menu_arrow,
        progress:transitionAnimation,
      ),onPressed:(){
        close(context, null);
    },
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    // SHOW RESULTS

  }

  @override
  Widget buildSuggestions(BuildContext context) {
    // WHEN USER SEARCHES SOMETHING

    List<GroupMembers> buildSearchList(){

      List<GroupMembers> searchList=List();

      if(query.isEmpty){

        return membersList;
      }
      else{
        membersList.forEach((groupmember){

          if(groupmember.name.contains(query.toUpperCase())){

            searchList.add(groupmember);
          }

        });

        return searchList.toSet().toList();

      }

    }

     return ListView(
       padding:EdgeInsets.symmetric(vertical:8.0),
       children:buildSearchList()

     );
  }
}

在第一个Build上,它完美地在列表视图上显示数据,但是在搜索时它会复制数据,每次我开始在搜索栏上搜索数据时,都会在显示的列表视图上复制数据,因此对您的帮助将不胜感激。

0 个答案:

没有答案