简体   繁体   中英

How to call a composable function in onClick event

This is a composable function and I want to do that when I click on cardview then article will open in android view but didn't understand how to achieve this. I also try with lamda but not got success.

I Want to call ShowOnWebView composable function when NewsCardView(Card View) function clicked but compiler shows an error "@Composable invocations can only happen from the context of a @Composable function" how to call my function when cardview will clicked

Kindly help and thanks in andvance

@Composable
fun NewsCardView(
article: Article,
modifier: Modifier = Modifier,
) {
Card(
    modifier = modifier
        .height(150.dp)
        .fillMaxWidth()
        .padding(2.dp)
        .clickable {
       // I Want to call ShowOnWebView composable function but compiler 
       shows an error "@Composable invocations can only happen from the context of a 
                       @Composable function"
       how to call my function when cardview will clicked
        },
    elevation = 5.dp,
    backgroundColor = Color.White
) {
    val painter = rememberImagePainter(data = article.urlToImage) {
        crossfade(1000)
        error(R.drawable.ic_placeholder)
        placeholder(R.drawable.ic_placeholder)

    }
    Row(
        modifier = Modifier
            .fillMaxSize(),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceEvenly
    ) {
        Column(
            modifier = Modifier.fillMaxWidth(.3f),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(
                modifier = Modifier.size(100.dp),
                painter = painter,
                contentDescription = "News",
                contentScale = ContentScale.Crop,
            )
            Text(
                text = article.author ?: "",
                fontSize = 12.sp,
            )
            Text(
                text = article.publishedAt,
                fontSize = 12.sp,
            )
        }
        Column(
            modifier = Modifier.fillMaxWidth(.7f),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(modifier = Modifier.fillMaxWidth(),
                text = article.title,
                color = Color.Black,
                fontWeight = FontWeight.Bold,
                fontSize = 16.sp
            )
            Text(modifier = Modifier.fillMaxWidth(),
                text = article.description ?: "",
                color = Color.Black,
                fontWeight = FontWeight.Medium,
                fontSize = 14.sp
            )
        }
    }

    }

    }

This composable function display an article in androidView

   @Composable
   fun ShowOnWebView(url:String) {
   val context= LocalContext.current

    AndroidView(factory = {
    WebView(context).apply {
        webViewClient= WebViewClient()
        loadUrl(url)
    }
   })
   }

You can't call set a Composable inside your non Composable scope. What you should do is have a State with boolean and set it to true when you want to show your composable.

var showWebView by remember {mutableState(false)}

Modifier.clickable {
  showWebView = true
}

if(showWebView) {
  ShowOnWebView(someUrl)
}

This is also how we display dialogs or conditional Composable such as Loading, Result, Error. It's also used for expanded/shrinked Composables too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM