I created a multi-select list, but the issue is it doesn't recompose right away (the checkmark doesn't show immediately, I have to scroll down then up for it to appear). Here is my current code:
Data class:
data class Item(
val obj: SomeObject? = null,
var isSelected: Boolean = false
)
ViewModel:
private val _itemsList: MutableStateFlow<MutableList<Item>> = MutableStateFlow(mutableListOf())
val itemsList = _itemsList.asStateFlow()
fun updateSelection(item: Item) {
_itemsList.value.find {
it.obj?.id == item.obj?.id
}?.isSelected = item.isSelected
}
Composable functions:
Row(
modifier = modifier
.fillMaxWidth()
.clickable {
item.isSelected = !item.isSelected
onCheckClicked(item)
}
) {
CheckableCard(
// other unrelated params,
isSelected = item.isSelected,
onCheckClicked = null
)
}
}
@Composable
fun CheckableCard(
// unrelated params,
isSelected: Boolean,
onCheckClicked: ((Boolean) -> Unit)? = null,
) {
val checkedState by remember { mutableStateOf(isSelected) }
Card(
modifier = modifier
.fillMaxWidth()
.wrapContentHeight()
.padding(top = 16.dp),
) {
Row(
modifier = modifier
.padding(8.dp)
) {
// unrelated content
Checkbox(
checked = checkedState,
onCheckedChange = onCheckClicked
)
}
}
}
I added the onCheckClicked in the Row instead of passing it to the CheckableCard because this way the item's isSelected gets updated correctly when checking/unchecking the item. However this isn't the case when I pass the function to CheckableCard (I'm monitoring the behaviour via log.D and when the function is passed to CheckableCard nothing gets updated)