Search code examples
fluttergradientcard

How to add a gradient to a card in Flutter?


I'm trying to achieve this design:

enter image description here

I also want to use a Card widget provided by Flutter. It comes with some nice theming support that I'd like to use (CardTheme).

So don't know how to give a LinearGradient to a Card. Here's what I tried to do with combining a Card with Container:

Card(
    child: Container(
      margin: EdgeInsets.all(5),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [
            cardBorderColor,
            Theme.of(context).colorScheme.surface,
          ],
          stops: [0, 0.8],
        ),
      ),
      child: ...

enter image description here

As you can see, the border radius of the card is respected when positioning the Container.


Solution

  • Setting the clipBehavior property of the Card to Clip.antiAlias achieves the desired outcome:

    Card(
        clipBehavior: Clip.antiAlias, // <-- this did the trick
        margin: EdgeInsets.all(5),
        child: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [
                cardBorderColor,
                Theme.of(context).colorScheme.surface,
              ],
              stops: [0, 0.8],
            ),
          ),
          child: ...