当我向卡中添加图像时,卡顶部的半径被覆盖。我该如何解决?
当我向卡中添加图像时,卡顶部的半径被覆盖。我该怎么解决?
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.yellow),
home: Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(title: Text('Demo'),),
body: SizedBox(
height: 310.0,
child: Card(
elevation: 3.0,
color: Colors.white,
margin: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 0.0,),
Image.network('https://img.zcool.cn/community/012157578c405f0000012e7e69e7cd.jpg@1280w_1l_2o_100sh.jpg'),
SizedBox(height: 16.0,),
Row(
children: <Widget>[
SizedBox(width: 16.0,),
Text('素雪', style: Theme.of(context).textTheme.headline,),
SizedBox(width: 16.0,),
Text('吉时已到', style: Theme.of(context).textTheme.subhead,),
],
),
SizedBox(height: 16.0,),
],
),
))),
);
}
}
这是渲染
答案 0 :(得分:5)
您可以为卡设置clipBehavior
:
Card(
clipBehavior: Clip.antiAliasWithSaveLayer, ...
或者您可以将图片包装在ClipRRect
ClipRRect(
borderRadius: BorderRadius.vertical(top: Radius.circular(4.0)),
child: Image.network(...),
)
答案 1 :(得分:0)
您需要将图片放在Container
或DecoratedBox
中,并将BorderRadius
放在BoxDecoration
上。
children: <Widget>[
.....
Container(
width: double.maxFinite,
height: 220.0,
decoration: BoxDecoration(
borderRadius:
BorderRadius.vertical(top: Radius.circular(5.0)),
image: DecorationImage(
image: NetworkImage(
'https://img.zcool.cn/community/012157578c405f0000012e7e69e7cd.jpg@1280w_1l_2o_100sh.jpg'),
fit: BoxFit.cover,
),
),
),
...
]
答案 2 :(得分:0)
如果您真的很在意性能,请不要使用clipBehavior: Clip.antiAliasWithSaveLayer
。如chemamolins'sanswer中所述,将Container
或DecoratedBox
与BorderRadius
一起使用。
请参阅Performance best practices
答案 3 :(得分:-1)
您可以将图像放在Material
中,并设置Border Radius
和Clip Behaviour
。
Material(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
elevation: 2.0,
clipBehavior: Clip.antiAliasWithSaveLayer,
type: MaterialType.transparency,
child: Image.asset(
"images/user.png",
height: 150,
width: double.infinity,
fit: BoxFit.cover,
),
),