首页 > 解决方案 > 如何控制列表视图的位置?

问题描述

我有一个小部件,用户可以将项目添加到上面的列表视图(如标签),我希望 Listview.builder 始终显示列表的最后一项,当用户向列表添加更多项目时,我该如何提供它?.. …………………………………………………………………………………………………………………………………………………………………………………… ..................................................... ………………………………………………………………………………………………………………………………………………

 @override
  Widget build(BuildContext context) {
    List<Ingredient> _ingredients = Provider.of<IngredientsProvider>(context, listen: true).selectedIngredients;
    return AnimatedPadding(
      duration: Duration(milliseconds: 500),
      curve: Curves.linearToEaseOut,
      padding:EdgeInsets.only(top: _top,right: _right,left: _left,bottom: _bottom),
      child: AnimatedContainer(
          height:_animatedContainerHeight,
          duration: Duration(milliseconds: 500),
          child: Padding(
            padding: const EdgeInsets.all(10.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Container(
                  decoration: BoxDecoration(
                    border:Border.all(style: BorderStyle.solid, width: 1),
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(90),
                  ),
                  height: 60,
                  child: TextField(
                    style: TextStyle(
                      color: Colors.black,
                      fontFamily:"OpenSans",
                      fontSize: 20,
                    ),
                    textAlign: TextAlign.center,
                    textAlignVertical: TextAlignVertical.center,
                    onChanged: (value){
                      if(value.length>0){
                        value=value[0].toUpperCase()+value.substring(1);
                        filterSearchResults(value);
                      }
                      else{
                        filterSearchResults(value);
                      }
                    },
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.search),
                      border:OutlineInputBorder(
                        borderRadius: BorderRadius.circular(90),
                        borderSide: BorderSide(
                          color: kColorTheme10,
                        ),
                      ),
                      hintText: "Malzeme ismi arayın",
                      hintStyle: TextStyle(
                        color: Colors.black.withOpacity(0.5),
                        fontFamily: "OpenSans",
                      ),
                    ),
                  ),
                ),
                SizedBox(height: 2,),
                Expanded(
                  flex: _animatedContainerHeight==350?1:4,
                  child: ListView.builder(
                    scrollDirection: Axis.horizontal,
                    shrinkWrap: true,
                    itemCount: _ingredients.length,
                    itemBuilder: (context,index){
                      return Padding(
                        padding: EdgeInsets.all(1),
                        child: GestureDetector(
                          onTap: (){
                            setState(() {
                              removeIngredient(_ingredients[index],context);
                              if (_ingredients.length == 0) {
                                _toggleCardHeight();
                                _toggleCardSize();
                                }
                              }
                            );
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              color: kColorTheme11,
                              borderRadius: BorderRadius.circular(90),
                              border: Border.all(style: BorderStyle.solid,width: 1),
                            ),
                            child: Padding(
                              padding: EdgeInsets.all(8.0),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  BorderedText(strokeColor: Colors.black,strokeWidth: 2,child: Text(_ingredients[index].ingredientName,style:
                                    TextStyle(fontWeight: FontWeight.bold,fontSize:20,color: Colors.white),)),
                                  SizedBox(width: 5,),
                                  Icon(
                                    Icons.cancel,color: Colors.white,size: 20,
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      );
                    }
                  ),
                ),
                SizedBox(height: 2,),
                Expanded(
                  flex: 40,
                  child: ListView.builder(
                    shrinkWrap: true,
                    itemCount: items.length,
                    itemBuilder: (context, index) {
                      return Padding(
                        padding: EdgeInsets.all(2.0),
                        child: GestureDetector(
                          onTap: ()async{
                            if(_ingredients.length==0){
                              Ingredient ingredient=Ingredient(ingredientName:"${items[index]}",dropDownValue: "Çay Kaşığı",ingredientAmount: null);
                              addIngredient(ingredient,context);
                              _toggleCardHeight();
                              _toggleCardSize();
                              setState(() {});
                            }
                            else{
                              for(var i in _ingredients){
                                if(i.ingredientName==items[index]){
                                  var response= await showAlertDialog(context);
                                  if (response!=true){
                                    setState(() {});
                                  }
                                }
                                else {
                                  Ingredient ingredient=Ingredient(ingredientName:"${items[index]}",dropDownValue: "Çay Kaşığı",ingredientAmount: null);
                                  addIngredient(ingredient,context);
                                  setState(() {});
                                }
                              }
                            }
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(90),
                              border: Border.all(style:BorderStyle.solid,width: 1),
                              color: Colors.white54,

                            ),
                            child: Padding(
                              padding: EdgeInsets.all(5),
                              child: Text('${items[index]}',style: TextStyle(fontWeight: FontWeight.bold),)),
                          ),
                        ),
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
          decoration: BoxDecoration(
            border: Border.all(style: BorderStyle.solid, width: 1),
            borderRadius: BorderRadius.circular(30),
            color:categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId].categoryColor.withOpacity(0.5),
          )
      ),
    );
  }
}

标签: flutter

解决方案


要控制位置,您必须管理ListView.

这是一个理论示例:

final _controller = ScrollController();

@override
Widget build(BuildContext context) {

  return ListView.builder(
    controller: _controller,
    itemCount: 100,
    itemBuilder: (_, __) => ListTile(title: Text('Some Text')),
  );
}

然后,如果您想顺利走到最后一个位置,请执行以下操作:

_controller.animateTo(
  _controller.position.maxScrollExtent,
  duration: Duration(seconds: 1),
  curve: Curves.easeOut,
);

推荐阅读