How to Create Custom Cards in Flutter?

Learn to create custom cards in Flutter, enhancing your app’s UI with visually appealing & functional cards tailored to your requirements.

Follow the official documentation to set up Flutter: Flutter Installation Guide.

 How to Create Custom Cards in Flutter?

Create Custom Cards in Flutter (2 Examples)

In this tutorial, we are going to learn two examples of code to implement the card design in Flutter with output.

Custom Card with Title, Description, Button

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Custom Card Example'),
        ),
        body: Center(
          child: Container(
            width: 300, // Adjust width as needed
            child: Card(
              elevation: 4, // Add shadow
              child: Padding(
                padding: EdgeInsets.all(16.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Text(
                      'Custom Card Title',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 18,
                      ),
                    ),
                    SizedBox(height: 8),
                    Text(
                      'This is a custom card example.',
                      style: TextStyle(fontSize: 16),
                    ),
                    SizedBox(height: 16),
                    ElevatedButton(
                      onPressed: () {
                        print('Button Clicked!');
                      },
                      child: Text('Click Me'),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
  1. The MaterialApp includes a Scaffold widget, which represents the main app layout structure. It has an AppBar with the title “Custom Card Example”.
  2. The Scaffold body contains a Center widget, which centers its child widget (in this case, a Container) horizontally and vertically.
  3. The Container has a fixed width of 300 pixels (you can adjust this value as needed). It contains a Card widget.
  4. The Card widget has an elevation of 4, which adds a shadow effect to the card, making it appear raised from the background.
  5. Inside the Card, there is a Padding widget with EdgeInsets.all(16.0), which adds a 16-pixel padding around the content of the card.
  6. The content of the card is a Column widget with mainAxisSize: MainAxisSize.min, which means the column will only take up the minimum space required by its children.
  7. The Column contains the following children:
    • Text widget with the title “Custom Card Title”. It has a bold font weight and a font size of 18.
    • SizedBox with a height of 8 pixels, which adds vertical spacing.
    • Another Text widget with the text “This is a custom card example.” and a font size of 16.
    • Another SizedBox with a height of 16 pixels, which adds more vertical spacing.
    • An ElevatedButton with the text “Click Me”. When the button is pressed, it prints “Button Clicked!” to the console.

This code creates a centered custom card with a title, a description, and a button. The card has a fixed width of 300 pixels and a shadow effect thanks to the Card widget’s elevation property. The content of the card is vertically centered using the Column widget with mainAxisSize: MainAxisSize.min.

Output

Custom Card with Title, Description, Button

Create Custom Cards in Flutter with Title, Description, Image, Button

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Custom Cards'),
        ),
        body: Padding(
          padding: EdgeInsets.all(16.0),
          child: FractionallySizedBox(
            widthFactor: 1,
            child: CustomCard(),
          ),
        ),
      ),
    );
  }
}

class CustomCard extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 4.0,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16.0),
      ),
      child: Container(
        child: SingleChildScrollView( // Wrap with SingleChildScrollView
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Image.network(
                'https://picsum.photos/400/200',
                fit: BoxFit.cover,
                width: double.infinity,
              ),
              Padding(
                padding: EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Card Title',
                      style: TextStyle(
                        fontSize: 20.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 8.0),
                    Text(
                      'This is a custom card with a title, description, an image, and a button.',
                      style: TextStyle(fontSize: 16.0),
                    ),
                    SizedBox(height: 16.0),
                    SizedBox(
                      width: double.infinity,
                      child: ElevatedButton(
                        onPressed: () {
                          // Handle button press
                        },
                      child: Text('Button'),
                      style: ElevatedButton.styleFrom(
                        padding: EdgeInsets.symmetric(vertical: 16.0),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8.0),
                        ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

This is the CustomCard widget, which is a StatelessWidget. It returns a Card widget with an elevation of 4.0 and rounded corners with a border radius of 16.0. The Card contains a Container with a SingleChildScrollView, which allows the content to scroll vertically if it exceeds the available height.

Inside the SingleChildScrollView, there is a Column with the following children:

  1. An Image.network widget that loads an image from the provided URL. The image has a fit of BoxFit.cover and a width of double.infinity, which means it will take up the full available width.
  2. Padding widget that adds spacing around its child.
  3. Another Column inside the Padding with the following children:
    • Text widget for the card title with a larger font size and bold weight.
    • SizedBox for vertical spacing.
    • Another Text widget for the card description.
    • Another SizedBox for vertical spacing.
    • SizedBox with width: double.infinity that contains an ElevatedButton. The button has a custom style applied using ElevatedButton.styleFrom, which sets the padding and rounded corners for the button.

The purpose of this code is to create a custom card with an image, title, description, and button. The card has rounded corners and a drop shadow and the content is scrollable if it exceeds the available height.

The FractionallySizedBox in the body of the Scaffold ensures that the card takes up the full available width, while the Padding widget adds some spacing around the card.

Also, Want to learn How to Create Enum in Flutter?

Output

Custom Card with Title, Description, Image, Button

At the End

Furthermore, you can further customize the appearance and behavior of the card and its components by modifying the styles, spacing, and other properties as needed.

Share:
Ambika Dulal

Ambika Dulal is a Flutter Developer from Nepal who is passionate about building beautiful and user-friendly apps. She is always looking for new challenges and is eager to learn new things. She is also a strong believer in giving back to the community and is always willing to help others.

Leave a Comment

AO Logo

App Override is a leading mobile app development company based in Kathmandu, Nepal. Specializing in both Android and iOS app development, the company has established a strong reputation for delivering high-quality and innovative mobile solutions to clients across a range of industries.

Services

UI/UX Design

Custom App Development

Mobile Strategy Consulting

App Testing and Quality Assurance

App Maintenance and Support

App Marketing and Promotion

Contact

App Override

New Plaza, Kathmandu