Git Product home page Git Product logo

compose-tinder-card's Introduction

Compose Tinder Card

Banner image

A Jetpack Compose Modifier that enables Tinder-like card gestures.

Demo

Click the play button to see the Modifier.swipeableCard() in action:

demo.mp4

How to Install via Gradle

Add the following code in your app/build.gradle file:

repositories {
...
mavenCentral()
}

dependencies {
    implementation 'com.alexstyl.swipeablecard:swipeablecard:0.0.1'
}

How to use

Add the Modifier.swipeableCard() into your @Composable function to enable Tinder-like card gestures:

val state = rememberSwipeableCardState()

Box(
    modifier = Modifier
        .fillMaxSize()
        .swipableCard(
            state = state,
            onSwiped = { direction ->
                println("The card was swiped to $direction")
            },
            onSwipeCancel = {
                println("The swiping was cancelled")
            }
        )
) {
  // contents ...
}

The SwipeableCardState gives you access to the card's offset so that you can create advanced animations according to the amount of swiping done.

The swipedDirection gives you the direction the card has been fully swiped.

How to swipe programmatically

Use the SwipeableCardState to swipe to a specific direction without a gesture:

val state = rememberSwipeableCardState()

// pass the state in your Modifier.swipeableCard(state)

val scope = rememberCoroutineScope()
Button(
    onClick = {
        scope.launch {
            state.swipe(Direction.Right)
        }
    }
) {
    Text("Like")
}

The swipe() suspend function will return, as soon as the swipe animation is finished.

How to detect that a card has been swiped away

Use the SwipeableCardState.swipedDirection. You may want to combine it with a LaunchedEffect() in order to receive a callback when your card is swiped away (using a gesture or programmatically):

LaunchedEffect(state.swipedDirection){
    if(state.swipedDirection!=null) {
        println("The card was swiped to ${state.swipedDirection!!}")
    }
}

Demo app included

Checkout the app's MainActivity.kt to see a fully functioning example of usage.

License

Apache 2.0. See the LICENSE file for details.

Author

Made by Alex Styl. Follow me on Twitter for updates.

Inspired by Twypper and react-tinder-card

compose-tinder-card's People

Contributors

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