androidx.xr.glimmer.stack


Interfaces

StackItemScope

Receiver scope used by item content in VerticalStack.

StackScope

Receiver scope used by VerticalStack that defines a DSL for adding items to the stack.

Classes

StackState

The VerticalStack state that allows programmatic control and observation of the stack's state.

Top-level functions summary

Unit
@Composable
VerticalStack(modifier: Modifier, state: StackState, content: StackScope.() -> Unit)

VerticalStack is a lazy scrollable layout that displays its children in a form of a stack where the item on top of the stack is prominently displayed.

StackState
@Composable
rememberStackState(initialTopItem: @IntRange(from = 0) Int)

Creates and remembers a StackState for a VerticalStack.

Extension functions summary

inline Unit
<T : Any?> StackScope.items(
    items: List<T>,
    noinline key: ((item) -> Any)?,
    crossinline itemContent: @Composable StackItemScope.(item) -> Unit
)

Adds a list of items.

Top-level functions

VerticalStack

@Composable
fun VerticalStack(
    modifier: Modifier = Modifier,
    state: StackState = rememberStackState(),
    content: StackScope.() -> Unit
): Unit

VerticalStack is a lazy scrollable layout that displays its children in a form of a stack where the item on top of the stack is prominently displayed. VerticalStack implements the item traversal in a vertical direction.

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.xr.glimmer.Card
import androidx.xr.glimmer.CardDefaults
import androidx.xr.glimmer.Text
import androidx.xr.glimmer.stack.VerticalStack

VerticalStack(modifier = Modifier.height(300.dp)) {
    item(key = 0) {
        Card(modifier = Modifier.fillMaxSize().itemDecoration(CardDefaults.shape)) {
            Text("Item-0")
        }
    }
    items(count = 10, key = { it + 1 }) { index ->
        Card(modifier = Modifier.fillMaxSize().itemDecoration(CardDefaults.shape)) {
            Text("Item-${index + 1}")
        }
    }
}
Parameters
modifier: Modifier = Modifier

the modifier to apply to this layout.

state: StackState = rememberStackState()

the state of the stack.

content: StackScope.() -> Unit

a block that describes the content. Inside this block you can use methods like StackScope.item to add a single item or StackScope.items to add a collection of items.

rememberStackState

@Composable
fun rememberStackState(initialTopItem: @IntRange(from = 0) Int = 0): StackState

Creates and remembers a StackState for a VerticalStack.

The returned StackState is remembered across compositions and can be used to control or observe the state of a VerticalStack. It's essential to pass this state to the state parameter of the corresponding VerticalStack composable.

Note: Properties of the state will only be correctly populated after the VerticalStack it is associated with has been composed for the first time.

Warning: A single StackState instance must not be shared across multiple VerticalStack composables.

Parameters
initialTopItem: @IntRange(from = 0) Int = 0

The index of the item to show at the top of the stack initially. Must be non-negative. Defaults to 0.

Extension functions

inline fun <T : Any?> StackScope.items(
    items: List<T>,
    noinline key: ((item) -> Any)? = null,
    crossinline itemContent: @Composable StackItemScope.(item) -> Unit
): Unit

Adds a list of items.

Parameters
items: List<T>

the list of item data

noinline key: ((item) -> Any)? = null

a factory of stable and unique keys representing the items. If a key is specified, the scroll position will be maintained based on the key. If items are added/removed before the current visible item, the item with the given key will be kept as the first visible one. If null is passed, the position in the stack will represent the key.

crossinline itemContent: @Composable StackItemScope.(item) -> Unit

the content displayed by a single item