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: 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...

Flutter: DraggableScrollableSheet widget Tutorial

Introduction In this tutorial, we are going to learn about the DraggableScrollableSheet widget. It is a widget that responds to user gestures. When the user drags from the bottom of the screen in the upward direction, the widget opens up and when scrolls down the widget close. The final app will look as follows  Final App DraggableScrollableSheet It is a widget that scrolls up and down of the viewport or screen according to the user's screen gestures. This widget becomes handy when you want to give some extra details which are lengthy content but don't want to navigate to a new screen. You might have come across many apps that implement the same thing. So let us learn to implement DraggableScrollableSheet in our own app. Approach The approach of our project is very simple. In our app, we are going to display some names of countries with the help of the  ListView.builder  and Li...

Flutter : Image Picker Tutorial | Pick single image from Gallery

Introduction In this tutorial, we will create an image picker and display the content on the screen . I mage Picker picks a file from the Storage or Camera and stores it in an XFile object. Implementation Install dependency We will first need the image_picker dependency . To install it, add the following dependency to pubspec.yaml file. image_picker: ^0.8.4+3 Then click on Get Packages in your IDE or run the following command on your Terminal / Command Prompt. flutter pub get Example Import the dependency in your dart file. import 'package:image_picker/image_picker.dart' ; Syntax Image picking is a future task . So we need to await the image picking . Here is the syntax. await _picker.pickImage( source : ImageSource.gallery); Here we have provided the source from the gallery . We can also provide the source as a Camera using the following syntax. await _picker.pickImage( source : Ima...