仅当我单击“搜索”表单或热重新加载时,才会填充ListView

时间:2018-05-30 09:11:01

标签: dart flutter

class _SearchState extends State<Search> {
  List  _searchResult = [];


  TextEditingController controller = new TextEditingController();


  var userDetails  = {};
  List returnTicketDetails;

  @override
  Widget build(BuildContext context) {

    body: new Column(
        children: <Widget>[
          new Container(

            color: Theme.of(context).primaryColor,

            child: new Padding(

              padding: const EdgeInsets.all(8.0),
              child: new Card(
                child: new ListTile(
                  leading: new Icon(Icons.search),
                  title: new TextField(
                    controller: controller,
                    decoration: new InputDecoration(
                        hintText: 'Search', border: InputBorder.none),
                    onChanged: onSearchTextChanged,
                  ),
                  trailing: new IconButton(icon: new Icon(Icons.cancel), onPressed: () {
                    controller.clear();
                    onSearchTextChanged('');
                  },),
                ),
              ),
            ),
          ),
          new Expanded(

            child: _searchResult.length != 0 || controller.text.isNotEmpty ?
            new ListView.builder(

               itemCount: _searchResult.length,
              itemBuilder: (context, i) {

                return new Card(

                  child: new Column
                    (mainAxisSize: MainAxisSize.min, children:
                    <Widget>[
                    new Row(children: <Widget>[
                    new Container(
                    width: 80.0,
                    height: 80.0,
                    decoration: new BoxDecoration(

                    shape: BoxShape.circle,
                    image: new DecorationImage(
                    fit: BoxFit.fill,
                    image: new NetworkImage(

                    "https:.....")
                )
                )),
                    new Text(" " + userDetails[returnTicketDetails[i]["user_id"]]["first_name"] +  " " + (userDetails[returnTicketDetails[i]["user_id"]]["last_name"]),


                ])
                    ])
                );
                },
            )
                : new ListView.builder(
               itemCount: _searchResult.length,
              itemBuilder: (context, i) {
                return new Card(
                  child: new ListTile(
                                     ),
                  margin: const EdgeInsets.all(0.0),
                );
              },
            ),
          ),
        ],
      ),

      onSearchTextChanged(String text) async {

            _searchResult.clear();
        if (text.isEmpty) {
          for (int i = 0; i< returnTicketDetails.length;  i++){
            _searchResult.add(userDetails[returnTicketDetails[i]["user_id"]]);
          }
          setState(() {});
          return;
        }


        for (int i = 0; i< returnTicketDetails.length;  i++){

          if (userDetails[returnTicketDetails[i]["user_id"]]["first_name"].toString().toLowerCase().contains(text.toLowerCase()) || userDetails[returnTicketDetails[i]["user_id"]]["last_name"].toString().toLowerCase().contains(text.toLowerCase())
              || returnTicketDetails[i]["code"].toString().toLowerCase().contains(text.toLowerCase())) {
            _searchResult.add(userDetails[returnTicketDetails[i]["user_id"]]);

          }
        }
        setState(() {});
      }

我想知道为什么当我进入我的应用程序的这个屏幕时,ListView是空的,即使我期望列表中的元素很少。但是,只有当我点击搜索栏字段或热重新加载按钮时,数据才会被加载,我的ListView会被正确的数据填充。 理想情况下,作为默认值,我想让ListView在我进入此屏幕后立即显示列表中的所有数据,然后根据用户输入开始搜索过滤器列表中的元素。 提前谢谢

1 个答案:

答案 0 :(得分:0)

I guess the problem is with the line
**child: _searchResult.length != 0 || controller.text.isNotEmpty ?**

since you don't have anything in the _searchResult when the app loads first time your

**new ListView.builder(
               itemCount: _searchResult.length,
              itemBuilder: (context, i) {
                return new Card(
                  child: new ListTile(
                                     ),
                  margin: const EdgeInsets.all(0.0),**

is being rendered with no content to display and when you start typing in the text box onSearchTextChanged is called and the other ListView.builder is rendered with some data (as in your case 2,3 items in list).

Hope it helps!!!