如何动态地将小部件添加到Flutter中的列? [英] How to add the widgets dynamically to column in Flutter?

查看:153
本文介绍了如何动态地将小部件添加到Flutter中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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

            ],
          ),
        ),

推荐答案

如果已经具有注释数据,只需创建一个列表,然后将其传递给Columnchildren属性.像这样:

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,),
        ],
      ),
    ),

如果您还没有注释数据,需要获取它,请使用

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中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