Skip to content

Instantly share code, notes, and snippets.

@aritra-tech
Created May 18, 2024 16:30
Show Gist options
  • Save aritra-tech/4c83e803b2bc384fecce5eeab2e08b6b to your computer and use it in GitHub Desktop.
Save aritra-tech/4c83e803b2bc384fecce5eeab2e08b6b to your computer and use it in GitHub Desktop.
@OptIn(ExperimentalLayoutApi::class)
@Composable
fun ContextualFlowRowLayout(modifier: Modifier = Modifier) {
val techStacks = listOf(
"Kotlin",
"Jetpack Compose",
"Java",
"Rust",
"Go",
"React",
"C#",
"KMP",
"Node.js",
"JavaScript",
"HTML",
"CSS",
"Express.js",
"C",
"C++",
"Docker",
"Kubernetes"
)
var maxLines by remember { mutableIntStateOf(2) }
val moreOrCollapseIndicator = @Composable { scope: ContextualFlowRowOverflowScope ->
val remainingItems = techStacks.size - scope.shownItemCount
ChipItem(if (remainingItems == 0) "Less" else "+$remainingItems", onClick = {
if (remainingItems == 0) {
maxLines = 2
} else {
maxLines += 5
}
})
}
ContextualFlowRow(
modifier = Modifier
.safeDrawingPadding()
.fillMaxWidth(1f)
.padding(16.dp)
.wrapContentHeight(align = Alignment.Top)
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(4.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
maxLines = maxLines,
overflow = ContextualFlowRowOverflow.expandOrCollapseIndicator(
minRowsToShowCollapse = 4,
expandIndicator = moreOrCollapseIndicator,
collapseIndicator = moreOrCollapseIndicator
),
itemCount = techStacks.size
) { index ->
ChipItem(techStacks[index])
}
}
@Composable
fun ChipItem(text: String, onClick: (() -> Unit)? = null) {
Box(
modifier = Modifier
.clip(RoundedCornerShape(50))
.background(Color.Black.copy(0.7f))
.clickable { onClick?.invoke() }
.padding(horizontal = 12.dp, vertical = 8.dp),
contentAlignment = Alignment.Center
) {
Text(text = text, color = Color.White)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment