class PickerState : ScrollableState


A state object that can be hoisted to observe item selection.

In most cases, this will be created via rememberPickerState.

Summary

Public companion properties

Saver<PickerStateAny>

The default Saver implementation for PickerState.

Public constructors

PickerState(
    initialNumberOfOptions: @IntRange(from = 1) Int,
    initiallySelectedIndex: @IntRange(from = 0) Int,
    shouldRepeatOptions: Boolean
)

Public functions

suspend Unit

Animate (smooth scroll) to the given option at index.

open Float
open suspend Unit
scroll(scrollPriority: MutatePriority, block: suspend ScrollScope.() -> Unit)
suspend Unit

Instantly scroll to an option.

Public properties

open Boolean
open Boolean
open Boolean
Int

Represents how many different choices are presented by this Picker

Int

Index of the selected option (i.e. at the center).

Boolean

if true (the default), the options will be repeated.

Public companion properties

Saver

Added in 1.0.0-alpha26
val SaverSaver<PickerStateAny>

The default Saver implementation for PickerState.

Public constructors

PickerState

Added in 1.0.0-alpha26
PickerState(
    initialNumberOfOptions: @IntRange(from = 1) Int,
    initiallySelectedIndex: @IntRange(from = 0) Int = 0,
    shouldRepeatOptions: Boolean = true
)
Parameters
initialNumberOfOptions: @IntRange(from = 1) Int

the number of options.

initiallySelectedIndex: @IntRange(from = 0) Int = 0

the index of the option to show in the center at the start, zero-based.

shouldRepeatOptions: Boolean = true

if true (the default), the options will be repeated.

Public functions

animateScrollToOption

Added in 1.0.0-alpha26
suspend fun animateScrollToOption(index: Int): Unit

Animate (smooth scroll) to the given option at index.

A smooth scroll always happens to the closest item if PickerState has repeatItems=true. For example, picker values are : 0 1 2 3 0 1 2 3 0 1 2 3 Target value is 0. 0 1 2 3 >0< 1 2 3>0< 1 2 3 Picker can be scrolled forwards or backwards. To get to the target 0 it requires 1 step to scroll forwards and 3 steps to scroll backwards. Picker will be scrolled forwards as this is the closest destination.

If the distance between possible targets is the same, picker will be scrolled backwards.

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.wear.compose.material3.Button
import androidx.wear.compose.material3.Picker
import androidx.wear.compose.material3.Text
import androidx.wear.compose.material3.rememberPickerState

val coroutineScope = rememberCoroutineScope()
val state = rememberPickerState(initialNumberOfOptions = 10)
val contentDescription by remember { derivedStateOf { "${state.selectedOptionIndex + 1}" } }

Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
    Picker(
        state = state,
        spacing = 4.dp,
        contentDescription = contentDescription,
    ) {
        Button(
            onClick = { coroutineScope.launch { state.animateScrollToOption(it) } },
            label = { Text("$it") }
        )
    }
}
Parameters
index: Int

The index of the option to scroll to.

dispatchRawDelta

Added in 1.0.0-alpha26
open fun dispatchRawDelta(delta: Float): Float

scroll

Added in 1.0.0-alpha26
open suspend fun scroll(scrollPriority: MutatePriority, block: suspend ScrollScope.() -> Unit): Unit

scrollToOption

Added in 1.0.0-alpha26
suspend fun scrollToOption(index: Int): Unit

Instantly scroll to an option.

import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.unit.dp
import androidx.wear.compose.material3.Button
import androidx.wear.compose.material3.Picker
import androidx.wear.compose.material3.Text
import androidx.wear.compose.material3.rememberPickerState

val coroutineScope = rememberCoroutineScope()
val state = rememberPickerState(initialNumberOfOptions = 10)
val contentDescription by remember { derivedStateOf { "${state.selectedOptionIndex + 1}" } }
Picker(
    state = state,
    spacing = 4.dp,
    contentDescription = contentDescription,
) {
    Button(
        onClick = { coroutineScope.launch { state.scrollToOption(it) } },
        label = { Text("$it") }
    )
}
Parameters
index: Int

The index of the option to scroll to.

Public properties

canScrollBackward

open val canScrollBackwardBoolean

canScrollForward

open val canScrollForwardBoolean

isScrollInProgress

Added in 1.0.0-alpha26
open val isScrollInProgressBoolean

numberOfOptions

Added in 1.0.0-alpha26
var numberOfOptionsInt

Represents how many different choices are presented by this Picker

selectedOptionIndex

Added in 1.0.0-alpha26
val selectedOptionIndexInt

Index of the selected option (i.e. at the center).

shouldRepeatOptions

Added in 1.0.0-alpha26
val shouldRepeatOptionsBoolean

if true (the default), the options will be repeated.