@@ -21,25 +21,40 @@ import androidx.compose.foundation.background
21
21
import androidx.compose.foundation.border
22
22
import androidx.compose.foundation.layout.Arrangement
23
23
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
24
27
import androidx.compose.foundation.layout.ExperimentalLayoutApi
28
+ import androidx.compose.foundation.layout.FlowColumn
25
29
import androidx.compose.foundation.layout.FlowRow
26
30
import androidx.compose.foundation.layout.Spacer
31
+ import androidx.compose.foundation.layout.fillMaxHeight
27
32
import androidx.compose.foundation.layout.fillMaxSize
28
33
import androidx.compose.foundation.layout.fillMaxWidth
29
34
import androidx.compose.foundation.layout.height
30
35
import androidx.compose.foundation.layout.padding
36
+ import androidx.compose.foundation.layout.safeDrawingPadding
31
37
import androidx.compose.foundation.layout.size
32
38
import androidx.compose.foundation.layout.width
39
+ import androidx.compose.foundation.layout.wrapContentHeight
40
+ import androidx.compose.foundation.rememberScrollState
33
41
import androidx.compose.foundation.shape.RoundedCornerShape
42
+ import androidx.compose.foundation.verticalScroll
34
43
import androidx.compose.material.Chip
35
44
import androidx.compose.material.ExperimentalMaterialApi
36
- import androidx.compose.material .Text
45
+ import androidx.compose.material3 .Text
37
46
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
38
52
import androidx.compose.ui.Modifier
39
53
import androidx.compose.ui.draw.clip
40
54
import androidx.compose.ui.graphics.Color
41
55
import androidx.compose.ui.tooling.preview.Preview
42
56
import androidx.compose.ui.unit.dp
57
+ import androidx.compose.ui.unit.sp
43
58
import com.example.compose.snippets.util.MaterialColors
44
59
45
60
@Preview
@@ -262,10 +277,10 @@ private fun FlowItems() {
262
277
263
278
@OptIn(ExperimentalMaterialApi ::class )
264
279
@Composable
265
- fun ChipItem (text : String ) {
280
+ fun ChipItem (text : String , onClick : () -> Unit = {} ) {
266
281
Chip (
267
282
modifier = Modifier .padding(end = 4 .dp),
268
- onClick = {} ,
283
+ onClick = onClick ,
269
284
leadingIcon = {},
270
285
border = BorderStroke (1 .dp, Color (0xFF3B3A3C ))
271
286
) {
@@ -426,9 +441,115 @@ fun FlowLayout_FractionalSizing() {
426
441
) {
427
442
val itemModifier = Modifier
428
443
.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
+ )
432
462
}
433
463
// [END android_compose_flow_layout_fractional_sizing]
434
464
}
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
+ )
0 commit comments