ListView构建器不会在所需时间内更改

时间:2019-11-30 16:00:27

标签: flutter dart

我有一个搜索方法,它可以检测元素表单映射并将主题放入List变量,其中List View构建器将从中获取他的元素。 当我在Text Filed中插入单词时出现问题,直到关闭键盘然后看到更改后,列表视图生成器才更改她的声明。 我想查看当前搜索的元素

这是我的代码:

  _showDialog(){
    var searchList = [];
    int _manyOfWidgetShow = 0;
    double heightOfListView = 0;
   double higNumber(){
     double _number;
      if(_manyOfWidgetShow == 0){
          _number = 0.25;
      }
      else if(_manyOfWidgetShow == 1){
          _number = 0.3;

      }else if(_manyOfWidgetShow == 2){
          _number = 0.5;
      }else if(_manyOfWidgetShow == 3){
        _number = 0.6;
      }
      return _number;
    }
    void _showSearchReturn(String query){
      List _items = [];
      if(query.isNotEmpty){
       testList.forEach( (element){
         if(element["name"].toString().toLowerCase().startsWith(query) && _items.length < 3){
             _items.add(element);
         }
         else if(query.length >= 2 && element["name"].toString().toLowerCase().contains(query) && _items.length < 3){
           _items.add(element);
         }
       }
       );
       setState(() {
         _manyOfWidgetShow = _items.length;
         if(_manyOfWidgetShow != 0) {
           heightOfListView = _manyOfWidgetShow * 50.0;
         }else{
           heightOfListView = 0;
         }
         searchList.clear();
         searchList.addAll(_items);
       });
     }else{
       setState(() {
         _manyOfWidgetShow = _items.length;
         if(_manyOfWidgetShow != 0){
           heightOfListView = _manyOfWidgetShow * 50.0;
         }else{
           heightOfListView = 0;
         }
         searchList.clear();
       });
     }
    }
    showDialog(
        context: context,
      builder: (BuildContext context){
          var _size = MediaQuery.of(context).size;
          return Container(

            child: Card(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(30)
              ),
              child: Container(
                height: _size.height * higNumber(),
                child: Column(
                  children: <Widget>[
                    Card(
                        margin: EdgeInsets.only(top:20,bottom: 15),
                        elevation: 4,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(15)
                        ),
                        child: Container(
                          alignment: Alignment.center,
                          padding: EdgeInsets.only(top: 15,bottom: 18),
                          child: TextField(
                            onChanged: (value){
                              _showSearchReturn(value);
                            },
                            scrollPadding: EdgeInsets.all(20),
                            autocorrect: false,
                            decoration: InputDecoration.collapsed(
                              hintText: "Movie name",
                            ),
                            style: TextStyle(
                                fontSize: 20
                            ),
                            strutStyle: StrutStyle(
                                fontWeight: FontWeight.bold,
                                height: 1.5
                            ),
                            controller: searchText ,
                            cursorWidth: 2,
                            cursorColor: Color(0xff54C4A2),
                            textAlign: TextAlign.center,
                          ),
                        )
                    ),
                    Container(
                        height: heightOfListView,
                        child: ListView.builder(
                            itemCount: searchList.length,
                            shrinkWrap: true,
                            itemBuilder:(context,index){
                              return ItemInfo(searchList[index]["name"],searchList[index]["id"]);
                            }
                        )
                    ),
                    Expanded(
                        child: Container(
                          alignment: Alignment.bottomLeft,
                          child: IconButton(icon: Icon(Icons.arrow_back_ios,color: Colors.black54,),
                            onPressed: (){
                            },

                          ),
                        )
                    )
                  ],
                ),
              ),
            )
          );
      }
    );
  }

0 个答案:

没有答案