GithubHelp home page GithubHelp logo

singh-prabh / tinder_cards Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ivaskuu/tinder_cards

0.0 0.0 0.0 1.15 MB

Tinder like cards swipe effect with Flutter.

Java 2.08% Objective-C 4.26% Dart 93.66%

tinder_cards's Introduction

Tinder cards

Hi! After showcasing Focus for Reddit, the app I am working on, 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:

Focus for Reddit preview

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

Focus for Reddit preview

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)

P.S: Use the appBar switch to go from Draggable cards section to GestureDetector and Alignment cards section.

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

How animation works

// TODO


For help getting started with Flutter, view our online documentation.

tinder_cards's People

Contributors

ivaskuu avatar jillevdw avatar raacker avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.