PickerState
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<PickerState, Any> |
The default |
Public constructors |
|---|
PickerState( |
Public functions |
|
|---|---|
suspend Unit |
animateScrollToOption(index: Int)Animate (smooth scroll) to the given item at |
open Float |
dispatchRawDelta(delta: Float) |
open suspend Unit |
scroll(scrollPriority: MutatePriority, block: suspend ScrollScope.() -> Unit) |
suspend Unit |
scrollToOption(index: Int)Instantly scroll to an item. |
Public properties |
|
|---|---|
open Boolean |
|
open Boolean |
|
open Boolean |
|
Int |
|
Boolean |
if true (the default), the contents of the component will be repeated |
Int |
Index of the option selected (i.e., at the center) |
Inherited properties |
|---|
Public companion properties
Saver
val Saver: Saver<PickerState, Any>
The default Saver implementation for PickerState.
Public constructors
PickerState
PickerState(
initialNumberOfOptions: Int,
initiallySelectedOption: Int = 0,
repeatItems: Boolean = true
)
Public functions
animateScrollToOption
suspend fun animateScrollToOption(index: Int): Unit
Animate (smooth scroll) to the given item 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.material.Chip import androidx.wear.compose.material.Picker import androidx.wear.compose.material.Text import androidx.wear.compose.material.rememberPickerState val coroutineScope = rememberCoroutineScope() val state = rememberPickerState(initialNumberOfOptions = 10) val contentDescription by remember { derivedStateOf { "${state.selectedOption + 1}" } } Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Picker(state = state, separation = 4.dp, contentDescription = contentDescription) { Chip( onClick = { coroutineScope.launch { state.animateScrollToOption(it) } }, label = { Text("$it") }, ) } }
| Parameters | |
|---|---|
index: Int |
The index of the option to scroll to. |
scroll
open suspend fun scroll(scrollPriority: MutatePriority, block: suspend ScrollScope.() -> Unit): Unit
scrollToOption
suspend fun scrollToOption(index: Int): Unit
Instantly scroll to an item.
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.material.Chip import androidx.wear.compose.material.Picker import androidx.wear.compose.material.Text import androidx.wear.compose.material.rememberPickerState val coroutineScope = rememberCoroutineScope() val state = rememberPickerState(initialNumberOfOptions = 10) val contentDescription by remember { derivedStateOf { "${state.selectedOption + 1}" } } Picker(state = state, separation = 4.dp, contentDescription = contentDescription) { Chip( onClick = { coroutineScope.launch { state.scrollToOption(it) } }, label = { Text("$it") }, ) }
| Parameters | |
|---|---|
index: Int |
The index of the option to scroll to. |
Public properties
repeatItems
val repeatItems: Boolean
if true (the default), the contents of the component will be repeated
selectedOption
val selectedOption: Int
Index of the option selected (i.e., at the center)