Skip to content

Instantly share code, notes, and snippets.

@qamarelsafadi
Created September 21, 2023 09:00
Show Gist options
  • Save qamarelsafadi/2c5b6b598b67b5e0775d488d8e07693e to your computer and use it in GitHub Desktop.
Save qamarelsafadi/2c5b6b598b67b5e0775d488d8e07693e to your computer and use it in GitHub Desktop.
@Composable
fun ExpandableCard(
isOpenByDefault: Boolean = false,
title: String,
hasItem: Boolean = true,
padding: Dp = 15.dp,
onClick: () -> Unit,
content: @Composable () -> Unit
) {
var expanded by remember { mutableStateOf(isOpenByDefault) }
val rotation by animateFloatAsState(
targetValue =
if (expanded.not())
0f
else 180f, label = ""
)
Card(
shape = RoundedCornerShape(5.dp),
elevation = CardDefaults.elevatedCardElevation(0.dp),
modifier = Modifier
.fillMaxWidth()
.defaultMinSize(minHeight = 35.dp)
.animateContentSize()
.noRippleClickable {
if (hasItem)
expanded = !expanded
else onClick()
},
colors = CardDefaults.cardColors(containerColor = BgColor)
) {
Box(
modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
Column(
modifier = Modifier.padding(horizontal = padding),
verticalArrangement = Arrangement.Center,
) {
Row(
Modifier
.fillMaxWidth()
.defaultMinSize(minHeight = 35.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = title,
style = if (expanded) boldStyle else regularStyle,
fontSize = 13.sp,
color = ColorPrimaryDark
)
if (hasItem) {
Spacer(modifier = Modifier.weight(1f))
Image(
painter = R.drawable.downarrow.getResource(),
contentDescription = "down arrow",
modifier = Modifier.rotate(rotation)
)
}
}
if (expanded) {
Spacer(modifier = Modifier.height(9.dp))
content()
Spacer(modifier = Modifier.height(9.dp))
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment