mattinger
01/07/2022, 6:31 PMBryan Herbst
01/07/2022, 6:51 PMModifier.toggleable()
on the parent like so:
Row(
modifier = Modifier.toggleable(
value = checked,
onValueChange = { checked = !checked},
role = Role.Switch
)
) {
Switch(
checked = checked,
// null prevents the switch from being independently clickable
onCheckedChange = null
)
Text( ... )
}
Modifier.toggleable()
will do a Modifier.semantics(mergeDescendants = true)
internally, which is the merging behavior that you want. Using Modifier.toggleable()
instead of Modifier.semantics()
directly ensures that the entire merged content gets the correct semantics for a switch element in addition to just merging them.
This also has the side effect (desired, IMO) of making the text clickable to toggle the switch as well.lhwdev
01/08/2022, 9:47 AMMutableInteractionSource
(or something?) and pass into both Checkbox and Text(with indication = null.)
It would only show indication for Checkbox, but also allows clicking through text.
Just like https://raw.githubusercontent.com/lhwdev/covid-selftest-macro/c2db5c40805fb7262aa9c8f7da493a1867c3cb24/app/src/main/java/com/lhwdev/selfTestMacro/ui/utils/TextCheckbox.kt (but I forgot semantic ones so merge with the answer above)