One Off the Slack: Conditional Modifiers

Nat Strangerweather asked:

is it possible to have modifiers under certain conditions only? For instance, here: is it possible to have modifiers under certain conditions only? For instance, here:

Card(
    Modifier
        .clickable(indication = null, onClick = {}

I want my card to be clickable only if the “isEnabled” Boolean is true. Something like .if (isEnabled) clickable()…

Modifier offers a builder-style API, where Modifier functions almost always return a Modifier for the purposes of chaining things together. Typically, we see Modifier chains as a simple chain of calls:

Modifier
  .size(64.dp)
  .clickable(onClick = {})
  .padding(8.dp)

However, Louis points out that:

On the surface, those seem to be… less than completely useful. However, they enable fairly simple syntax for Nat’s problem:

Modifier
  .size(64.dp)
  .then(if (enabled) Modifier.clickable(onClick = {}) else Modifier)
  .padding(8.dp)

Read the original thread in the kotlinlang Slack workspace. Not a member? Join that Slack workspace here!