Skip to main content

Card View and ListView of Cards using Flutter and Dart. Easy tutorial for Beginners.

Cards are material panel with slightly round corners. The cards make simple apps looks good and easy to use interface. Many apps you come daily across use here and their cards. And with the Flutter framework, we can easily make beautiful cards with absolutely no other dependencies. We will also cover cards in a list in this tutorial.
thumbnail


Contents

  1. Project setup
  2. Code
  3. Design
  4. Result

Project Setup

It would be recommended to create a new project and follow this tutorial and later implement it in your own project. But you can also use your own project. 
So use your own project or create a new project in Android Studio or Visual Studio Code and name it list_cards or you can use Command Prompt / Terminal to create a new project.

flutter create list_card

Code

Create a new file under the lib folder to keep our code separate and name it list_cards_screen.dart.
After that make a stateful class in the file and name it simply ListCardsScreen.
import 'package:flutter/material.dart';

class ListCardsScreen extends StatefulWidget {
  const ListCardsScreen({Key? key}) : super(key: key);

  @override
  _ListCardsScreenState createState() => _ListCardsScreenState();
}

class _ListCardsScreenState extends State<ListCardsScreen> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

In the return statement replace the Container widget with the Scaffold widget. Add appbar and body to the Scaffold widget and give some title(e.g.- 'Cards List') to your appbar. In the body of the Scaffold widget, we will use a ListView and we will Card as children of ListView. Your code should look as shown below.
appBar: AppBar(
  title: Text('Cards List'),
),
body: ListView(
  children: [],
),

Now let us make two Card widget. But before that, we will discuss the fields in the Card widget and what we are going to use here.
  1. color: Here colour of background the Card widget is given.
  2. child: Here we are going to specify what to place inside our Card widget. For our tutorial, I am going to use an image and a text heading.
  3. shadowColor: Here colour of the shadow of the Card widget is given.
  4. elevation: It takes a double value and simply specifies the length of the shadow. Its default value is 1.0.
  5. shape:  Here the shape of the border is given, whether you want a rounded or sharp corner for your Card widget. It takes the ShapeBorder type of value.
  6. borderOnForeground: It takes bool value and specifies whether to display border above or below the child widget. The default value is true.
  7. margin: This field asks how much margin to provide and accepts EdgeInsets type value. Its default value is EdgeInsets.all(4.0).
  8. clipBehaviour: Asks whether you want to clip your contents or not. The default value is true.
Let's start making our app.
We will use a Column in the child field of Card. Here we will use an Image widget and a Text widget.
Card(
child: Column(
children: [
Image.network(src),
Text(title),
],
),
)

Here is the data we are using now.
String src = 'https://cdn.pixabay.com/photo/2021/06/01/07/03/sparrow-6300790_960_720.jpg';
String title = 'Sparrow';
//https://pixabay.com/users/schauhi-2509795/

Copy and paste above the Widget build function.

In the main.dart file, replace the home field with ListCardsScreen
MaterialApp(
  title: 'List Cards Screen',
  theme: ThemeData(
    primarySwatch: Colors.blue,
  ),
  home: ListCardsScreen(),
);

Now run the app and see. It should look as shown below.
List Cards App First

Design

Now this apps works but we need to add some more data and design a better card. So lets make a new Card widget function separate from the ListCardsScreen class and and we will return Card widget.
This function will accept the image link and title of the image. Now your Card code should look like below.
Widget card(String image, String title) {
  return Card();
}

We will give the card a background color of light yellow and also give image padding and image ratio of 3:4. We will give title bold font with bigger font size. So its your choice completely how you want to design your card. Also I am going to give the card a more rounded corner.
Widget card(String image, String title, BuildContext context) {
  return Card(
    color: Colors.yellow[50],
    elevation: 8.0,
    margin: EdgeInsets.all(4.0),
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
    child: Column(
      children: [
        Padding(
          padding: EdgeInsets.all(16.0),
          child: Image.network(
            image,
            height: MediaQuery.of(context).size.width * (3 / 4),
            width: MediaQuery.of(context).size.width,
          ),
        ),
        Text(
          title,
          style: TextStyle(
            fontSize: 38.0,
            fontWeight: FontWeight.w700,
          ),
        ),
      ],
    ),
  );
}

