更改搜索栏状态后如何更改ListView.builder?

时间:2019-01-03 12:45:24

标签: list dart flutter state

我正在快速建立联系人列表,我想在搜索栏中键入字母时更新列表,以仅显示以该字母开头的联系人。 一切正常,在调试中更新了联系人列表,我只是无法更改列表视图以显示更新后的列表。 如果有人可以帮助我将不胜感激。谢谢。

 class ContactsScreen extends StatefulWidget{
    @override
    ContactsScreenState createState() {
      return new ContactsScreenState();
    }
  }

  class ContactsScreenState extends State<ContactsScreen> {

    Icon _searchIcon = new Icon(Icons.search);

    Widget _appBarTitle = new Text("Search Contact");

    List filteredNames = new List();


  Widget build(context) {

    final FeedBloc bloc = AppProvider.of(context).feedBloc;

    bloc.fetchFeeds();


    return Scaffold(
      appBar: AppBar(
        title: _appBarTitle,
        actions: <Widget>[
          new IconButton(
              icon: _searchIcon,
              onPressed:(){
                  setState(() {
                    if (_searchIcon.icon == Icons.search) {
                      _searchIcon = new Icon(Icons.close);
                      _appBarTitle = new TextField(
                        style: new TextStyle(
                          color: Colors.white,
                        ),
                        decoration: new InputDecoration(
                            prefixIcon:
                            new Icon(Icons.search, color: Colors.white),
                            hintText: "Search...",
                            hintStyle: new TextStyle(color: Colors.white)),
                        onChanged: (value) {
                          for(int i = 0; i < filteredNames.length; i++)
                          {
                            if(filteredNames[i].name.toLowerCase().contains(value.toLowerCase()))
                            {

                              print("aqui2");

                              print(filteredNames[i].name);

                            }
                          }
                          //filter your contact list based on value
                        },
                      );
                    }
                  }
                  );
              }
          )
        ],

      ),
      body: StreamBuilder(
          stream: bloc.feeds,
          builder:(context, AsyncSnapshot<List<FeedModel>>feeds){

            filteredNames = feeds.data;

            return ListView.builder(
              itemCount: filteredNames.length,
              itemBuilder: (context, int index)
                {
                  print("again");

                  return ViewListsHelper.contactBox(
                      filteredNames[index].image,
                      filteredNames[index].name
                  );
                }
            );


          }
      ),
    );
  }

}

0 个答案:

没有答案