Skip to content

Commit bf83206

Browse files
authored
Adding samples for ContextualFlowRow (#234)
* Add snippets for ContextualFlowRow * Apply Spotless * Update flow snippets * Apply Spotless --------- Co-authored-by: riggaroo <[email protected]>
1 parent 8242f9e commit bf83206

File tree

2 files changed

+128
-7
lines changed

2 files changed

+128
-7
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/layouts/FlowLayoutSnippets.kt

Lines changed: 127 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,40 @@ import androidx.compose.foundation.background
2121
import androidx.compose.foundation.border
2222
import androidx.compose.foundation.layout.Arrangement
2323
import androidx.compose.foundation.layout.Box
24+
import androidx.compose.foundation.layout.ContextualFlowRow
25+
import androidx.compose.foundation.layout.ContextualFlowRowOverflow
26+
import androidx.compose.foundation.layout.ContextualFlowRowOverflowScope
2427
import androidx.compose.foundation.layout.ExperimentalLayoutApi
28+
import androidx.compose.foundation.layout.FlowColumn
2529
import androidx.compose.foundation.layout.FlowRow
2630
import androidx.compose.foundation.layout.Spacer
31+
import androidx.compose.foundation.layout.fillMaxHeight
2732
import androidx.compose.foundation.layout.fillMaxSize
2833
import androidx.compose.foundation.layout.fillMaxWidth
2934
import androidx.compose.foundation.layout.height
3035
import androidx.compose.foundation.layout.padding
36+
import androidx.compose.foundation.layout.safeDrawingPadding
3137
import androidx.compose.foundation.layout.size
3238
import androidx.compose.foundation.layout.width
39+
import androidx.compose.foundation.layout.wrapContentHeight
40+
import androidx.compose.foundation.rememberScrollState
3341
import androidx.compose.foundation.shape.RoundedCornerShape
42+
import androidx.compose.foundation.verticalScroll
3443
import androidx.compose.material.Chip
3544
import androidx.compose.material.ExperimentalMaterialApi
36-
import androidx.compose.material.Text
45+
import androidx.compose.material3.Text
3746
import androidx.compose.runtime.Composable
47+
import androidx.compose.runtime.getValue
48+
import androidx.compose.runtime.mutableStateOf
49+
import androidx.compose.runtime.remember
50+
import androidx.compose.runtime.setValue
51+
import androidx.compose.ui.Alignment
3852
import androidx.compose.ui.Modifier
3953
import androidx.compose.ui.draw.clip
4054
import androidx.compose.ui.graphics.Color
4155
import androidx.compose.ui.tooling.preview.Preview
4256
import androidx.compose.ui.unit.dp
57+
import androidx.compose.ui.unit.sp
4358
import com.example.compose.snippets.util.MaterialColors
4459

4560
@Preview
@@ -262,10 +277,10 @@ private fun FlowItems() {
262277

263278
@OptIn(ExperimentalMaterialApi::class)
264279
@Composable
265-
fun ChipItem(text: String) {
280+
fun ChipItem(text: String, onClick: () -> Unit = {}) {
266281
Chip(
267282
modifier = Modifier.padding(end = 4.dp),
268-
onClick = {},
283+
onClick = onClick,
269284
leadingIcon = {},
270285
border = BorderStroke(1.dp, Color(0xFF3B3A3C))
271286
) {
@@ -426,9 +441,115 @@ fun FlowLayout_FractionalSizing() {
426441
) {
427442
val itemModifier = Modifier
428443
.clip(RoundedCornerShape(8.dp))
429-
Box(modifier = itemModifier.height(200.dp).width(60.dp).background(Color.Red))
430-
Box(modifier = itemModifier.height(200.dp).fillMaxWidth(0.7f).background(Color.Blue))
431-
Box(modifier = itemModifier.height(200.dp).weight(1f).background(Color.Magenta))
444+
Box(
445+
modifier = itemModifier
446+
.height(200.dp)
447+
.width(60.dp)
448+
.background(Color.Red)
449+
)
450+
Box(
451+
modifier = itemModifier
452+
.height(200.dp)
453+
.fillMaxWidth(0.7f)
454+
.background(Color.Blue)
455+
)
456+
Box(
457+
modifier = itemModifier
458+
.height(200.dp)
459+
.weight(1f)
460+
.background(Color.Magenta)
461+
)
432462
}
433463
// [END android_compose_flow_layout_fractional_sizing]
434464
}
465+
466+
@OptIn(ExperimentalLayoutApi::class)
467+
@Preview
468+
@Composable
469+
fun ContextualFlowLayoutExample() {
470+
// [START android_compose_layouts_contextual_flow]
471+
val totalCount = 40
472+
var maxLines by remember {
473+
mutableStateOf(2)
474+
}
475+
476+
val moreOrCollapseIndicator = @Composable { scope: ContextualFlowRowOverflowScope ->
477+
val remainingItems = totalCount - scope.shownItemCount
478+
ChipItem(if (remainingItems == 0) "Less" else "+$remainingItems", onClick = {
479+
if (remainingItems == 0) {
480+
maxLines = 2
481+
} else {
482+
maxLines += 5
483+
}
484+
})
485+
}
486+
ContextualFlowRow(
487+
modifier = Modifier
488+
.safeDrawingPadding()
489+
.fillMaxWidth(1f)
490+
.padding(16.dp)
491+
.wrapContentHeight(align = Alignment.Top)
492+
.verticalScroll(rememberScrollState()),
493+
verticalArrangement = Arrangement.spacedBy(4.dp),
494+
horizontalArrangement = Arrangement.spacedBy(8.dp),
495+
maxLines = maxLines,
496+
overflow = ContextualFlowRowOverflow.expandOrCollapseIndicator(
497+
minRowsToShowCollapse = 4,
498+
expandIndicator = moreOrCollapseIndicator,
499+
collapseIndicator = moreOrCollapseIndicator
500+
),
501+
itemCount = totalCount
502+
) { index ->
503+
ChipItem("Item $index")
504+
}
505+
// [END android_compose_layouts_contextual_flow]
506+
}
507+
508+
@OptIn(ExperimentalLayoutApi::class)
509+
@Preview
510+
@Composable
511+
fun FillMaxColumnWidth() {
512+
// [START android_compose_flow_layouts_fill_max_column_width]
513+
FlowColumn(
514+
Modifier
515+
.padding(20.dp)
516+
.fillMaxHeight()
517+
.fillMaxWidth(),
518+
horizontalArrangement = Arrangement.spacedBy(8.dp),
519+
verticalArrangement = Arrangement.spacedBy(8.dp),
520+
maxItemsInEachColumn = 5,
521+
) {
522+
repeat(listDesserts.size) {
523+
Box(
524+
Modifier
525+
.fillMaxColumnWidth()
526+
.border(1.dp, Color.DarkGray, RoundedCornerShape(8.dp))
527+
.padding(8.dp)
528+
) {
529+
530+
Text(
531+
text = listDesserts[it],
532+
fontSize = 18.sp,
533+
modifier = Modifier.padding(3.dp)
534+
)
535+
}
536+
}
537+
}
538+
// [END android_compose_flow_layouts_fill_max_column_width]
539+
}
540+
private val listDesserts = listOf(
541+
"Apple",
542+
"Banana",
543+
"Cupcake",
544+
"Donut",
545+
"Eclair",
546+
"Froyo",
547+
"Gingerbread",
548+
"Honeycomb",
549+
"Ice Cream Sandwich",
550+
"Jellybean",
551+
"KitKat",
552+
"Lollipop",
553+
"Marshmallow",
554+
"Nougat",
555+
)

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ coil = "2.5.0"
2222
# @keep
2323
compileSdk = "34"
2424
compose-compiler = "1.5.4"
25-
compose-latest = "1.7.0-alpha01"
25+
compose-latest = "1.7.0-alpha03"
2626
coroutines = "1.7.3"
2727
google-maps = "18.2.0"
2828
gradle-versions = "0.51.0"

0 commit comments

Comments
 (0)