简体   繁体   English

自定义卡片形状 Flutter SDK

[英]Custom Card Shape Flutter SDK

I just started learning Flutter and I have developed an app with GridView.我刚开始学习 Flutter,我已经用 GridView 开发了一个应用程序。 GridView items are Card. GridView 项目是卡片。 Default card shape is Rectangle with a radius of 4.默认卡片形状为半径为 4 的矩形。

I know there is shape property for Card Widget and it takes ShapeBorder class.我知道 Card Widget 有 shape 属性,它需要 ShapeBorder 类。 But I am unable to find how to use ShapeBorder class and customize my cards in GridView.但我无法找到如何使用 ShapeBorder 类并在 GridView 中自定义我的卡片。

Thanks in Advance.提前致谢。

You can use itthis way你可以用它这样

在此处输入图片说明

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 Beveled border',
    textScaleFactor: 1.2,
  ),
),

When Card I always use RoundedRectangleBorder.当卡片我总是使用 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),
            ),
        ),
        ],
    ),
  ),
),

You can also customize the card theme globally with ThemeData.cardTheme :您还可以使用ThemeData.cardTheme全局自定义卡片主题:

MaterialApp(
  title: 'savvy',
  theme: ThemeData(
    cardTheme: CardTheme(
      shape: RoundedRectangleBorder(
        borderRadius: const BorderRadius.all(
          Radius.circular(8.0),
        ),
      ),
    ),
    // ...

An Alternative Solution to the above上述问题的替代解决方案

Card(
  shape: RoundedRectangleBorder(
     borderRadius: BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20))),
  color: Colors.white,
  child: ...
)

You can use BorderRadius.only() to customize the corners you wish to manage.您可以使用 BorderRadius.only() 来自定义您希望管理的角。

A Better way to customise Card Radius ( Also other options ) is to set theme for Card globally.自定义卡片半径的更好方法(还有其他选项)是全局设置卡片主题。 So that you can use same style for Card througout the entire app.这样您就可以在整个应用程序中为 Card 使用相同的样式。 You can use this method for many other widget also.您也可以将此方法用于许多其他小部件。

For Card Theme you can use ThemeData.cardTheme对于卡片主题,您可以使用ThemeData.cardTheme

MaterialApp(
 title: 'MySampleApp',
 theme: ThemeData(
   cardTheme: CardTheme(
     shape: RoundedRectangleBorder(
       borderRadius: BorderRadius.all(
         Radius.circular(16.0),
       ),
     ),
   ),
// ............
// ............

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM