我正在使用 Flutter 制作有关电影的信息列表。现在我希望左侧的封面图片是圆角图片。我做了以下操作,但没有成功。谢谢!
getItem(var subject) {
var row = Container(
margin: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Container(
width: 100.0,
height: 150.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
color: Colors.redAccent,
),
child: Image.network(
subject['images']['large'],
height: 150.0,
width: 100.0,
),
),
],
),
);
return Card(
color: Colors.blueGrey,
child: row,
);
}
如下
ClipRRect
它将完美工作。
ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.network(
subject['images']['large'],
height: 150.0,
width: 100.0,
),
)
CircleAvatar
:CircleAvatar(
radius: 48, // Image radius
backgroundImage: NetworkImage('imageUrl'),
)
ClipRRect
:ClipOval(
child: SizedBox.fromSize(
size: Size.fromRadius(48), // Image radius
child: Image.network('imageUrl', fit: BoxFit.cover),
),
)
CircleAvatar
:CircleAvatar(
radius: 56,
backgroundColor: Colors.red,
child: Padding(
padding: const EdgeInsets.all(8), // Border radius
child: ClipOval(child: Image.network('imageUrl')),
),
)
ClipRRect
:Container(
padding: EdgeInsets.all(8), // Border width
decoration: BoxDecoration(color: Colors.red, shape: BoxShape.circle),
child: ClipOval(
child: SizedBox.fromSize(
size: Size.fromRadius(48), // Image radius
child: Image.network('imageUrl', fit: BoxFit.cover),
),
),
)
ClipRRect(
borderRadius: BorderRadius.circular(20), // Image border
child: SizedBox.fromSize(
size: Size.fromRadius(48), // Image radius
child: Image.network('imageUrl', fit: BoxFit.cover),
),
)
final borderRadius = BorderRadius.circular(20); // Image border
Container(
padding: EdgeInsets.all(8), // Border width
decoration: BoxDecoration(color: Colors.red, borderRadius: borderRadius),
child: ClipRRect(
borderRadius: borderRadius,
child: SizedBox.fromSize(
size: Size.fromRadius(48), // Image radius
child: Image.network('imageUrl', fit: BoxFit.cover),
),
),
)
还有其他方法,例如使用
DecoratedBox
,但这会使答案有点太长。
也可以使用
CircleAvatar
,它是flutter自带的
CircleAvatar(
radius: 20,
backgroundImage: NetworkImage('https://via.placeholder.com/140x100')
)
试试这个,对我有用:
Container(
width: 100.0,
height: 150.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover, image: NetworkImage('Path to your image')),
borderRadius: BorderRadius.all(Radius.circular(8.0)),
color: Colors.redAccent,
),
),
尝试使用 CircleAvatar 并使用 CachedNetworkImage 加载图像。
CircleAvatar(
radius: 45,
child: ClipOval(
child: CachedNetworkImage(
imageUrl: "https:// your image url path",
fit: BoxFit.cover,
width: 80,
height: 80,
),
),
),
backgroundColor: Colors.deepOrangeAccent,
在这里面
CircleAvatar(
radius: 45,
backgroundColor: Colors.deepOrangeAccent,
child: ClipOval(
child: CachedNetworkImage(
imageUrl: "https:// your image url path",
fit: BoxFit.cover,
width: 80,
height: 80,
),
),
),
Container(
width: 48.0,
height: 48.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image: NetworkImage("path to your image")
)
)),
这是我用过的代码。
Container(
width: 200.0,
height: 200.0,
decoration: BoxDecoration(
image: DecorationImage(image: NetworkImage('Network_Image_Link')),
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(25.0)),
),
),
谢谢!!!
对于图像使用这个
ClipOval(
child: Image.network(
'https://url to your image',
fit: BoxFit.fill,
),
);
对于资产图像使用此
ClipOval(
child: Image.asset(
'Path to your image',
fit: BoxFit.cover,
),
)
你可以像这样使用 ClipRRect :
Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(25),
child: Image.asset(
'assets/images/pic13.jpeg',
fit: BoxFit.cover,
),
),
)
您可以设置半径,或用户仅用于左上角或左下角,例如:
Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25)
,bottomLeft: Radius.circular(25)),
child: Image.asset(
'assets/images/pic13.jpeg',
fit: BoxFit.cover,
),
),
)
使用新版本的 flutter 和 Material 主题,您还需要使用“Padding”小部件,以获得不填充其容器的图像。
例如,如果您想在 AppBar 中插入圆形图像,则必须使用填充,否则您的图像将始终与 AppBar 一样高。
希望这对某人有帮助
InkWell(
onTap: () {
print ('Click Profile Pic');
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ClipOval(
child: Image.asset(
'assets/images/profile1.jpg',
),
),
),
),
使用 ClipRRect 并设置适合的图像属性:BoxFit.fill
ClipRRect(
borderRadius: new BorderRadius.circular(10.0),
child: Image(
fit: BoxFit.fill,
image: AssetImage('images/image.png'),
width: 100.0,
height: 100.0,
),
),
使用 ClipRRect 它将解决您的问题。
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
child: Image.network(
Constant.SERVER_LINK + model.userProfilePic,
fit: BoxFit.cover,
),
),
在此圆形图像中使用这种方式也可以工作+您也有网络图像的预加载器:
new ClipRRect(
borderRadius: new BorderRadius.circular(30.0),
child: FadeInImage.assetNetwork(
placeholder:'asset/loader.gif',
image: 'Your Image Path',
),
)
试试这个效果很好。
Container(
height: 220.0,
width: double.infinity,
decoration: BoxDecoration(
borderRadius: new BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(
photoUrl,
),
),
),
);
图像所有侧面圆角尝试这个
Container(
// height and width depend on your your requirement.
height: 220.0,
width: double.infinity,
decoration: BoxDecoration(
// radius circular depend on your requirement
borderRadius: new BorderRadius.all(
Radius.circular(10),
),
image: DecorationImage(
fit: BoxFit.fill,
// image url your network image url
image: NetworkImage(
"dynamic image url",
),
),
),
);
容器的用户装饰图像。
@override
Widget build(BuildContext context) {
final alucard = Container(
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(10),
image: new DecorationImage(
image: new AssetImage("images/logo.png"),
fit: BoxFit.fill,
)
)
);
Flutter 中的圆形图像
ClipRRect(
child: Image.asset(
"assets/images/ic_cat.png",
width: 80,
height: 80,
),
borderRadius: BorderRadius.circular(50),
))
如果您只想要图像的角点,则只需更改 BorderRadius.circular,如下所示
ClipRRect(
child: Image.asset(
"assets/images/ic_cat.png",
width: 80,
height: 80,
),
borderRadius: BorderRadius.circular(20),
))
创建带圆角的图像:
Flutter 中有多个小部件可以创建圆形图像。具体细节如下:
容器:
Container(
width: 150,
height: 120,
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15) // Adjust the radius as needed
),
child: Image.network(
'https://picsum.photos/seed/picsum/200/300',
fit: BoxFit.cover,
),
),
剪辑RRect
ClipRRect(
borderRadius: BorderRadius.circular(15), // Adjust the radius as needed
child: Image.network(
'https://picsum.photos/seed/picsum/200/300',
fit: BoxFit.cover,
width: 150,
height: 120,
),
)
卡:
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15), // Adjust the radius as needed
),
clipBehavior: Clip.antiAlias,
elevation: 5.0,
child: Image.network(
'https://picsum.photos/seed/picsum/200/300',
width: 150,
height: 120,
fit: BoxFit.cover,
),
)
物理模型:
PhysicalModel(
color: Colors.transparent,
borderRadius: BorderRadius.circular(10),
clipBehavior: Clip.antiAlias,
elevation: 5.0,
child: Image.network(
'https://picsum.photos/seed/picsum/200/300',
width: 150,
height: 120,
fit: BoxFit.cover,
))
创建圆形图像:
容器:
Container(
width: 120,
height: 120,
clipBehavior: Clip.antiAlias,
decoration: const BoxDecoration(
shape: BoxShape.circle,
),
child: Image.network(
'https://picsum.photos/seed/picsum/200/300',
fit: BoxFit.cover,
),
)
剪辑R矩形:
ClipRRect(
borderRadius: BorderRadius.circular(120),
child: Image.network(
'https://picsum.photos/seed/picsum/200/300',
fit: BoxFit.cover,
width: 120,
height: 120,
),
)
圆圈头像:
CircleAvatar(
radius: 60,
backgroundImage: NetworkImage(
'https://picsum.photos/seed/904/600',
),
)
卡:
Card(
shape: const CircleBorder(),
clipBehavior: Clip.antiAlias,
elevation: 5,
child: Image.network(
'https://picsum.photos/seed/904/600',
width: 120,
height: 120,
fit: BoxFit.cover,
),
)
物理模型:
PhysicalModel(
color: Colors.transparent,
clipBehavior: Clip.antiAlias,
elevation: 5.0,
shape: BoxShape.circle,
child: Image.network(
'https://picsum.photos/seed/904/600',
width: 120,
height: 120,
fit: BoxFit.cover,
))
要了解这些小部件及其优先级如何创建此形状,请参阅此内容丰富的博客:在 Flutter 中创建圆角图像和圆形图像的不同方法。