flutter - 如何控制列表视图的位置?
问题描述
我有一个小部件,用户可以将项目添加到上面的列表视图(如标签),我希望 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),
)
),
);
}
}
解决方案
要控制位置,您必须管理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,
);
推荐阅读
- python - 赋值运算符如何创建深层副本?
- batch-file - 创建一个批处理文件来备份文件夹,以及如何在其中排除一个文件夹进行备份
- python-3.x - Visual Studio Code,Python 调试不起作用
- python - odoo 从 anthor 类调用按钮上的方法
- java - 出现意外错误(类型=不支持的媒体类型,状态=415)。不支持内容类型“null”
- c# - 为什么 RQM 执行变量使用 IVariablesManager 仅将空值传递给 RFT?
- docker - 尝试在mac中通过gradle构建docker镜像
- java - 无法使用 Java 从松弛通道检索附件
- java - 如何在 Jena 中创建空白节点资源
- html - 如何隐藏在每个html标签之后附加的br标签