flutter - 如何将小部件动态添加到 Flutter 中的列?

我在 ListView 中添加了一些小部件。这样我就可以滚动所有小部件。现在我需要向 ListView 添加一个小部件来加载评论列表。我无法在 ListView 中添加 ListView。此外,我的评论不需要单独的滚动条。它应该与 ListView 内的小部件一起滚动。所以我打算添加 Column 而不是 ListView。在 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 属性。比如:

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在未来完成后构建 UI。

https://stackoverflow.com/questions/51605131/

相关文章:

android - Flutter Launcher 图标没有改变?

dart - 使用 Navigator.popUntil 和没有固定名称的路由

dart - Flutter Navigator.replace() 示例

dart - 添加点击功能 flutter

amazon-web-services - 如何在不使用原生 SDK 的情况下使用 AWS Cogn

dart - 如何在 Flutter 中创建带有圆角的自定义模糊形状

android - Flutter - Image.network 失败时的默认图像

flutter - 如何在 flutter 中加载所有 dart DateFormat 语言环境?

image - Flutter:如何将 Canvas/CustomPainter 保存到图像文件中?

Flutter - 我想将变量从一个类传递到另一个类