A scrollable list of items to pick from.
PickerGroupSample
@Composable
fun PickerGroupSample() {
var selectedPickerIndex by remember { mutableIntStateOf(0) }
val pickerStateHour = rememberPickerState(initialNumberOfOptions = 24)
val pickerStateMinute = rememberPickerState(initialNumberOfOptions = 60)
Column(
modifier = Modifier.fillMaxWidth(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
Spacer(modifier = Modifier.size(30.dp))
val label = if (selectedPickerIndex == 0) "Hours" else "Minutes"
AnimatedContent(targetState = label) { targetText -> Text(text = targetText) }
Spacer(modifier = Modifier.size(10.dp))
PickerGroup(
selectedPickerState =
if (selectedPickerIndex == 0) pickerStateHour else pickerStateMinute,
autoCenter = false,
) {
PickerGroupItem(
pickerState = pickerStateHour,
selected = selectedPickerIndex == 0,
onSelected = { selectedPickerIndex = 0 },
option = { optionIndex, _ -> Text(text = "%02d".format(optionIndex)) },
contentDescription = { "Hours" },
modifier = Modifier.size(80.dp, 100.dp),
)
PickerGroupItem(
pickerState = pickerStateMinute,
selected = selectedPickerIndex == 1,
onSelected = { selectedPickerIndex = 1 },
option = { optionIndex, _ -> Text(text = "%02d".format(optionIndex)) },
contentDescription = { "Minutes" },
modifier = Modifier.size(80.dp, 100.dp),
)
}
}
}
SimplePicker
@Composable
fun SimplePicker() {
val items = listOf("One", "Two", "Three", "Four", "Five")
val state = rememberPickerState(items.size)
// We forward scroll gestures from the whole screen to the Picker which makes this sample
// accessible for 2-finger vertical scrolling.
Box(
modifier =
Modifier.fillMaxSize()
.scrollable(
state = state,
orientation = Orientation.Vertical,
reverseDirection = true,
),
contentAlignment = Alignment.Center,
) {
Text(
modifier = Modifier.align(Alignment.TopCenter).padding(top = 10.dp),
text = "Selected: ${items[state.selectedOptionIndex]}",
)
Picker(
modifier = Modifier.size(100.dp, 100.dp),
state = state,
contentDescription = { "${state.selectedOptionIndex + 1}" },
) {
Text(items[it])
}
}
}