Row常见属性
- mainAxisAlignment:用于控制主轴(水平方向)上子Widget的排列方式。
- 值:start(左对齐)、end(右对齐)、center(居中对齐)、spaceBetween(两端对齐,子Widget之间间距相等)、spaceAround(子Widget两侧间距相等,且为子Widget之间间距的一半)、spaceEvenly(所有间距相等)。
- 实际场景:在布局一个包含图片和文字的卡片组件时,如果希望图片在左,文字在右,可以使用
mainAxisAlignment: MainAxisAlignment.spaceBetween
。示例代码:
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Image.asset('image_path'),
Text('这是一段文字')
],
)
- crossAxisAlignment:控制交叉轴(垂直方向)上子Widget的对齐方式。
- 值:start(顶部对齐)、end(底部对齐)、center(垂直居中对齐)、stretch(拉伸填充交叉轴)。
- 实际场景:当图片和文字高度不一致,希望它们垂直居中对齐时,使用
crossAxisAlignment: CrossAxisAlignment.center
。示例代码:
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image.asset('image_path', height: 50),
Text('这是一段文字', style: TextStyle(fontSize: 20))
],
)
- textDirection:指定文本方向,影响主轴方向上子Widget的排列顺序。
- 值:TextDirection.ltr(从左到右)、TextDirection.rtl(从右到左)。
- 实际场景:对于支持从右到左语言(如阿拉伯语)的应用,可设置
textDirection: TextDirection.rtl
,让子Widget从右到左排列。示例代码:
Row(
textDirection: TextDirection.rtl,
children: [
Text('右边的文字'),
Text('左边的文字')
],
)
- mainAxisSize:控制Row在主轴方向上占用的空间大小。
- 值:MainAxisSize.max(尽可能占用多的空间)、MainAxisSize.min(尽可能占用少的空间)。
- 实际场景:如果希望卡片组件在水平方向尽可能紧凑,可以使用
mainAxisSize: MainAxisSize.min
。示例代码:
Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.favorite),
Text('喜欢')
],
)
- children:Row中包含的子Widget列表。
- 实际场景:在卡片组件布局中,将图片和文字等组件添加到
children
列表中进行组合。示例代码:
Row(
children: [
Image.asset('image_path'),
Column(
children: [
Text('标题'),
Text('描述')
],
)
],
)
Column常见属性
- mainAxisAlignment:控制主轴(垂直方向)上子Widget的排列方式。
- 值:start(顶部对齐)、end(底部对齐)、center(居中对齐)、spaceBetween(两端对齐,子Widget之间间距相等)、spaceAround(子Widget两侧间距相等,且为子Widget之间间距的一半)、spaceEvenly(所有间距相等)。
- 实际场景:在布局一个垂直排列的菜单,希望菜单项在垂直方向两端对齐时,使用
mainAxisAlignment: MainAxisAlignment.spaceBetween
。示例代码:
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('菜单1'),
Text('菜单2'),
Text('菜单3')
],
)
- crossAxisAlignment:控制交叉轴(水平方向)上子Widget的对齐方式。
- 值:start(左对齐)、end(右对齐)、center(水平居中对齐)、stretch(拉伸填充交叉轴)。
- 实际场景:在卡片组件中,如果图片和文字垂直排列,希望文字水平居中,可使用
crossAxisAlignment: CrossAxisAlignment.center
。示例代码:
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image.asset('image_path'),
Text('这是一段文字')
],
)
- textDirection:指定文本方向,这会影响交叉轴上子Widget的排列顺序(如果子Widget的排列方向受文本方向影响)。
- 值:TextDirection.ltr(从左到右)、TextDirection.rtl(从右到左)。
- 实际场景:对于从右到左语言的应用,当Column中有水平排列的子Widget(如包含文本的Row)时,设置
textDirection: TextDirection.rtl
,使这些子Widget从右到左排列。示例代码:
Column(
textDirection: TextDirection.rtl,
children: [
Row(
children: [
Text('右边的文字'),
Text('左边的文字')
],
)
],
)
- mainAxisSize:控制Column在主轴方向上占用的空间大小。
- 值:MainAxisSize.max(尽可能占用多的空间)、MainAxisSize.min(尽可能占用少的空间)。
- 实际场景:在列表项中,如果希望垂直排列的内容尽可能紧凑,可使用
mainAxisSize: MainAxisSize.min
。示例代码:
Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('简短标题'),
Text('简短描述')
],
)
- children:Column中包含的子Widget列表。
- 实际场景:在布局一个垂直排列的卡片,将图片、标题、描述等组件添加到
children
列表中。示例代码:
Column(
children: [
Image.asset('image_path'),
Text('标题'),
Text('描述')
],
)