为什么 AspectRatio 放置在 ListView 中时不保留我的宽高比?
return ListView(
children: [
Container(
height: 200,
child: AspectRatio(
aspectRatio: 1 / 2,
child: Container(
color: Colors.red,
),
),
),
],
);
Run Code Online (Sandbox Code Playgroud)
return Container(
height: 200,
child: AspectRatio(
aspectRatio: 1 / 2,
child: Container(
color: Colors.red,
),
),
);
Run Code Online (Sandbox Code Playgroud)
这是因为“在横轴上,子项需要填充ListView。 ”所以ListView拉伸了Container。
解决方案是将容器包装在中心小部件中,这有助于维护子小部件的大小“以防父小部件对容器应采用的大小有自己的意见”,如下所示:
Center(
child: Container(
height: 200,
child: AspectRatio(
aspectRatio: 1 / 2,
child: Container(
color: Colors.red,
),
),
),
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2080 次 |
| 最近记录: |