I have a list of specialties from which the user will check. After the user is happy with their selection, I will gather the information for selected checkboxes and save those specialties in the user's profile.
However, when I tap any of the checkboxes, the state in the view model changes but checkbox is still unchecked.
Specialty List
@Composable
fun SpecialtyList(
viewModel: SpecialtyListViewModel = hiltViewModel(),
navController: NavController
) {
LazyColumn(modifier = Modifier.fillMaxSize()) {
item {
viewModel.specialtyList.value.forEach { specialty ->
Row(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp),
verticalAlignment = Alignment.CenterVertically
) {
Checkbox(
checked = specialty.isSelected,
onCheckedChange = {
specialty.isSelected = it
},
colors = CheckboxDefaults.colors(MaterialTheme.colors.primary)
)
Text(
text = specialty.name,
modifier = Modifier
.padding(horizontal = 10.dp)
)
}
}
}
}
}
Specialty View Model
@HiltViewModel
class SpecialtyListViewModel @Inject constructor() : ViewModel() {
// HARD-CODED PROPERTIES
val specialtyList = mutableStateOf(
mutableListOf(
Specialty(name = "Emergency Medicine", isSelected = false),
Specialty(name = "Forensic Medicine", isSelected = false),
Specialty(name = "General Practitioner", isSelected = false)
)
}
Specialty Model
data class Specialty(
val name: String,
var isSelected: Boolean
)