Created
February 13, 2025 19:16
-
-
Save cavin-macwan/f0371286c38568faa3bfcad0f7f43773 to your computer and use it in GitHub Desktop.
ExpandableCard is a custom composable card that expands and collapses when clicked. It features smooth animations, a rotating dropdown icon, and dynamic content visibility, making it perfect for displaying information in a structured way.
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
/** | |
* Use it like this: | |
* | |
* ``` | |
* @Composable | |
* fun ExpandableScreen() { | |
* Box( | |
* modifier = Modifier | |
* .padding(16.dp) | |
* .fillMaxSize(), | |
* contentAlignment = Alignment.Center | |
* ) { | |
* ExpandableCard( | |
* title = "Expandable Card Demo", | |
* content = "This is some dummy content" | |
* ) | |
* } | |
* } | |
* ``` | |
*/ | |
@Composable | |
fun ExpandableCard( | |
title: String, | |
content: String | |
) { | |
var expanded by remember { mutableStateOf(false) } | |
val rotationState by animateFloatAsState( | |
targetValue = if (expanded) 180f else 0f | |
) | |
Card( | |
modifier = Modifier | |
.fillMaxWidth() | |
.animateContentSize( | |
animationSpec = spring( | |
dampingRatio = Spring.DampingRatioMediumBouncy, | |
stiffness = Spring.StiffnessLow | |
) | |
) | |
) { | |
Column( | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(12.dp) | |
) { | |
Row( | |
horizontalArrangement = Arrangement.SpaceBetween, | |
verticalAlignment = Alignment.CenterVertically, | |
modifier = Modifier | |
.fillMaxWidth() | |
.clickable { expanded = !expanded } | |
) { | |
Text( | |
text = title, | |
style = MaterialTheme.typography.titleMedium | |
) | |
Icon( | |
imageVector = Icons.Default.ArrowDropDown, | |
contentDescription = "Expand", | |
modifier = Modifier.rotate(rotationState) | |
) | |
} | |
if (expanded) { | |
Spacer(modifier = Modifier.height(8.dp)) | |
Text( | |
text = content, | |
style = MaterialTheme.typography.bodyMedium | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment