CrossAxisAlignment.stretch 不生效?
Flutter 版本:2.5.1
布局代码如下,加 Container
是为了设置个背景颜色,看效果,单独使用 Row
也一样:
ElevatedButton(onPressed: buttonAction, child: const Text("切换布局样式")),
Text(text),
Container(
// height: 100,
color: Colors.black12,
child: Row(
......
![请添加图片描述 Flutter Row 或者 Column 的拉伸效果,CrossAxisAlignment.stretch_背景颜色](//dev-img.mos.moduyun.com/20231025/0ed03ec5-58d7-441c-b956-0874ba8fc785.png)
实际效果:没有拉伸!
![请添加图片描述 Flutter Row 或者 Column 的拉伸效果,CrossAxisAlignment.stretch_flutter_02](//dev-img.mos.moduyun.com/20231025/68325344-bf40-4a96-ab64-cfbc83648310.png)
指定高度
当给 Container
设置指定高度时,这时才生效
ElevatedButton(onPressed: buttonAction, child: const Text("切换布局样式")),
Text(text),
Container(
height: 100,
color: Colors.black12,
child: Row(
......
![请添加图片描述 Flutter Row 或者 Column 的拉伸效果,CrossAxisAlignment.stretch_背景颜色_03](//dev-img.mos.moduyun.com/20231025/22170f4e-e2b0-4e57-a00d-dc987512f380.png)
实际效果:拉伸!
![请添加图片描述 Flutter Row 或者 Column 的拉伸效果,CrossAxisAlignment.stretch_sed_04](//dev-img.mos.moduyun.com/20231025/65ea4b71-21a1-42b1-9018-468125197f9b.png)
由于 Row
的默认高度是取 高度最大
的子 Widget
的高度
当要拉伸所有子 Widget
时,由于父 Widget
没有指定高度
所以,就导致拉伸失效吧
官方文档对 stretch
的解释:
/// This causes the constraints passed to the children to be tight in the
/// cross axis.