Skip to content

Commit

Permalink
Switch CardsSection type and started writing the README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivaskuu committed May 6, 2018
1 parent 15540ac commit 5f9f956
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
# tinder_cards
# Tinder cards

A new Flutter project.
Hi! After showcasing my app (Focus for Reddit client), people asked me how did I do the tinder like cards swipe (posts media are shown as a stack of 3 swipable cards) and if I could make tutorial or open-source it.

## Getting Started
And I did it! Here it is. I've created a Tinder like user interface (not working, that's not the point).

I've found 2 ways of doing this (there may be way more ways)
[ ] Using Draggable
[ ] Using GestureDetector and Alignment (what I use for my app)

# Draggable
This technique uses the already implemented drag system in Flutter. It's also pretty easy to set up:
- Stack
- Row with DragTargets
- IgnorePointer with ProfileCard (IgnorePointer so the top card Draggable touches the DragTarget)
- IgnorePointer with ProfileCard
- Draggable with ProfileCard

How does this work? Well, when the Draggable is dragged by the user, if there's a DragTest widget under the finger, it will execute the DragTest onWillAccept function, or onAccept if the card is released over. Also, when released, it will return to the default position, then call changeCardsOrder() which moves the cards by 1 and creates a new ProfileCard for the last card.

The problem with this technique is that you can't get the Draggable position while it is being swiped. So when released it will pop to the default position, and the return movement can't be animated (or going off the screen like in my app). Another problem (for me) is that you can't update the Draggable while it is swiped. It stays the same as when the user started dragging, and setState() takes effect only after being released. So, no way to slightly rotate the card while it is moving (which was very important for me).

# Gesture detector and Alignment
As cards are placed in a Stack, each of them needs an Align widget to specify it's position in the stack. In this technique, a DragTarget is placed over the Stack.
The user thinks he is swiping the front card but in reality he is just triggering the onPanUpdate() function of the GestureDetector. The amount the finger moves each frame is added to the Alignment of the front card, modified by a speed constant (otherwise the card would go too fast or too slow).
The card rotation is then calculated based on the front card x alignment. On finger release, if the card alignment.x is bigger than a certain value, the swipe is considered valid and changeCardsOrder() is called.
- Stack
- ProfileCard
- ProfileCard
- ProfileCard
- GestureDetector

For help getting started with Flutter, view our online
[documentation](https://flutter.io/).
[documentation](https://flutter.io/).
12 changes: 10 additions & 2 deletions lib/swipe_feed_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'cards_section_alignment.dart';
import 'cards_section_draggable.dart';

class SwipeFeedPage extends StatefulWidget
{
Expand All @@ -9,6 +10,8 @@ class SwipeFeedPage extends StatefulWidget

class _SwipeFeedPageState extends State<SwipeFeedPage>
{
bool showAlignmentCards = false;

@override
Widget build(BuildContext context)
{
Expand All @@ -24,7 +27,12 @@ class _SwipeFeedPageState extends State<SwipeFeedPage>
onPressed: () {},
icon: new Icon(Icons.settings, color: Colors.grey)
),
title: new Text('tinder', style: new TextStyle(color: Colors.red, fontSize: 22.0, fontWeight: FontWeight.w700)),
title: new Switch
(
onChanged: (bool newValue) => setState(() => showAlignmentCards = newValue),
value: showAlignmentCards,
activeColor: Colors.red,
),
actions: <Widget>
[
new IconButton
Expand All @@ -39,7 +47,7 @@ class _SwipeFeedPageState extends State<SwipeFeedPage>
(
children: <Widget>
[
new CardsSectionAlignment(),
showAlignmentCards ? new CardsSectionAlignment() : new CardsSectionDraggable(),
buttonsRow()
],
),
Expand Down

0 comments on commit 5f9f956

Please sign in to comment.