Skip to content

Slider

Slider is a basic interactive component in Miuix used for selecting values within a continuous range. Users can adjust values by dragging the slider, making it suitable for scenarios such as volume adjustment, brightness control, and progress display.

Miuix also provides VerticalSlider for vertical orientation and RangeSlider for selecting a range of values.

Import

kotlin
import top.yukonga.miuix.kmp.basic.Slider
import top.yukonga.miuix.kmp.basic.VerticalSlider
import top.yukonga.miuix.kmp.basic.RangeSlider

Basic Usage

Slider

kotlin
var sliderValue by remember { mutableFloatStateOf(0.5f) }

Slider(
    value = sliderValue,
    onValueChange = { sliderValue = it }
)

VerticalSlider

kotlin
var sliderValue by remember { mutableFloatStateOf(0.5f) }

VerticalSlider(
    value = sliderValue,
    onValueChange = { sliderValue = it },
    modifier = Modifier.height(200.dp)
)

RangeSlider

kotlin
var rangeValue by remember { mutableStateOf(0.2f..0.8f) }

RangeSlider(
    value = rangeValue,
    onValueChange = { rangeValue = it }
)

Component States

Disabled State

kotlin
var value by remember { mutableFloatStateOf(0.5f) }

Slider(
    value = value,
    onValueChange = { value = it },
    enabled = false
)

Haptic Feedback

Slider supports haptic feedback, which can be customized through the hapticEffect parameter. See SliderHapticEffect for details.

kotlin
var value by remember { mutableFloatStateOf(0.5f) }

Slider(
    value = value,
    onValueChange = { value = it },
    hapticEffect = SliderHapticEffect.Step
)

Properties

Slider Properties

Property NameTypeDescriptionDefault ValueRequired
valueFloatCurrent value of the slider. If outside of valueRange provided, value will be coerced to this range-Yes
onValueChange(Float) -> UnitCallback when value changes-Yes
modifierModifierModifier applied to the sliderModifierNo
enabledBooleanWhether the slider is enabledtrueNo
valueRangeClosedFloatingPointRange<Float>Range of values that this slider can take. The passed value will be coerced to this range0f..1fNo
stepsIntAmount of discrete allowable values. If 0, the slider will behave continuously. Must not be negative0No
onValueChangeFinished(() -> Unit)?Called when value change has endednullNo
reverseDirectionBooleanControls the direction of slider. When false, increases left to right. When true, increases right to left (RTL)falseNo
heightDpHeight of the sliderSliderDefaults.MinHeightNo
colorsSliderColorsColor configuration of the sliderSliderDefaults.sliderColors()No
effectBooleanWhether to show special effectsfalseNo
hapticEffectSliderDefaults.SliderHapticEffectType of haptic feedbackSliderDefaults.DefaultHapticEffectNo
showKeyPointsBooleanWhether to show key point indicators on the slider. Only works when keyPoints is not nullfalseNo
keyPointsList<Float>?Custom key point values to display on the slider. If null, uses step positions from steps parameter. Values should be within valueRangenullNo
magnetThresholdFloatMagnetic snap threshold as a fraction (0.0 to 1.0). When slider value is within this distance from a key point, it will snap to that point. Only applies when keyPoints is set0.02fNo

VerticalSlider Properties

Property NameTypeDescriptionDefault ValueRequired
valueFloatCurrent value of the slider-Yes
onValueChange(Float) -> UnitCallback when value changes-Yes
modifierModifierModifier applied to the sliderModifierNo
enabledBooleanWhether the slider is enabledtrueNo
valueRangeClosedFloatingPointRange<Float>Range of values that this slider can take0f..1fNo
stepsIntAmount of discrete allowable values0No
onValueChangeFinished(() -> Unit)?Called when value change has endednullNo
reverseDirectionBooleanControls the direction of slider. When false, increases bottom to top. When true, increases top to bottomfalseNo
widthDpWidth of the vertical sliderSliderDefaults.MinHeightNo
colorsSliderColorsColor configuration of the sliderSliderDefaults.sliderColors()No
effectBooleanWhether to show special effectsfalseNo
hapticEffectSliderDefaults.SliderHapticEffectType of haptic feedbackSliderDefaults.DefaultHapticEffectNo
showKeyPointsBooleanWhether to show key point indicators on the slider. Only works when keyPoints is not nullfalseNo
keyPointsList<Float>?Custom key point values to display on the slider. If null, uses step positions from steps parameter. Values should be within valueRangenullNo
magnetThresholdFloatMagnetic snap threshold as a fraction (0.0 to 1.0). When slider value is within this distance from a key point, it will snap to that point. Only applies when keyPoints is set0.02fNo

RangeSlider Properties