Now in we are going to replace the ListView with ListView.builder. But before that, I have provided some data here which is needed for ListView of our app. So copy that from below.
List<String> image = [
  'https://cdn.pixabay.com/photo/2021/06/01/07/03/sparrow-6300790_960_720.jpg',
  'https://cdn.pixabay.com/photo/2017/10/20/10/58/elephant-2870777_960_720.jpg',
  'https://cdn.pixabay.com/photo/2014/09/08/17/32/humming-bird-439364_960_720.jpg',
  'https://cdn.pixabay.com/photo/2018/05/03/22/34/lion-3372720_960_720.jpg'];
List<String> title = ['Sparrow', 'Elephant', 'Humming Bird', 'Lion'];

Now come to the ListView part of the code and rename the ListView to ListView.builder.
body: ListView.builder(),

Now one required argument in this widget is itemBuilder which will build our card views. So add it to your ListView as shown.
ListView.builder(
itemBuilder: (BuildContext context, int index) {  },
),

Now in the curly braces, we have to return any widget and for this tutorial, we will return the custom card widget we made.
itemBuilder: (BuildContext context, int index) {
  return card(image[index], title[index], context);
},

We will also require the itemCount field in our ListView.builder so add it and we will provide the length of the data lists we are using(image list and title list). So our whole ListView.builder should look as follows.
ListView.builder(
  itemCount: image.length,
  itemBuilder: (BuildContext context, int index) {
    return card(image[index], title[index], context);
  },
),

Now run the app again and see the result.

Result

List Cards Final App


So we have successfully made an app with cards and ListView and by following this tutorial you can make many wonderful apps. Thank you for following this tutorial and if you have any doubts comment below or you can contact me from contact form.

Comments

  1. Such an awesome content you have posted I really liked your content keep posting and sharing.
    flutter app development companies

    ReplyDelete

Post a Comment

If you have any problem or doubt please comment.

Popular posts from this blog

Flutter | Material Banner Tutorial

In this tutorial, we will create and display Material Banner in Flutter . Material Banners are displayed at the top of the screen . User interaction is required to dismiss the banner . Material Banner Material Banner alerts the user about action and p rovides some actions for the user to take . In brief, it alerts the user about an issue and the user address the issue . The user should dismiss the banner to remove it from the screen else it will be active. Syntax  ScaffoldMessenger.of(context).showMaterialBanner(MaterialBanner( content: content, actions: actions)) Code Here is our starting class. It is a stateless class and we will display a banner when the Button is pressed. class SnackBarTutorial extends StatelessWidget { const SnackBarTutorial({Key ? key}) : super ( key: key); @ override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text( "Material Banner"

How to create Snackbars in Flutter | Snackbar Tutorial

In this article, we will create and display different types of SnackBars in Flutter.  SnackBars in Flutter SnackBars are used to briefly display some information or inform the user about an action. For instance, when a user deletes a mail or message you may want to inform the user about the action. Also, the user can undo the action he performed by the undo button in the Snackbar . Syntax For creating a SnackBar, we use the following code. ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: content)); Inside the content field of SnackBar, we will pass the content. Any content can be passed inside it, but in practice, small messages with or without a button. Example Simple SnackBar class SnackBarTutorial extends StatelessWidget {   const SnackBarTutorial({Key? key}) : super (key: key);   @override   Widget build(BuildContext context) {     return

Flutter: DatePicker Tutorial both with Material and Cupertino Style

Introduction DatePicker is very important when you want the user to pick his / her date of birth or something else. In Flutter, implementing DatePicker is very easy and we will implement DatePicker in both Android or Material style and Cupertino or IOS style.  Table of contents Approach Project Setup Material Style DatePicker Cupertino Style DatePicker Conclusion Approach Flutter has widgets for everything and even for DatePicker. DatePicker widget is loaded with all needed animation and colours. So implementation is very easy.  Project Setup No extra plugins are required for this project and you can continue with your existing project. Here is the starting code. class DatePickerTutorial extends StatefulWidget { const DatePickerTutorial({Key? key}) : super (key: key); @ override _DatePickerTutorialState createState() => _DatePickerTutorialState(); } class _DatePickerTutorialState extends State<DatePick