I wish uriHandler.openUri(annotation.item) can be fired when I don't pass a block paramater to AnnotatedURLText, and block() can be fired when I pass one to it.
But I find uriHandler.openUri(annotation.item) isn't fired when I run Call A, B or C, how can I fire it?
Code 1
@Composable
fun AnnotatedURLText(
...
block: ()->Unit = {}
) {
val uriHandler = LocalUriHandler.current
...
ClickableText(
...
onClick = { offset ->
annotatedText.getStringAnnotations(tag = "URL", start = offset, end = offset)
.firstOrNull()?.let { annotation ->
if (block=={}) {
uriHandler.openUri(annotation.item) //It can't be fired
}else{
block()
}
}
}
)
}
Call A
AnnotatedURLText(...)
{
todo()
}
Call B
AnnotatedURLText(...)
Call C
AnnotatedURLText(...)
{
}
Added Content
To Fogmeister: Thanks!
I write Code 2 based your ideas, but I get the following error.
@Composable invocations can only happen from the context of a @Composable function
Code 2
@Composable
fun AnnotatedURLText(
...
block: (String) -> Unit = {
LocalUriHandler.current.openUri(it) //It cause error
}
) {
...
ClickableText(
...
onClick = { offset ->
annotatedText.getStringAnnotations(tag = "URL", start = offset, end = offset)
.firstOrNull()?.let { annotation ->
block(annotation.item)
}
}
)
}