Last active
December 13, 2025 06:34
-
-
Save fvilarino/92d03574d21755739ce178c5178393ec to your computer and use it in GitHub Desktop.
Flip Card final
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum class CardFace(val angle: Float) { | |
| Front(0f) { | |
| override val next: CardFace | |
| get() = Back | |
| }, | |
| Back(180f) { | |
| override val next: CardFace | |
| get() = Front | |
| }; | |
| abstract val next: CardFace | |
| } | |
| enum class RotationAxis { | |
| AxisX, | |
| AxisY, | |
| } | |
| @ExperimentalMaterialApi | |
| @Composable | |
| fun FlipCard( | |
| cardFace: CardFace, | |
| onClick: (CardFace) -> Unit, | |
| modifier: Modifier = Modifier, | |
| axis: RotationAxis = RotationAxis.AxisY, | |
| back: @Composable () -> Unit = {}, | |
| front: @Composable () -> Unit = {}, | |
| ) { | |
| val rotation = animateFloatAsState( | |
| targetValue = cardFace.angle, | |
| animationSpec = tween( | |
| durationMillis = 400, | |
| easing = FastOutSlowInEasing, | |
| ) | |
| ) | |
| Card( | |
| onClick = { onClick(cardFace) }, | |
| modifier = modifier | |
| .graphicsLayer { | |
| if (axis == RotationAxis.AxisX) { | |
| rotationX = rotation.value | |
| } else { | |
| rotationY = rotation.value | |
| } | |
| cameraDistance = 12f * density | |
| }, | |
| ) { | |
| if (rotation.value <= 90f) { | |
| Box( | |
| Modifier.fillMaxSize() | |
| ) { | |
| front() | |
| } | |
| } else { | |
| Box( | |
| Modifier | |
| .fillMaxSize() | |
| .graphicsLayer { | |
| if (axis == RotationAxis.AxisX) { | |
| rotationX = 180f | |
| } else { | |
| rotationY = 180f | |
| } | |
| }, | |
| ) { | |
| back() | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment