At first, itemBackgroundColor
is correct, some items are blue and some are gray.
But when the list is updated and some item.judge
changed, itemBackgroundColor
won't update accordingly.
How can I update itemBackgroundColor
when the list
is updated? Thanks a lot!
@Composable
fun LayoutsCodelab(viewModel: MyViewModel) {
// list<Item>, Item contains name, judge
val list = viewModel.myList.observeAsState(listOf()).value
Scaffold {
LazyColumn(state = rememberLazyListState()) {
items(list.size) {
val item = list[it]
// How can I update itemBackgroundColor based on item.judge?
val itemBackgroundColor by mutableStateOf(
if (item.judge) Color.Blue else Color.Gray)
// apply itemBackgroundColor here
Row(modifier = Modifier.background(color = itemBackgroundColor)) {
//other detail codes ...
}
}
}
}
}