无法在列中包装水平滚动ListView

时间:2018-06-10 12:34:32

标签: listview containers flutter expandable

我正在尝试在Flutter中创建以下UI。

enter image description here

以下是我的代码和相应的输出。

body: new Column(
      children: <Widget>[
        getTabs(),
        new Container(
          child: Expanded(
            child: new ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: 10,
                itemBuilder: (context, index) => new Container(
                  alignment: Alignment.topCenter,
                  child: new Stack(
                        alignment: Alignment.topCenter,
                        children: <Widget>[
                          new Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: new Container(
                              width: 60.0,
                              height: 60.0,
                              decoration: new BoxDecoration(
                                shape: BoxShape.circle,
                                image: new DecorationImage(
                                    fit: BoxFit.fill,
                                    image: new NetworkImage(
                                        "https://pbs.twimg.com/profile_images/916384996092448768/PF1TSFOE_400x400.jpg")),
                              ),
                              //margin: const EdgeInsets.symmetric(horizontal: 8.0),
                            ),
                          ),
                          index == 0
                              ? Positioned(
                                  right: 10.0,
                                  bottom: 10.0,
                                  child: new CircleAvatar(
                                    backgroundColor: Colors.blueAccent,
                                    radius: 10.0,
                                    child: new Icon(
                                      Icons.add,
                                      size: 14.0,
                                      color: Colors.white,
                                    ),
                                  ))
                              : new Container()
                        ],
                      ),
                ),

            ),
          ),
        ),
      ],
    )

以下是此代码工作后的内容。

enter image description here

该列表工作正常。我甚至可以滚动它但是一旦我尝试将它包装在一列中,我就会出错。我想将它包装在一个列中,以便我可以在列表之前添加一些小部件。我无法将其包装在任何其他小部件中。如果我能将这个listview包装在一张卡片中,那将是有意义的。我错过了什么吗?

The following assertion was thrown during performLayout():
RenderFlex children have non-zero flex but incoming height constraints are unbounded. When a column is in a parent that does not provide a finite height constraint, for example, if it is in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining space in the vertical direction. These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent. Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible children (using Flexible rather than Expanded). This will allow the flexible children to size themselves to less than the infinite remaining space they would otherwise be forced to take, and then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum constraints provided by the parent.

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

Expanded放在包含Container的{​​{1}}周围,而不是ListView本身。

答案 1 :(得分:0)

您有两种选择: 设置列的高度或将其展开。