I tried making an extension function on Modifier to apply changes to it only if a condition was true, since quite frequently I need to make small changes to a Modifier object in case certain things happen. But the code is not working as expected and I can’t seem to understand why.
This is the extension function
inline fun Modifier.conditional(
condition: Boolean,
ifTrue: Modifier.() -> Modifier
): Modifier = if (condition) {
ifTrue(this)
} else {
this
}
And this is how I am using it:
@Composable
fun PlusMinusButton(
text: String,
enabled: Boolean,
onClick: () -> Unit
) {
Text(
modifier = Modifier
.width(60.dp)
.background(
color = if (enabled) Blue02 else Gray1,
shape = RoundedCornerShape(8.dp)
)
.conditional(enabled) {
clickable { onClick() }
},
text = text,
textAlign = TextAlign.Center,
fontSize = 32.sp,
color = if (enabled) Blue06 else Blue02
)
}
And this is how it’s looking on the preview:
Whenever the conditional is true, it seems the rest of the modifier is ignored, and the background doesn’t get applied. Any ideas what can be causing this?