ListView.builder widget not scrolling



我的页面上有一个ListView.builder小部件。问题是小部件在显示其结果时没有滚动。请问我做错了什么?下面的代码显示了我到目前为止所尝试的内容。

我尝试只将ListView.builder小部件放在页面上,在这种情况下小部件会滚动,但是一旦我添加了另一个小部件,Listview就会停止滚动。

@override Widget build(BuildContext context) { 返回脚手架( 应用栏:应用栏(

title: Text(widget.title,style: TextStyle(fontSize: 14),),
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
physics: AlwaysScrollableScrollPhysics(),
child:Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child:Expanded(
child: Image.asset("assets/images/1frenchToast.webp"),
),
// ),
),
],
),
SingleChildScrollView(
child: ListView.builder(
//scrollDirection: Axis.vertical,
physics: AlwaysScrollableScrollPhysics(),
itemCount:  foodCategory != null?foodCategory.length:1,
itemBuilder: (context,index){
return ListTile(
//dense: true,
leading: Container(
margin: EdgeInsets.only(bottom: 10),
child:Image.asset('assets/images/3poached-egg.webp',),
),
title: Text(foodCategory != null?foodCategory[index].foodType:1),
onTap: (){
_navigate(dropDownSelectedItemState, foodCategory[index].foodType);
},
);
},shrinkWrap: true,
// physics: AlwaysScrollableScrollPhysics(),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: null,
tooltip: 'Increment',
child: Icon(Icons.add),

我希望能够使用显示的代码使我的 ListView.builder 滚动

根据另一个线程中@VidorVistrom的建议,这就是我解决问题的方式。我将ListView.builder包装在一个容器小部件中,简单地给它一个200的高度,并删除了ListView.builder周围的SingleChildScrollView,并解决了这个问题。以防这对其他人有帮助。

完整代码如下所示:

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title,style: TextStyle(fontSize: 14),),
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
physics: AlwaysScrollableScrollPhysics(),
child:Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child:Expanded(
child: Image.asset("assets/images/1frenchToast.webp"),
),
// ),
),
],
),
Container(//child:SingleChildScrollView(
height: 200,
margin: EdgeInsets.only(top: 20),
//scrollDirection: Axis.vertical,
// physics: AlwaysScrollableScrollPhysics(),
child: ListView.builder(
scrollDirection: Axis.vertical,
physics: AlwaysScrollableScrollPhysics(),
itemCount:  foodCategory != null?foodCategory.length:1,
itemBuilder: (context,index){
return ListTile(
dense: true,
leading: Container(
margin: EdgeInsets.only(bottom: 10),
child:Image.asset('assets/images/3poached-egg.webp',),
),
title: Text(foodCategory != null?foodCategory[index].foodType:1),
onTap: (){
_navigate(dropDownSelectedItemState, foodCategory[index].foodType);
},
);
},shrinkWrap: true,
// physics: AlwaysScrollableScrollPhysics(),
),//)
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: null,
tooltip: 'Increment',
child: Icon(Icons.add),

最新更新