ContextualFlowColumn

Functions summary

Unit
@Composable
@ExperimentalLayoutApi
ContextualFlowColumn(
    itemCount: Int,
    modifier: Modifier,
    verticalArrangement: Arrangement.Vertical,
    horizontalArrangement: Arrangement.Horizontal,
    itemHorizontalAlignment: Alignment.Horizontal,
    maxItemsInEachColumn: Int,
    maxLines: Int,
    overflow: ContextualFlowColumnOverflow,
    content: @Composable ContextualFlowColumnScope.(index: Int) -> Unit
)

This function is deprecated. ContextualFlowLayouts are no longer maintained

Cmn

Functions

ContextualFlowColumn

@Composable
@ExperimentalLayoutApi
fun ContextualFlowColumn(
    itemCount: Int,
    modifier: Modifier = Modifier,
    verticalArrangement: Arrangement.Vertical = Arrangement.Top,
    horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
    itemHorizontalAlignment: Alignment.Horizontal = Alignment.Start,
    maxItemsInEachColumn: Int = Int.MAX_VALUE,
    maxLines: Int = Int.MAX_VALUE,
    overflow: ContextualFlowColumnOverflow = ContextualFlowColumnOverflow.Clip,
    content: @Composable ContextualFlowColumnScope.(index: Int) -> Unit
): Unit

ContextualFlowColumn is a specialized version of the FlowColumn layout. It is designed to enable users to make contextual decisions during the construction of FlowColumn layouts.

This component is particularly advantageous when dealing with a large collection of items, allowing for efficient management and display. Unlike traditional FlowColumn that composes all items regardless of their visibility, ContextualFlowColumn smartly limits composition to only those items that are visible within its constraints, such as maxLines or maxWidth. This approach ensures optimal performance and resource utilization by composing fewer items than the total number available, based on the current context and display parameters.

While maintaining the core functionality of the standard FlowColumn, ContextualFlowColumn operates on an index-based system and composes items sequentially, one after another. This approach provides a perfect way to make contextual decisions and can be an easier way to handle problems such as dynamic see more buttons such as (N+ buttons).

Example:

import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.ContextualFlowColumn
import androidx.compose.foundation.layout.ContextualFlowColumnOverflow
import androidx.compose.foundation.layout.ContextualFlowColumnOverflowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

val totalCount = 300
var maxLines by remember { mutableStateOf(2) }

Text(
    modifier = Modifier.fillMaxWidth(1f).padding(20.dp),
    text =
        "ContextualFlowColumn (based on Subcompose)" +
            " is great for Large Items & +N dynamic labels",
    fontWeight = FontWeight.Bold,
)

val moreOrCollapseIndicator =
    @Composable { scope: ContextualFlowColumnOverflowScope ->
        val remainingItems = totalCount - scope.shownItemCount
        DynamicSeeMore(isHorizontal = true, remainingItems = remainingItems) {
            if (remainingItems == 0) {
                maxLines = 2
            } else {
                maxLines += 2
            }
        }
    }
ContextualFlowColumn(
    modifier =
        Modifier.fillMaxWidth(1f)
            .horizontalScroll(rememberScrollState())
            .padding(20.dp)
            .height(200.dp)
            .wrapContentHeight(align = Alignment.Top),
    verticalArrangement = Arrangement.spacedBy(10.dp),
    horizontalArrangement = Arrangement.spacedBy(20.dp),
    maxLines = maxLines,
    overflow =
        ContextualFlowColumnOverflow.expandOrCollapseIndicator(
            minColumnsToShowCollapse = 4,
            expandIndicator = moreOrCollapseIndicator,
            collapseIndicator = moreOrCollapseIndicator,
        ),
    itemCount = totalCount,
) { index ->
    Box(
        modifier =
            Modifier.align(Alignment.CenterHorizontally)
                .height(50.dp)
                .width(50.dp)
                .background(Color.Green)
    ) {
        Text(
            text = index.toString(),
            fontSize = 18.sp,
            modifier = Modifier.padding(3.dp).align(Alignment.Center),
        )
    }
}
Parameters
itemCount: Int

The total number of item composable

modifier: Modifier = Modifier

The modifier to be applied to the Row.

verticalArrangement: Arrangement.Vertical = Arrangement.Top

The vertical arrangement of the layout's virtual column.

horizontalArrangement: Arrangement.Horizontal = Arrangement.Start

The horizontal arrangement of the layout's children.

itemHorizontalAlignment: Alignment.Horizontal = Alignment.Start

The cross axis/horizontal alignment of an item in the column.

maxItemsInEachColumn: Int = Int.MAX_VALUE

The maximum number of items per column

maxLines: Int = Int.MAX_VALUE

The maximum number of columns

overflow: ContextualFlowColumnOverflow = ContextualFlowColumnOverflow.Clip

The straoadtegy to handle overflowing items

content: @Composable ContextualFlowColumnScope.(index: Int) -> Unit

The indexed-based content of ContextualFlowColumnScope