Flutter中如何在Column内对齐指定Widget?
Flutter Column内Widget对齐解决方案
需求说明
左侧为固定尺寸(130x130)的黄色容器,右侧橙色区域内的Column包含两行文本:
- 第一行文本可能内容较长,会撑高外层紫色Row的高度
- 第二行文本需要对齐左侧黄色容器的底部,若外层Row被撑高,则保持在该底部位置的下方
修改后代码
Row(children: [ Expanded( child: ColoredBox( color: Colors.deepPurpleAccent, child: Row(crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( color: Colors.amberAccent, width: 130, height: 130, ), Expanded( child: ColoredBox( color: Colors.deepOrangeAccent, child: ConstrainedBox( constraints: const BoxConstraints(minHeight: 130, maxHeight: double.infinity), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Text( "此行文本可能包含大量字符,会扩展`deepPurpleAccent` Row的高度", style: TextStyle(fontSize: 14), ), ), Text("此行文本需对齐左侧amberAccent Container的底部或其下方") ]), ), ), ) ]), ), ) ]),
实现逻辑
- 给第一行文本外层包裹
Expanded,让它占据Column内除第二行文本外的所有可用空间,从而将第二行文本推至Column底部,刚好对应左侧黄色容器的底部位置 - 当第一行文本过长撑高外层Row时,Column高度同步增加,
Expanded会让第一行文本填充额外高度,第二行文本始终保持在Column底部,满足“对齐黄色容器底部或其下方”的需求
内容的提问来源于stack exchange,提问作者chdo
相关产品推荐
相关产品推荐

