当图像太宽时,如何使图像适合可用空间?



我有一个图像资源,宽度为1250px。我想让它填充给定的空间,但由于图像太大,我总是得到一个溢出错误。我已经尝试为图像指定宽度,但当然这是设备相关的,所以不是一个解决方案。我怎样才能使它适合给定的空间?

Widget build(BuildContext context) {
return Stack(
children: [
Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(
left: 12.0, top: 5.0, bottom: 16.0, right: 0.0),
child: Row(
children: [
Image.asset(
'assets/images/my_image.png',
fit: BoxFit.fitWidth,
width: 348, // works on one device but not others
),
],
),
),
],
),
],
);
}

你可以尝试使用手机特有的动态宽度

Image.asset(
'assets/images/my_image.png',
fit: BoxFit.fitWidth,
width: MediaQuery.of(context).size.width-12,                 
),

如果您想填充屏幕宽度,请换行图像。具有如下所示的扩展小部件的资产。如果它不工作,请分享你到底想要实现什么。

Expanded(
child: Image.asset(
'assets/images/my_image.png',
fit: BoxFit.fitWidth
),
),

最新更新