is it possible to have modifiers under certain con...
# compose
n
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
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
oh nice, thanks, I tried lots of things but not that lol!
a
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
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
You can also use the
then
function:
Copy code
Modifier
    .then(if (enabled) Modifier.clickable() else Modifier)
    .something()
    .else()
n
@Louis Pullen-Freilich [G] Fabulous, that worked! Thanks!
a
I should have been more clear, with adding it to the chain I meant using the then function
n
Ah I see, makes sense!