Skip to content

NumberPicker

NumberPicker is a basic interactive component in COUI used for selecting a value from a range of numbers by vertical scrolling. The selected item is centered, enlarged and highlighted, while surrounding items scale down and transition to the unselected color within half an item of the center. Supports infinite scrolling with the wrapAround parameter.

Import

kotlin
import io.github.suqi8.coui.kmp.basic.NumberPicker

Basic Usage

kotlin
var value by remember { mutableIntStateOf(5) }

NumberPicker(
    value = value,
    onValueChange = { value = it },
    range = 0..10
)

Component States

Disabled State

kotlin
var value by remember { mutableIntStateOf(5) }

NumberPicker(
    value = value,
    onValueChange = { value = it },
    range = 0..10,
    enabled = false
)

Infinite Scrolling

When wrapAround is enabled, the picker wraps from the last item back to the first, allowing continuous scrolling.

kotlin
var value by remember { mutableIntStateOf(0) }

NumberPicker(
    value = value,
    onValueChange = { value = it },
    range = 0..23,
    wrapAround = true
)

Properties

NumberPicker Properties

Property NameTypeDescriptionDefault ValueRequired
valueIntCurrent selected value. If outside range, it will be coerced-Yes
onValueChange(Int) -> UnitCallback invoked when the selected value changes-Yes
modifierModifierModifier applied to the pickerModifierNo
enabledBooleanWhether the picker is enabled for user interactiontrueNo
rangeIntRangeThe range of selectable values0..10No
label(Int) -> StringConverts a value to its display stringNo
visibleItemCountIntNumber of visible items. Must be odd and at least 33No
wrapAroundBooleanWhether the picker wraps around (infinite scrolling)falseNo
colorsNumberPickerColorsColor configuration of the pickerNumberPickerDefaults.colors()No
textStyleTextStyleText style for picker itemsCOUITheme.textStyles.title3No
itemHeightDpThe height of each item in the pickerNumberPickerDefaults.ItemHeightNo

NumberPickerDefaults Object

The NumberPickerDefaults object provides default configurations for the NumberPicker component.

Properties

Property NameTypeDescriptionDefault Value
ItemHeightDpDefault height of each item45.dp

Methods

Method NameReturn TypeDescription
colors()NumberPickerColorsCreates default color configuration

The colors() factory accepts the following parameters:

Parameter NameTypeDefault Value
selectedTextColorColorCOUITheme.colorScheme.onSurface
unselectedTextColorColorCOUITheme.colorScheme.onSurfaceVariantActions
disabledSelectedTextColorColorCOUITheme.colorScheme.disabledOnSecondary
disabledUnselectedTextColorColorCOUITheme.colorScheme.disabledOnSecondary

NumberPickerColors Class

Property NameTypeDescription
selectedTextColorColorText color for the selected (center) item
unselectedTextColorColorText color for unselected items
disabledSelectedTextColorColorSelected text color when disabled
disabledUnselectedTextColorColorUnselected text color when disabled

Advanced Usage

Custom Label Format

kotlin
var hour by remember { mutableIntStateOf(9) }

NumberPicker(
    value = hour,
    onValueChange = { hour = it },
    range = 0..23,
    label = { it.toString().padStart(2, '0') }  // "00", "01", ... "23"
)

Time Picker

Combine two NumberPickers to create a time picker.

kotlin
var hour by remember { mutableIntStateOf(16) }
var minute by remember { mutableIntStateOf(30) }

Row(
    horizontalArrangement = Arrangement.Center,
    verticalAlignment = Alignment.CenterVertically,
) {
    NumberPicker(
        value = hour,
        onValueChange = { hour = it },
        range = 0..23,
        label = { it.toString().padStart(2, '0') },
        wrapAround = true,
        modifier = Modifier.weight(1f),
    )
    Text(
        text = ":",
        fontWeight = FontWeight.Bold,
    )
    NumberPicker(
        value = minute,
        onValueChange = { minute = it },
        range = 0..59,
        label = { it.toString().padStart(2, '0') },
        wrapAround = true,
        modifier = Modifier.weight(1f),
    )
}

Custom Visible Item Count

kotlin
var value by remember { mutableIntStateOf(5) }

NumberPicker(
    value = value,
    onValueChange = { value = it },
    range = 1..100,
    visibleItemCount = 5 // Must be odd and at least 3
)

Custom Colors

kotlin
var value by remember { mutableIntStateOf(5) }

NumberPicker(
    value = value,
    onValueChange = { value = it },
    range = 0..10,
    colors = NumberPickerDefaults.colors(
        selectedTextColor = Color.Red,
        unselectedTextColor = Color.Gray
    )
)

Changelog

Released under the Apache-2.0 License