Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

请问 flutter 网格布局 高度如何自适应子组件?

我想写个网格布局,下图中红色的子组件平均分布,居中显示

请问绿色的网格布局如何自适应红格子的高度呢?

image

我尝试的写法:

Widget build(BuildContext context) {
    return Container(
      height: 300,
      child: GridView.count(
        padding: EdgeInsets.all(10),
        crossAxisSpacing: 10,
        crossAxisCount: 3,
        mainAxisSpacing: 10,
        children: List.generate(
          9,
          (index) {
            return Container(
              width: 5.0,
              height: 5.0,
              alignment: Alignment.center,
              child: Text(
                '-',
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
              color: Colors.blue,
            );
          },
        ),
      ),
    );
  }

渲染出来的效果:

image

这样写第三行 height: 300, 去掉后无法运行,

请问如何让这个高度自适应子组件的高度和呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
GridView.count(
          shrinkWrap: true,
          )

加个这个属性


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...