Skip to content

Instantly share code, notes, and snippets.

@eevajonnapanula
Created July 11, 2024 11:11
Show Gist options
  • Save eevajonnapanula/59eea746053bc80880672480cca0fe11 to your computer and use it in GitHub Desktop.
Save eevajonnapanula/59eea746053bc80880672480cca0fe11 to your computer and use it in GitHub Desktop.
// CardStack.kt
@Composable
fun CardStack(
modifier: Modifier = Modifier,
content: @Composable () -> Unit,
) {
Layout(
content,
modifier,
) { measurables, constraints ->
val placeables =
measurables.map { measurable ->
measurable.measure(constraints)
}
val height = if (placeables.isNotEmpty())
placeables.first().height +
(CardStack.EXTRA_PADDING * placeables.size)
else 0
val width = if (placeables.isNotEmpty())
placeables.first().width
else 0
layout(width = width, height = height) {
placeables.mapIndexed { index, placeable ->
placeable.place(
x = if (index.isEven())
0
else
CardStack.X_POSITION,
y = CardStack.Y_POSITION * index,
)
}
}
}
}
object CardStack {
const val EXTRA_PADDING = 10
const val Y_POSITION = 5
const val X_POSITION = 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment