Flutter – Tinder like swipe cards

Flutter – Tinder like swipe cards

Implement Tinder like nice swipe card feature using flutter is quite easy because of the powerful widget provided by Flutter sdk. In this article, I’m gone a show you how to implement swipe card function in Flutter using several known widgets.

You may know the everything in the flutter is some kind of widget. The Draggable widget will give a ability to drag the child widget and we can control what we need to do when we drag the widget.

Solution breakdown

First of all you need to think about the widget Structure. This is the widget structure you need to follow.

Create a Card widget using the card class.

Wrap the Card widget inside the Positionedwidget.

Positioned those card widgets inside the Stackwidget.

Wrap the card inside Draggable widget to give the ability to drag and drop.

Implementation

Let’s think about what are the properties need to change for each card.

First, we need to change the colour and the position from the top of the stack widget. If we don’t change the position from the top, each card will be stack the top left corner and we cannot see each card separately. Because that’s the default behaviour of the stack widget.

So Let’s start with Creating MatchCard Class. You can set the color and the position for each card using this MatchCard class.

The _removeCard method is not defined yet and I will define it in the end. You can see some interesting attributes inside Draggable widget. Let’s discuss what are those attributes.

feedback – We must provide some kind of Widget for this. This is the widget which is shown when a drag event is happening. In this scenario, we need to show the same actual card widget when dragging.

childWhenDragging – The widget to display instead of [child] when one or more drags are underway. For that, you can give an empty container. Then It will don’t show any child widget when dragging.

onDragEnd – This call when the drag ends. We can remove the card when drag get completed.

Next, we need to call _getMatchCard() inside the initState method and assign those List of the widget to the List<Widget> cardList.

initState – This is the first method called when the widget is created (after the class constructor, of course.)

https://flutterbyexample.com/stateful-widget-lifecycle/

Finally, you need to remove the card after each drag gets completed. For that, I am gone to define a method called _removeCard. Inside that method I am gone to call setState and inside that you can remove each card widget.