Colton Idle
11/17/2021, 10:11 PMclass ScreenAState {
val people = mutableStateListOf<PersonUIWrapper>()
}
data class PersonUIWrapper(var selected: Boolean, val person: Person)
then in an onclick of a person in my list I do
TextButton(onClick = {
it.selected = true
})...
but my code doesn't recompose, and therefore doesn't show a checkmark. Is my PersonUIWrapper supposed to use snapshot state internally for it's two fields?Joseph Hawkes-Cates
11/17/2021, 10:15 PMJoseph Hawkes-Cates
11/17/2021, 10:15 PMColton Idle
11/17/2021, 10:16 PMvm.state.people.forEach {
TextButton(onClick = {
it.selected = true
}) {
Joseph Hawkes-Cates
11/17/2021, 10:17 PMJoseph Hawkes-Cates
11/17/2021, 10:18 PMColton Idle
11/17/2021, 10:19 PMif (it.selected) {
Icon(Icons.Default.Check, null)
}
Text(text = it.person.name)
Colton Idle
11/17/2021, 10:19 PMvm.state.people.forEach {
TextButton(onClick = {
it.selected = true
}) {
if (it.selected) {
Icon(Icons.Default.Check, null)
}
Text(text = it.person.name)
}
Sorry for providing it in pieces. I was trying to be as concise as possible. 😄Joseph Hawkes-Cates
11/17/2021, 10:20 PMJoseph Hawkes-Cates
11/17/2021, 10:21 PMColton Idle
11/17/2021, 10:21 PMJoseph Hawkes-Cates
11/17/2021, 10:23 PMJoseph Hawkes-Cates
11/17/2021, 10:23 PMJoseph Hawkes-Cates
11/17/2021, 10:23 PMnitrog42
11/17/2021, 10:33 PMnitrog42
11/17/2021, 10:33 PMcan only notify about adding/removing/replacing some element in the list. When you change any class inside the list, the mutable state cannot know about it.mutableStateListOf
Alex Vanyo
11/17/2021, 10:40 PMPersonUIWrapper
was backed by state that was observable to Compose (like mutableStateOf
), then this would work like you’d expect.
And that can be preferable as an alternative to the .copy
solution with a data class
.Colton Idle
11/18/2021, 1:12 AMclass PersonUIWrapper(val selected: MutableState<Boolean> = mutableStateOf(true), val person: Person)
It kind of sucks that I can't use by
in this case, but using .value
isn't the worst thing!Adam Powell
11/18/2021, 2:50 AMclass PersonUIWrapper(
selected: Boolean,
val person: Person
) {
var selected by mutableStateOf(selected)
}
Colton Idle
11/18/2021, 3:40 AMAdam Powell
11/18/2021, 5:27 AMAdam Powell
11/18/2021, 5:30 AM