Kulwinder Singh
11/26/2021, 2:06 AMvar readMore by remember { mutableStateOf(false) }
var clickText by remember { mutableStateOf("Read More") }
var showClickButton by remember { mutableStateOf(false) }
val annotatedText = buildAnnotatedString {
withStyle(style = SpanStyle(color = MaterialTheme.colors.OnSurface800)) {
append(text + " ")
}
if (showClickButton) {
pushStringAnnotation("click", "click")
withStyle(style = SpanStyle(color = MaterialTheme.colors.Interactive500)) {
append(clickText)
}
pop()
}
}
ClickableText(
text = annotatedText,
style = MaterialTheme.typography.smallBody,
overflow = TextOverflow.Ellipsis,
maxLines = if (readMore) Int.MAX_VALUE else 2,
onTextLayout = { showClickButton = it.hasVisualOverflow }
)
I am trying to “Read More” button at the end of text view but looks like buildAnnotatedString
does not recompose when showClickButton
or state change, so here how can i achieve this ?agrosner
11/26/2021, 2:10 AMKulwinder Singh
11/26/2021, 2:19 AMKulwinder Singh
11/26/2021, 2:56 AMZach Klippenstein (he/him) [MOD]
11/29/2021, 1:17 AMderivedStateOf
(although using derivedStateOf
would still probably be more efficient)Zach Klippenstein (he/him) [MOD]
11/29/2021, 1:20 AMKulwinder Singh
12/01/2021, 6:28 AMZach Klippenstein (he/him) [MOD]
12/01/2021, 6:31 PMZach Klippenstein (he/him) [MOD]
12/01/2021, 6:38 PMZach Klippenstein (he/him) [MOD]
12/01/2021, 6:39 PMonTextLayout
is getting called and giving you the values you expect