Last active
June 19, 2022 17:47
-
-
Save Skyyo/f0d5364a8e0f63b60e98f5be4b31af2b to your computer and use it in GitHub Desktop.
This file contains 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
@Composable | |
fun ExpandableCard( | |
card: ExpandableCardModel, | |
onCardArrowClick: () -> Unit, | |
expanded: Boolean, | |
) { | |
val transitionState = remember { | |
MutableTransitionState(expanded).apply { | |
targetState = !expanded | |
} | |
} | |
val transition = updateTransition(transitionState) | |
val cardBgColor by transition.animateColor({ | |
tween(durationMillis = EXPAND_ANIMATION_DURATION) | |
}) { | |
if (expanded) cardExpandedBackgroundColor else cardCollapsedBackgroundColor | |
} | |
val cardPaddingHorizontal by transition.animateDp({ | |
tween(durationMillis = EXPAND_ANIMATION_DURATION) | |
}) { | |
if (expanded) 48.dp else 24.dp | |
} | |
val cardElevation by transition.animateDp({ | |
tween(durationMillis = EXPAND_ANIMATION_DURATION) | |
}) { | |
if (expanded) 24.dp else 4.dp | |
} | |
val cardRoundedCorners by transition.animateDp({ | |
tween( | |
durationMillis = EXPAND_ANIMATION_DURATION, | |
easing = FastOutSlowInEasing | |
) | |
}) { | |
if (expanded) 0.dp else 16.dp | |
} | |
val arrowRotationDegree by transition.animateFloat({ | |
tween(durationMillis = EXPAND_ANIMATION_DURATION) | |
}) { | |
if (expanded) 0f else 180f | |
} | |
Card( | |
backgroundColor = cardBgColor, | |
contentColor = contentColour, | |
elevation = cardElevation, | |
shape = RoundedCornerShape(cardRoundedCorners), | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding( | |
horizontal = cardPaddingHorizontal, | |
vertical = 8.dp | |
) | |
) { | |
Column { | |
Box { | |
CardArrow( | |
degrees = arrowRotationDegree, | |
onClick = onCardArrowClick | |
) | |
CardTitle(title = card.title) | |
} | |
ExpandableContent(visible = expanded, initialVisibility = expanded) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment