Rick Regan
02/16/2021, 5:47 PMSlider
. As far as I can tell, it's not possible to hoist the discrete slider state and have it control the slider, meaning the local continuous slider state is the actual source of truth (see code in thread -- you of course can't reset the slider from the callback). You need to maintain continuous slider state so that the slider moves smoothly -- instead of jumping -- between discrete values.
Obviously I could fix this by hoisting the continuous state, but then the slider is no longer conceptually discrete at that level; I'd have to move the "discreteness processing" up there. I don't think there's anything to be done about it; I guess I'm just saying that the discrete slider abstraction is "leaky".var hoistedDiscreteSliderValue by remember { mutableStateOf(0) }
SliderTruth(
hoistedDiscreteSliderValue,
{ hoistedDiscreteSliderValue = it },
{ hoistedDiscreteSliderValue = 0 }
)
@Composable
fun SliderTruth(
discreteSliderValue: Int,
onDiscreteSliderValueChange: (newDiscreteSliderValue: Int) -> Unit,
onSliderReset: () -> Unit
) {
var continuousSliderValue by remember { mutableStateOf(discreteSliderValue.toFloat()) }
Column {
Slider(
valueRange = 0f..20f,
steps = 3, // Step size of 5
value = continuousSliderValue,
onValueChange = {
continuousSliderValue = it
val newDiscreteSliderValue = (continuousSliderValue).toInt()
if (newDiscreteSliderValue in 0..20 step 5)
onDiscreteSliderValueChange(newDiscreteSliderValue)
},
)
Button(onClick = { onSliderReset() }
) {
Text(text = "Reset slider")
}
}
}
matvei
02/16/2021, 5:52 PMRick Regan
02/16/2021, 5:54 PMmatvei
02/16/2021, 5:58 PMRick Regan
02/16/2021, 6:05 PMsteps
is specified? That would not change the API I don't think.matvei
02/17/2021, 11:37 AM