Skip to main content

Flutter: How to make Flip-Card animation in flutter. Flip card animation tutorial.

In this tutorial, we will learn to create Flip Card animation in Flutter. We will be using a package called flip_card for this tutorial. 

Contents:

  • Create a project
  • Import dependencies
  • Logic code
  • Design of app
  • Result

    Create a project

    Open  Android Studio or Visual Studio Code whichever is preferred and create a project and name simply flip_list_card or something else.
    You can use command prompt/terminal to create the project.
    flutter create flip_list_card
    cd flip_list_card
    
    You can also use your existing project.

    Import dependencies

    The only dependency required is flip_card dependency from pub.dev.
    You can search for it on pub.dev or click here.

    Open your pubspec.yaml file and in the dependencies section add
    flip_card: ^0.5.0
    

    After that click on Get Dependencies or in terminal write
    flutter pub get
    
    and press Enter key.

    Logic code

    Open your lib folder and create a new  file and call it card_screen.dart
    Make a stateful class in the current file and name it CardScreen so you can follow along easily.
    We will make two dummy lists with some string value and make list of it accordingly using ListView.builder.
    Let us make two lists and name them country and capital respectively. Write them as shown below.
    List<String> country = ["India", "USA", "Canada", "United Kingdom"];
    List<String> capital = ["New Delhi", "Washington DC", "Ottawa", "London"];
    

    Now let us come to build method.
    So the main thing that we are going to use in our app is FlipCard widget.
    FlipCard()
    
     
    In the FlipCard we have two required arguments and that is front and back.
    FlipCard(
      front: front,
      back: back,
    )
    

    In the front we are going to use the front view of our card that is the country name and in back side capital name. So to do this we are going to use Text widget.
    FlipCard(
      front:Text(country[index]),
      back: Text(capital[index]),
    )
    

    We are using a ListView.builder so in the itemBuilder field we are going to place our FlipCard widget.
    ListView.builder(
      itemCount: country.length,
      itemBuilder: (BuildContext context, int index) {
        return FlipCard(
          front: Text(country[index]),
          back: Text(capital[index]),
        );
      },
    )
    


    In the main method in MaterialApp home field call the CardScreen class.
    home: CardScreen(),
    

    For now build and run the app in your IDE or enter
    flutter run
    
    in the Terminal/Command Prompt.

    If followed your app should look like
    Flip Card app

    Design of app

    Now our app does not looks good so we are going to so we are going to edit our code.
    First lets set alignment of our ListView contents to center. On order to do that we are going to wrap our Text widget in Container widget both for front and back of FlipCard widget.
    Container(
      child: Text(country[index]),
    )
    

    We will add some decoration and some styles to our Text widget. But it will be simple. We are going to align our Text widget to center.
    Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height / 4,
      margin: EdgeInsets.all(24),
      decoration: BoxDecoration(
        color: Colors.purple,
        borderRadius: BorderRadius.circular(25.0),
      ),
      child: Align(
        alignment: Alignment.center,
        child: Text(
          text,
          textAlign: TextAlign.center,
          style: TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.w800,
            fontSize: 48,
          ),
        ),
      ),
    )
    

    To make the code simple we are going to separate the above widget and the whole CardScreen code should look like.
    import 'package:flip_card/flip_card.dart';
    import 'package:flutter/material.dart';
    
    class CardScreen extends StatefulWidget {
      @override
      _CardScreenState createState() => _CardScreenState();
    }
    
    class _CardScreenState extends State<CardScreen> {
      List<String> country = ["India", "USA", "Canada", "United Kingdom"];
      List<String> capital = ["New Delhi", "Washington DC", "Ottawa", "London"];
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("FLip Card"),
          ),
          body: ListView.builder(
            itemCount: country.length,
            itemBuilder: (BuildContext context, int index) {
              return FlipCard(
                front: card(country[index], context),
                back: card(capital[index], context),
              );
            },
          ),
        );
      }
    }
    
    Widget card(String text, BuildContext context) {
      return Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height / 4,
        margin: EdgeInsets.all(24),
        decoration: BoxDecoration(
          color: Colors.purple,
          borderRadius: BorderRadius.circular(25.0),
        ),
        child: Align(
          alignment: Alignment.center,
          child: Text(
            text,
            textAlign: TextAlign.center,
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.w800,
              fontSize: 48,
            ),
          ),
        ),
      );
    }
    

    Result

    The final view of our app should be like this.
    Final Flip Card App
    So we have completed our small tutorial but some topics are not yet discussed.
    There are some fields that are alignment, direction, speed, onFlip, onFlipDone and flipOnTouch.
    Let's discuss one by one:-
    1. alignment: This field sets the alignment of the card. By default the alignment is to center.
    2. direction: This field sets the flip direction of  card. By default the flip direction FlipDirection.HORIZONTAL.
    3. speed: This field sets the speed with which the card turns. The speed is in integer and by default 500 milliseconds.
    4. onFlip: This function is called when you touch on card.
    5. onFlipDone: This function is called when the flipping of card ends.
    6. flipOnTouch: This field sets whether you want to flip your card or not. This field takes a boolean field.
    Thank you for viewing my blog.

    Comments

    Popular posts from this blog

    Animated Navigation in Flutter

      Introduction By default, Flutter has no animation when navigating from one Screen to another Screen. But in this tutorial, we are going to learn how to animate the Screen Transition.   Introduction By default, Flutter has no animation when navigating from one Screen to another Screen. But in this tutorial, we are going to learn how to animate the Screen Transition. Table of Contents Project Setup Code Result Project Setup Before directly applying to your existing project, practice the code in some example projects. For this tutorial, we are going to name our project screen_animation_tutorial. Open in VSCode or Android Studio. After that, we will make two Screens. The first Screen will be the Starting screen and the next Screen will be the Final Screen. So go ahead and create two dart files under the lib folder. The main.dart file code should look like this Dart import 'package:flutter/material.dart'; import 'package:screen_animation_tutorial/start_screen.dart'; void ...

    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) { ...

    Flutter : Screen Animation tutorial

    In this tutorial, we will learn how to perform animations while navigating from one screen to another. We will not require any other dependency to be installed. We will demonstrate the following example. So let's start. Approach Instead of using the default MaterialPageRoute, we will use the PageRouteBuilder. PageRouteBuilder provides all the functionalities like duration, type of animation. Syntax The syntax for PageRouteBuilder is as follows. PageRouteBuilder( transitionDuration: const Duration( seconds: 1 ), pageBuilder: (context, animation, secondaryAnimation) { return const SecondScreen(); }, ), The parameters and fields are self-explanatory. Code Let us code our First Screen of the app. class FirstScreen extends StatelessWidget { const FirstScreen({Key ? key}) : super ( key: key); @ override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text( "First...