I have been trying to show AlertDialog
from onClick
of Card
in dynamic list in Android Jetpack Compose.
I am getting compile time error @Composable invocations can only happen from the context of a @Composable function when doing it.
Below is my code snippet :
@Preview(showBackground = true)
@Composable
fun prepareCard(card: Card) {
MyApplicationTheme() {
androidx.compose.material.Card() {
Column(Modifier.clickable {
Log.d("TAG", "clicked : " + card.name)
val showDialog = mutableStateOf(true)
if (showDialog.value) {
alert(card)
}
...
}
}
}
I have made alert()
composable function which gets called from above code
@Composable
fun alert(card : Card) {
AlertDialog(
title = {
Text("")
},
text = {
Text(text = card.name)
}, onDismissRequest = {
},
confirmButton = {},
dismissButton = {}
)
}
As shown in image, I am getting compile time error
Please help to solve this issue
Note :
I have tried like below modifications, it solved compile time error but it is still not showing AlertDialog
on click of Card
@Preview(showBackground = true)
@Composable
fun prepareCard(card: Card) {
MyApplicationTheme() {
val showDialog = mutableStateOf(false)
if (showDialog.value) {
alert(card)
}
androidx.compose.material.Card() {
Column(Modifier.clickable {
Log.d("kushal", "clicked : " + card.name)
showDialog.value = true
}) {
...
}
}