image - 如何在颤动中使用 BoxFit.contain 圆角图像
问题描述
我的问题是,当我使用具有特定大小的容器包装图像并使用 BoxFit.contain 属性时,它不会对图像进行四舍五入。查看下图:
我认为该图像不能自圆,因为它不能扩展以填充容器。我知道我可以使用 BoxFit.cover,但我想使用 BoxFit.contain,因为我的空间和图像可以是任意大小。我的代码:
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Container(
color: Colors.red,
height: 300,
width: 300,
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Image.network(
'https://i.ytimg.com/vi/fq4N0hgOWzU/maxresdefault.jpg',
fit: BoxFit.contain,
),
),
),
),
);
}
解决方案
你必须ClipRRect
用Center
或任何小部件包装你的小Align
部件。
如果父级未指定任何对齐属性,大多数小部件将尝试填充其父级。
在您的情况下,ClipRRect
填充了其父Container
级(300x300),因为容器未指定与其子级的任何对齐方式。并且具有contain
属性的图像将尝试保持图像比例并在ClipRRect
小部件中居中。所以它使ClipRRect
角落不可见或没有效果。
演示: 飞镖板
Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: Text("My image size"),
),
body: Center(
child: Container(
color: Colors.red,
height: 300,
width: 300,
child: Center(
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Image.network(
'https://mfiles.alphacoders.com/847/847991.jpg',
fit: BoxFit.contain,
),
),
),
),
),
)
编辑:在这里我使用Center
了小部件。但是您也可以在Container
小部件中简单地指定对齐属性。
推荐阅读
- java - android 跳过了 44 帧!应用程序可能在其主线程上做了太多工作
- java - 在 Android 上运行时修改方法定义的注解字符串参数
- javascript - 结合对象数组的特定属性
- azure-devops - 在 azure 管道 yaml 中获取合并的分支名称
- reactjs - 如何在具有 ReactJs 代码的 Wildfly 中配置静态 index.html
- matlab - 以下 .sh 脚本是否有资格被并行处理?
- javascript - ReactJS - 仅在 ComponentDidMount 之后调用函数一次
- firebase - Flutter 将多张图片上传到 Firebase
- android - 当应用程序从前台被杀死时在 Ionic 应用程序中显示通知
- java - 如何从 RecyclerView Android 中的编辑文本中获取价值