Property NameTypeDescriptionDefault ValueRequired
valueClosedFloatingPointRange<Float>Current values of the RangeSlider. If either value is outside of valueRange, it will be coerced-Yes
onValueChange(ClosedFloatingPointRange<Float>) -> UnitLambda in which values should be updated-Yes
modifierModifierModifier applied to the sliderModifierNo
enabledBooleanWhether the slider is enabledtrueNo
valueRangeClosedFloatingPointRange<Float>Range of values that Range Slider values can take0f..1fNo
stepsIntAmount of discrete allowable values0No
onValueChangeFinished(() -> Unit)?Called when value change has endednullNo
heightDpHeight of the sliderSliderDefaults.MinHeightNo
colorsSliderColorsColor configuration of the sliderSliderDefaults.sliderColors()No
effectBooleanWhether to show special effectsfalseNo
hapticEffectSliderDefaults.SliderHapticEffectType of haptic feedbackSliderDefaults.DefaultHapticEffectNo
showKeyPointsBooleanWhether to show key point indicators on the slider. Only works when keyPoints is not nullfalseNo
keyPointsList<Float>?Custom key point values to display on the slider. If null, uses step positions from steps parameter. Values should be within valueRangenullNo
magnetThresholdFloatMagnetic snap threshold as a fraction (0.0 to 1.0). When slider value is within this distance from a key point, it will snap to that point. Only applies when keyPoints is set0.02fNo

SliderDefaults Object

The SliderDefaults object provides default configurations for the Slider component.

Constants

Constant NameTypeDefault ValueDescription
MinHeightDp30.dpDefault height of the slider
DefaultHapticEffectSliderHapticEffectSliderHapticEffect.EdgeDefault haptic feedback type

SliderHapticEffect

ValueDescription
NoneNo haptic feedback
EdgeHaptic feedback at edges
StepHaptic feedback at each step

Methods

Method NameReturn TypeDescription
sliderColors()SliderColorsCreates default color configuration

SliderColors Class

Property NameTypeDescription
foregroundColorColorForeground color of the slider
disabledForegroundColorColorForeground color when disabled
backgroundColorColorBackground color of the slider
keyPointColorColorColor of key point indicators in background
keyPointForegroundColorColorColor of key point indicators in foreground

Advanced Usage

Custom Value Range

kotlin
var temperature by remember { mutableFloatStateOf(25f) }

Column {
    Text("Temperature: ${temperature.roundToInt()}°C")
    Slider(
        value = temperature,
        onValueChange = { temperature = it },
        valueRange = 16f..32f
    )
}

Discrete Steps

kotlin
var rating by remember { mutableFloatStateOf(3f) }

Column {
    Text("Rating: ${rating.roundToInt()}/5")
    Slider(
        value = rating,
        onValueChange = { rating = it },
        valueRange = 1f..5f,
        steps = 3  // Creates 5 discrete values: 1, 2, 3, 4, 5
    )
}

Custom Colors

kotlin
var volume by remember { mutableFloatStateOf(0.7f) }

Slider(
    value = volume,
    onValueChange = { volume = it },
    colors = SliderDefaults.sliderColors(
        foregroundColor = Color.Red,
        backgroundColor = Color.LightGray
    )
)

Custom Height and Effects

kotlin
var brightness by remember { mutableFloatStateOf(0.8f) }

Slider(
    value = brightness,
    onValueChange = { brightness = it },
    height = 40.dp,
    effect = true
)

Slider with Haptic Feedback

kotlin
var value by remember { mutableFloatStateOf(0.5f) }

Slider(
    value = value,
    onValueChange = { value = it },
    hapticEffect = SliderDefaults.SliderHapticEffect.Step
)

VerticalSlider with Reverse Direction

kotlin
var volume by remember { mutableFloatStateOf(0.5f) }

VerticalSlider(
    value = volume,
    onValueChange = { volume = it },
    modifier = Modifier.height(200.dp),
    reverseDirection = true  // Top to bottom
)

RangeSlider for Price Filter

kotlin
var priceRange by remember { mutableStateOf(100f..500f) }

Column {
    Text("Price: $${priceRange.start.roundToInt()} - $${priceRange.endInclusive.roundToInt()}")
    RangeSlider(
        value = priceRange,
        onValueChange = { priceRange = it },
        valueRange = 0f..1000f,
        steps = 99  // 101 discrete values from 0 to 1000
    )
}

Complete Example with Value Change Callback

kotlin
var value by remember { mutableFloatStateOf(0.5f) }
var finalValue by remember { mutableFloatStateOf(0.5f) }

Column {
    Text("Current: $value")
    Text("Final: $finalValue")
    Slider(
        value = value,
        onValueChange = { value = it },
        onValueChangeFinished = { finalValue = value },
        valueRange = 0f..100f,
        steps = 99
    )
}

Slider with Custom Key Points

kotlin
var value by remember { mutableFloatStateOf(50f) }

Column {
    Text("Value: ${value.roundToInt()}")
    Slider(
        value = value,
        onValueChange = { value = it },
        valueRange = 0f..100f,
        showKeyPoints = true,
        keyPoints = listOf(0f, 25f, 50f, 75f, 100f),
        magnetThreshold = 0.05f,  // 5% snap threshold
        hapticEffect = SliderDefaults.SliderHapticEffect.Step
    )
}

RangeSlider with Key Points

kotlin
var range by remember { mutableStateOf(20f..80f) }

Column {
    Text("Range: ${range.start.roundToInt()} - ${range.endInclusive.roundToInt()}")
    RangeSlider(
        value = range,
        onValueChange = { range = it },
        valueRange = 0f..100f,
        showKeyPoints = true,
        keyPoints = listOf(0f, 20f, 40f, 60f, 80f, 100f),
        magnetThreshold = 0.03f
    )
}

Released under the Apache-2.0 License