Flutter AspectRatio 组件,设定组件宽高比
参数详解
属性 | 说明 |
aspectRatio | 宽高比。长宽比表示为宽高比。例如,16:9宽高比的值为16.0/9.0。 |
child | 子元素 |
代码示例
class MyBodyA extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
width: 300,
// height: 300,
color: Colors.blue,
child: AspectRatio(
aspectRatio: 3/1,
child: Container(
color: Colors.red,
),
),
);
}
}
特别说明
AspectRatio 宽高比是相对父容器的。宽是父容器的宽,高是根据指定的比例计算出来的。
如果父容器 同时给定了宽和高 则:AspectRatio 宽高比不生效。
效果图
父容器给定宽 父容器给定高