https://kotlinlang.org logo
#compose
Title
# compose
n

Nat Strangerweather

12/07/2020, 7:23 PM
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()
...
z

Zach Klippenstein (he/him) [MOD]

12/07/2020, 7:26 PM
yes, verbosely:
Copy code
var modifier = Modifier
if (isEnabled) modifier = modifier.clickable(…)
You could use
let
or something too
i usually pull modifiers out at this point though since the function you’re passing them to gets a bit gnarly with this much logic between the parentheses
👍 1
n

Nat Strangerweather

12/07/2020, 7:27 PM
oh nice, thanks, I tried lots of things but not that lol!
a

Adriano Celentano

12/07/2020, 8:50 PM
u could make your own modifier extension function, which only adds the modifier to the chain if the condition is met. u could use maybe... as a name prefix for the function
👍 1
n

Nat Strangerweather

12/07/2020, 9:03 PM
The problem I have is that I have other modifier parameters like padding, size, scale etc, but these need to be implemented whatever the condition, whether isEnabled is true or false.
l

Louis Pullen-Freilich [G]

12/07/2020, 9:06 PM
You can also use the
then
function:
Copy code
Modifier
    .then(if (enabled) Modifier.clickable() else Modifier)
    .something()
    .else()
n

Nat Strangerweather

12/07/2020, 9:09 PM
@Louis Pullen-Freilich [G] Fabulous, that worked! Thanks!
a

Adriano Celentano

12/07/2020, 10:15 PM
I should have been more clear, with adding it to the chain I meant using the then function
n

Nat Strangerweather

12/07/2020, 10:28 PM
Ah I see, makes sense!