What’s the right way of conditionally adding Modif...
# compose
k
What’s the right way of conditionally adding Modifiers? I tried
Copy code
Modifier.padding()
.preferredSize()
.apply {
    if(<some condition>) {
        this.then(Modifier.clickable { })
    }
}
but didn’t work
d
g
Copy code
Modifier.padding()
  .preferredSize()
  .then(if (x) Modifier.clickable(...) else Modifier)
👍 2
k
This worked! Any better way where I could write a lambda and don’t need to return default “Modifier” in else part?
g
No, but you can try make it a bit more readable by not inlining the modifier, e.g.:
Copy code
val click = if (x) {
    Modifier.clickable(...) 
} else {
    Modifier
}

Modifier.padding()
  .preferredSize()
  .then(click)
You see this pattern throughout foundational code
👍 1
a
Use
run
if you want to avoid unnecessary wrapping of the modifier.
k
Okay