颤动gridview无限双轴

时间:2020-06-19 13:21:16

标签: flutter gridview scroll

我的应用程序需要显示大量的单元格(> 200),并且我可以轻松地使用下面的代码垂直滚动,但是我还需要水平滚动gridview。像元需要固定宽度,并且不能像crossAxisCount那样按比例缩放。要求至少需要30列固定宽度和至少30行

return MaterialApp(
  title: 'test',
  home: Scaffold(
    body: CustomScrollView(
      slivers: <Widget>[
        SliverGrid(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 5,
          ) ,
          delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
            return new Container(
                color: _randomColor(index),
                height: 200.0
            );
          }),
        ),
      ],
    ),
  ),
);

1 个答案:

答案 0 :(得分:0)

网格将始终至少具有一个轴作为设置长度,两个轴不能无限。

作为解决方案,可以使用两个SingleChildScrollView(一个垂直和一个水平),并用它包装任何子级(在我的示例中为15行x 15列):

  SingleChildScrollView(
    scrollDirection: Axis.horizontal,
    child: SingleChildScrollView(
      child: Row(
        children: List.generate(
          15,
          (index) => Column(
              children: List.generate(
                  15,
                  (index) => Container(
                      width: 200,
                      height: 200,
                      color: Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0)))),
        ),
      ),
    ),
  );