I was wondering if it is possible in Jetpack Compose to get a google keep checkboxes like behavior, anyone did something like this before and want to share their solution?
I try this code but it doesn’t work
@Composable
fun ListScreen(people: List<Person>, onItemClick: (Int, Boolean) -> Unit, updateName: (Int, String) -> Unit) {
LazyColumn(
verticalArrangement = Arrangement.spacedBy(2.dp),
modifier = Modifier.fillMaxSize()
) {
items(items = people, key = { it.hashCode() }) {
ListItem(it.id, it.name, true, onItemClick = onItemClick, onChangeName = updateName)
}
}
}
@Composable
private fun ListItem(id:Int, text: String, flag: Boolean, onItemClick: (Int, Boolean) -> Unit, onChangeName: (Int, name: String) -> Unit) {
val t: String by remember {
mutableStateOf("${text}")
}
Column(
modifier = Modifier.border(3.dp, Color.Green)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
BasicTextField(value = t, onValueChange = { onChangeName(id, it) })
Checkbox(checked = flag, onCheckedChange = { onItemClick(id, it) })
}
}
}
class GameViewModel : ViewModel() {
private val initialList = listOf(
(Person(id = 0, name = "Name0")),
(Person(id = 1, name = "Name1")),
(Person(id = 2, name = "Name2")),
(Person(id = 3, name = "Name3")),
)
val people = mutableStateListOf<Person>().apply {
addAll(initialList)
}
fun toggleSelection(index: Int, checked: Boolean) {
val item = people[index]
people[index] = item.copy(bool = checked)
}
fun updateName(index: Int, name: String) {
people[index] = people[index].copy(name=name)
}
}
enter image description here
New contributor
developer1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.