So I did a stepper in Compose and followed an example where I had the state in the Composable method, then called a stepper function and passed it the state. Worked in 03, does not compile in 04, tells me I cannot mutate a val.
Copy code
@Composable
fun MeasureQuestionView() {
val typography = (MaterialTheme.typography())
val rangeOfValues = 115..125
val currentValue = state {rangeOfValues.first}
Card(shape = RoundedCornerShape(8.dp)) {
Column(modifier = Spacing(16.dp)) {
Text("What was your blood sugar reading?", style = typography.h6)
numberStepper(currentValue, rangeOfValues, "mg/dL")
}
}
}
@Composable
fun numberStepper(currentValue: State<Int>, range: IntRange, units: String) {
val typography = (MaterialTheme.typography())
Container(height = 75.dp) {
Row(Spacing(20.dp)) {
Button(text = "-", onClick = {
if (currentValue.value > range.first) {
currentValue.value -= 1
}
})
Spacer(LayoutWidth(10.dp))
Text(currentValue.value.toString(), style = typography.h5)
Spacer(LayoutWidth(10.dp))
Button(text = "+", onClick = {
if (currentValue.value < range.last) currentValue.value += 1
})
Spacer(LayoutWidth(10.dp))
Align(Alignment.CenterLeft){
Text(units)
}
}
}
}
a
Adam Powell
01/30/2020, 4:04 AM
The old
State<T>
has been split into
State<T>
and
MutableState<T>
to match the usual kotlin collection interfaces
Adam Powell
01/30/2020, 4:04 AM
looks like you now want
MutableState<T>
in your stepper function parameter list
Adam Powell
01/30/2020, 4:06 AM
(since
State<T>.value
is indeed now a
val
)
c
codeslubber
01/30/2020, 4:07 AM
strange that ever worked? ok thanks @Adam Powell! compiles now…
a
Adam Powell
01/30/2020, 4:07 AM
there was only one interface with
var
in -dev03 🙂
Adam Powell
01/30/2020, 4:08 AM
the
state {}
factory returns
MutableState
so if you were relying on inference and refactored, that would do it