[英]How to Round image corners with BoxFit.contain in flutter
My problem is when I wrap the image with a container that has a specific size and uses BoxFit.contain property for it will not round the image.我的问题是,当我使用具有特定大小的容器包装图像并使用 BoxFit.contain 属性时,它不会对图像进行四舍五入。 checkout the below image:
查看下图:
I think the image cant round itself because it cants expand to fill container.我认为图像不能自我圆润,因为它不能扩展以填充容器。 I know that I can use BoxFit.cover but I want to use BoxFit.contain because I have limited space and images that can be of any sizes.
我知道我可以使用 BoxFit.cover,但我想使用 BoxFit.contain,因为我的空间和图像可以是任意大小。 my code:
我的代码:
@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,
),
),
),
),
);
}
You have to wrap your ClipRRect
widget with Center
or any Align
widget.您必须用
Center
或任何Align
小部件包装您的ClipRRect
小部件。
Most of the widgets will try to fill its parent, if the parent doesn't specify any alignment property.如果父级未指定任何 alignment 属性,大多数小部件将尝试填充其父级。
In your case the ClipRRect
filled its parent Container
(300x300) since the container doesn't specify any alignment to its child.在您的情况下,
ClipRRect
填充了其父Container
(300x300),因为该容器没有为其子容器指定任何 alignment。 And Image with contain
property will try to maintain image ratio and being centered in ClipRRect
widget.并且具有
ClipRRect
contain
部件中居中。 So It made ClipRRect
corners invisible or no effect.所以它使
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,
),
),
),
),
),
)
Edit: Here I used Center
widget.编辑:这里我使用
Center
小部件。 But you can also just simply specify alignment property in the Container
widget.但您也可以在
Container
小部件中简单地指定 alignment 属性。
Try using BoxFit.fill
if i understood correctly this is what you want to achieve:如果我理解正确,请尝试使用
BoxFit.fill
这是您想要实现的目标:
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: Text("My image size"),
),
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.fill,
),
),
),
),
);
}
Put in a codepen demo放入一个codepen演示
EDIT for a workaround to your question:编辑您的问题的解决方法:
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
Future<Widget> getImage() async {
final Completer<Widget> completer = Completer();
final url = 'https://i.stack.imgur.com/lkd0a.png';
final image = NetworkImage(url);
final config = await image.obtainKey(const ImageConfiguration());
final load = image.load(config);
final listener = new ImageStreamListener((ImageInfo info, isSync) async {
print(info.image.width);
print(info.image.height);
completer.complete(Container(
child: Image(
image: image,
height: info.image.height.toDouble(),
width: info.image.width.toDouble(),
)));
});
load.addListener(listener);
return completer.future;
}
return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: Text("My image size"),
),
body: Center(
child: FutureBuilder<Widget>(
future: getImage(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return snapshot.data;
} else {
return Text('LOADING...');
}
},
),
),
);
}
}
You can simply wrap the widget with a ClipOval with flutter.您可以使用带有 flutter 的 ClipOval 简单地包装小部件。
Take a look at this doc: https://api.flutter.dev/flutter/widgets/ClipOval-class.html看看这个文档: https://api.flutter.dev/flutter/widgets/ClipOval-class.html
use stack for it使用堆栈
Stack(
children: <Widget>[
],
)
Here, I'm simply using a container with an image inside it在这里,我只是使用一个容器,里面有一个图像
Container(
decoration: Box Decoration(
image: Decoration Image(
image: Asset Image('images/new_york.jpg'),
fit: Box Fit . fit Height,
),
),
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.