Jetpack Compose – How can we call a @Composable function inside an onClick()

So you’re right, composable functions cannot be called from within onClicks from either a button or a modifier. So you need to create a value like:

private val showDialog = mutableStateOf(false)

When set to true you want to invoke the composable code, like:

if(showDialog.value) {
    alert()
}

Alert being something like:

@Composable
fun alert() {
    AlertDialog(
        title = {
            Text(text = "Test")
        },
        text = {
            Text("Test")
        },
        onDismissRequest = {

        },
        buttons = {
            Button(onClick = { showDialog.value = false }) {
                Text("test")
            }
        }

    )
}

Now finish with changing the boolean where intended, like:

Box(Modifier.clickable(
    onClick = {
        showDialog.value = true
    }
))

I hope this explanation helps, of course the value doesn’t have to be a boolean, but you get the concept :).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)