What is Card in Flutter?
Card is a Material Design widget that represents a piece of paper with slightly rounded corners and a shadow (elevation). It's used to group related information – like a product preview, a user profile summary, or an article teaser – in a visually distinct container. Cards are a fundamental building block of modern UIs.
Basic Usage
Using Card is straightforward: just wrap any widget (often a Column, ListTile, or Image) with a Card. By default, the Card has a slight elevation and rounded corners.
You can place multiple widgets inside by using a Column or a ListTile.
Key Properties
- child: The widget inside the card (usually a
Column,ListTile,Image, orContainer). - elevation: Controls the size of the drop shadow. Higher values make the card appear higher above the surface.
- shape: Defines the card's border shape. Common shapes are
RoundedRectangleBorderandCircleBorder. - color: The background color of the card.
- margin: Empty space around the card (outside the card's border).
- shadowColor: The color of the drop shadow.
- borderOnForeground: Whether to paint the border on top of the child.
- clipBehavior: How to clip the card if its content overflows (default
Clip.none).
Elevation and Shadow
The elevation property gives the card a 3D feel. The default elevation is 1.0, but you can increase it to make the card stand out more.
Shape Customization
Use the shape property to change the card's border radius, make it a circle, or add a custom border.
Adding Margins and Internal Padding
To separate a card from surrounding widgets, use margin. To add space between the card's border and its child, wrap the child with Padding.
Cards with ListTile
A very common pattern is to put a ListTile inside a Card to create a consistent, tappable list item with an icon, title, and subtitle.
Cards in a ListView
Cards are often used inside a ListView to create a scrollable list of items.
InkWell and Gestures
Card itself does not have an onTap property. To make a card tappable and show a ripple effect, wrap its child (or the whole card) with an InkWell.
Common Use Cases
- Profile cards: Show user avatar, name, and a short bio.
- Product cards: Display an image, title, price, and a buy button.
- Article previews: Include a headline, a summary, and an image.
- Dashboard tiles: Represent different sections of an app with icons and numbers.
- Media galleries: Combine an image with a caption.
Common Mistakes Beginners Make
- Forgetting to give the card a child: A Card with no child will be invisible (zero size).
- Over‑elevation: Using very high elevations (e.g., 24) can look unnatural; stick to 1–6 for typical cards.
- Not handling overflow: If the child is too large, the card might overflow. Use
clipBehavior: Clip.antiAliasto clip the content. - Putting too much content inside a single card: Cards should group related information, but avoid making them too tall.
- Ignoring margin and padding: Without margin, cards stick together; without padding, content touches the edges.
- Misusing shape: Using
CircleBorderwith a rectangular child can cause unexpected clipping.
Key Points to Remember
- Card is a Material Design container with rounded corners and a shadow.
- Use
elevationto control the shadow depth. - Customize the border with
shape. - Add external spacing with
margin, internal spacing withPaddinginside the child. - Cards work great with
ListTileand insideListView. - To add tap handling, wrap the child with
InkWell.
Common Interview Questions
- How do you change the corner radius of a Card?
- What is the difference between
marginandpaddingin the context of a Card? - How would you make a Card tappable and show a ripple effect?
- Can you use a
CircleAvatarinside a Card? Give an example. - What happens if you set
elevation: 0on a Card? - How do you create a card that spans the full width of its parent but has a fixed height?
- Why might you need to set
clipBehavioron a Card?