LazyCardStack

Jetpack compose tinder like card stack. Library supports Compose Multiplatform.
Installing
Android
Add the dependencies:
implementation("io.github.hukumister:lazycardstack-android:0.0.2")
Multi-platform
Add the dependency to the common source-set:
kotlin {
sourceSets {
commonMain {
dependencies {
implementation("io.github.hukumister:lazycardstack:0.0.2")
}
}
}
}
Demo
Wait a bit, the gif for the demo is quite large and may take a long time to load:
How to use?
The library has an API similar to LazyColumn.
val cardStackState = rememberLazyCardStackState()
LazyCardStack(
modifier = Modifier
.padding(24.dp)
.fillMaxSize(),
state = cardStackState
) {
items(
items = list.value,
key = { it.hashCode() }
) { text ->
TextCard(
modifier = Modifier
.background(Color.White),
text = text
)
}
item(
key = { "loading" }
) {
Text(
modifier = Modifier
.padding(16.dp)
.fillMaxSize(),
text = "Loading..."
)
}
}
The LazyCardStackState gives you access to the card's offset so that you can create
advanced animations according to the amount of swiping done.
How to swipe programmatically?
val state = rememberLazyCardStackState()
val scope = rememberCoroutineScope()
Button(
onClick = {
scope.launch { cardStackState.animateToNext(SwipeDirection.Right) }
}
) {
Text("Like")
}
The animateToNext() suspend function will return, as soon as the swipe animation is finished.
You can also specify specific animation settings for swipe:
val state = rememberLazyCardStackState()
val scope = rememberCoroutineScope()
scope.launch {
cardStackState.animateToNext(
direction = SwipeDirection.Right,
animation = tween(500)
)
}
How to detect that a card has been swiped away?
LazyCardStack has a callback that will be called after swiping the card:
LazyCardStack(
onSwipedItem = { index, direction ->
},
state = cardStackState
)
Can I return previous card?
Indeed, LazyCardStackState gives you method to return previous card:
val state = rememberLazyCardStackState()
val scope = rememberCoroutineScope()
scope.launch {
cardStackState.animateToBack(SwipeDirection.Up)
}
you must specify the direction from which the animation of returning the card will occur.
Sample
More usage examples can be found in sample.
License