水平SingleChildScrollView内的垂直ListView

问题描述 投票:2回答:1

我需要建立一个页面,其中包含以单行分组的对象列表。需要同时垂直滚动(以显示更多组)和水平滚动(以显示子组)。垂直和水平滚动条需要完全移动项目。像这样的东西:https://gph.is/g/46gjW0q

我正在使用此代码执行此操作:

Widget _buildGroups() {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Container(
        width: 2000,
        child: ListView.builder(
          scrollDirection: Axis.vertical,
          itemCount: boardData.length,
          itemBuilder: (context, index) {
            return Row(children: _buildSingleGroup(index));
          },
        ),
      ),
    );
}

List<Widget> _buildSingleGroup(int groupIndex) {
    return List.generate(...);
}

我需要使其具有水平动态性,但是在这里,如您所见,我需要设置宽度原因,否则会发生错误:

══════════════════ Exception caught by rendering library ═════════════════════
The following assertion was thrown during performResize()
Vertical viewport was given unbounded width.

Viewports expand in the cross axis to fill their container and constrain their 
children to match their extent in the cross axis. In this case, a vertical 
viewport was given an unlimited amount of horizontal space in which to expand.

我试图在ListView中设置“ shrinkWrap:true”,但结果给出了另一个错误:

Failed assertion: line 1629 pos 16: 'constraints.hasBoundedWidth': is not true.

那么,如何使用动态的ListView宽度做到这一点?

flutter flutter-layout
1个回答
0
投票

发生错误是因为ListView试图从其父级获取高度以填充整个高度时,由于SingleChildScrollView没有固定的容器高度值,因此父级返回无穷高。要解决该问题,您需要限制高度值。例如,我使用SizedBox指定SizedBox的高度。

以下代码对我有用。

ListView
© www.soinside.com 2019 - 2024. All rights reserved.