我使用Flutter开发了一个带有GridView的应用程序。GridView项是Card, 默认卡片形状为矩形,半径为4。
我知道Card Widget有shape属性,它需要ShapeBorder类,但我不知道如何使用ShapeBorder类并在GridView中自定义我的卡片。
我该怎么做?
我使用Flutter开发了一个带有GridView的应用程序。GridView项是Card, 默认卡片形状为矩形,半径为4。
我知道Card Widget有shape属性,它需要ShapeBorder类,但我不知道如何使用ShapeBorder类并在GridView中自定义我的卡片。
我该怎么做?
您可以这样使用
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Text(
'Card with circular border',
textScaleFactor: 1.2,
),
),
Card(
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Text(
'Card with Beveled border',
textScaleFactor: 1.2,
),
),
Card(
shape: StadiumBorder(
side: BorderSide(
color: Colors.black,
width: 2.0,
),
),
child: Text(
'Card with Stadium border',
textScaleFactor: 1.2,
),
),
我总是使用RoundedRectangleBorder来制作卡片。
Card(
color: Colors.grey[900],
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.white70, width: 1),
borderRadius: BorderRadius.circular(10),
),
margin: EdgeInsets.all(20.0),
child: Container(
child: Column(
children: <Widget>[
ListTile(
title: Text(
'example',
style: TextStyle(fontSize: 18, color: Colors.white),
),
),
],
),
),
),
Card
小部件中添加 elevation: 0
以删除默认阴影。 - Erisan OlasheniThemeData.cardTheme
全局自定义卡片主题:MaterialApp(
title: 'savvy',
theme: ThemeData(
cardTheme: CardTheme(
shape: RoundedRectangleBorder(
borderRadius: const BorderRadius.all(
Radius.circular(8.0),
),
),
),
// ...
以上问题的另一种解决方案
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20))),
color: Colors.white,
child: ...
)
您可以使用BorderRadius.only()来自定义您想要处理的角落。
更好的自定义卡片圆角(以及其他选项)的方法是全局设置卡片主题。这样,您可以在整个应用程序中使用相同的卡片样式。您也可以将此方法用于许多其他小部件。
对于卡片主题,您可以使用ThemeData.cardTheme
。
MaterialApp(
title: 'MySampleApp',
theme: ThemeData(
cardTheme: CardTheme(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16.0),
),
),
),
// ............
// ............
BorderRadius.circular(8)
代替 BorderRadius.all(Radius.circular(5.0))
。 - Son NguyenCard(
elevation: 20,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(100),
),
child: const Icon(
Icons.list,
size: 92,
),
)