Created
September 21, 2023 09:00
-
-
Save qamarelsafadi/2c5b6b598b67b5e0775d488d8e07693e 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( | |
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