I have the following Composable
@Composable
fun ClickableBar(text: String, action: () -> Unit) {
Row(
modifier = Modifier
.height(40.dp)
.background(color = AppPrimaryGreen)
.fillMaxWidth()
.clickable { action },
verticalAlignment = Alignment.CenterVertically
) {
Text(text = text)
Icon(
painter = painterResource(R.drawable.icon_arrow_right),
contentDescription = "",
)
}
}
I want to perform action
method when that Row
is tapped but action
gets highlighted and it says The expression is unused
.
What I’m doing wrong??
2
Answers
You need to call it like this:
Since
action
is a lambda function passed toClickableBar
function, you need to include parentheses when invoking it, just like with any other function call.There are actually two ways you can do this.
dev.tejasb proposed the following to invoke the
action
function by adding parentheses:But you can also do this:
Both options provide a value for the
onClick
parameter. While the first option creates a new lambda and callsaction
in it, the second option directly passesaction
as the parameter. Note thatonClick
has the type() -> Unit
, the same type asaction
so you can simply assign the parameter as in option 2.