1

I want an enabled switch to consume the click events and a disabled switch to pass the click event to its parent Composable.

Why does a disabled Switch consume click events?

A minified example of the issue,

@Composable
fun DisabledSwitch() {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable {}
            .padding(16.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween,
    ) {
        Text("Disabled Switch")
        Switch(
            checked = false,
            enabled = false,
            onCheckedChange = {
                // This cannot be null 
                // as I need to handle click events when the switch is enabled
            },
        )
    }
}

Clicking on the Text triggers the Text clickable, but clicking on the disabled Switch does not trigger the Text clickable (parent composable click handler).

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
  • This behaves exactly as I would expect, but if you wanna override this behaviour, check out [this answer](https://stackoverflow.com/a/68878910/3585796). You can choose wether to use `detectTapAndPressUnconsumed` or `detectTapAndPress` depending on switch state. – Phil Dukhov Nov 12 '21 at 17:57
  • @PhilipDukhov, Thanks that works, but the reason for this question was to maintain the same ripple effect for the label and switch click. But that solution doesn't use any default ripple like `clickable`. Moreover, I have to copy `PressGestureScopeImpl` complete class and `NoPressGesture`. Seems, too much code for a simple use-case. – Abhimanyu Nov 12 '21 at 18:42
  • This [this answer](https://stackoverflow.com/a/69754118/3585796) on how you can enable ripple effect for custom gestures – Phil Dukhov Nov 12 '21 at 18:43
  • I guessed you were about to say that. But for me, the easier option seems to be to pass `null` to `onCheckedChange` and handle all click events in the parent composable like [this](https://stackoverflow.com/a/69947060/9636037) – Abhimanyu Nov 12 '21 at 18:45

0 Answers0