问题描述
我在ListView
内添加了一些小部件.这样我就可以滚动所有小部件.现在,我需要在ListView
中再添加一个小部件以加载注释列表.我不能在ListView
内添加ListView
.而且,我不需要单独的滚动条来发表评论.它应该与ListView
中的小部件一起滚动.因此,我计划添加Column
而不是ListView
.有什么帮助可以在Columns
中动态添加我的评论吗?
I have added a few widgets inside the ListView
. So that I can scroll all widgets. Now I required to add one more widget to the ListView
to load the list of comments. I can not add the ListView
inside the ListView
. And moreover, I do not require the separate scroll for my comments. It should be scroll along with the widgets inside the ListView
. So I planned to add the Column
instead of ListView
. Could any help to add my comments dynamically in the Columns
?
new Expanded(
child:
new ListView(
shrinkWrap: true,
children: <Widget>[
// Title
new Padding(padding: const EdgeInsets.only(
top: 10.00, left: 10.00),
child: new Text(
_feed.title, textAlign: TextAlign.start,),
),
// content
new Container(
child: new Text(
_feed.content, textAlign: TextAlign.start,),
),
// Comments List will go here
],
),
),
推荐答案
如果已经具有注释数据,只需创建一个列表,然后将其传递给Column
的children
属性.像这样:
If you have the comments data already, simply create a List, then pass it to the children
property of the Column
. Something like:
var commentWidgets = List<Widget>();
for (var comment in comments) {
commentWidgets.Add(Text(comment.text)); // TODO: Whatever layout you need for each widget.
}
…
new Expanded(
child:
new ListView(
shrinkWrap: true,
children: <Widget>[
// Title
new Padding(padding: const EdgeInsets.only(
top: 10.00, left: 10.00),
child: new Text(
_feed.title, textAlign: TextAlign.start,),
),
// content
new Container(
child: new Text(
_feed.content, textAlign: TextAlign.start,),
),
// Comments List will go here
Column(children: commentWidgets,),
],
),
),
如果您还没有注释数据,需要获取它,请使用 FutureBuilder ,以便在将来完成后构建用户界面.
If you don't have the comments data already and need to fetch it, use a FutureBuilder to build the UI once the future completes.
这篇关于如何动态地将小部件添加到Flutter中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!