起初,itemBackgroundColor
是正确的,有些项目是蓝色的,有些是灰色的。
但是当列表更新并且有些item.judge
改变时,itemBackgroundColor
不会相应地更新。
itemBackgroundColor
更新时如何list
更新?非常感谢!
@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 ...
}
}
}
}
}