Skip to content

Instantly share code, notes, and snippets.

@cavin-macwan
Created February 13, 2025 19:16
Show Gist options
  • Save cavin-macwan/f0371286c38568faa3bfcad0f7f43773 to your computer and use it in GitHub Desktop.
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.
/**
* 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