https://kotlinlang.org logo
#codereview
Title
# codereview
c

chanjungskim

03/09/2023, 4:42 AM
Does this have any problem? when I reuse it consecutively, it cause out of index. I guess because the previous value remains.
here's my full code:
Copy code
@Composable
fun TypeAnimationText(
    text: String,
    duration: Long,
    modifier: Modifier = Modifier,
    style: TextStyle = TextStyle.Default,
    color: androidx.compose.ui.graphics.Color = Color.Black,
) {
    val transition = updateTransition(targetState = text, label = text)
    val caretIndex by transition.animateInt(
        transitionSpec = {
            tween(durationMillis = duration.toInt()*text.length)
        },
        label = "caretIndex"
    ) { targetText ->
        Log.d("aos", "targetText: $targetText")
        targetText.length
    }

    // decompose caretIndex to 0


    Text(
        text = buildAnnotatedString {
            withStyle(style = SpanStyle(color = color)) {
                append(text.substring(0, caretIndex))
            }
            withStyle(style = SpanStyle(color = Color.Transparent)) {
                append(text.substring(caretIndex, text.length))
            }
        },
        style = style,
        modifier = modifier
    )
}
j

Jan

04/12/2023, 9:53 PM
Does it help to change text parameter in function signature to mutableState of string or have the caret index deriveOf text or something? It's possible that the text deviates from caret index on recompositions when animated transition gets remembered.
c

chanjungskim

04/13/2023, 5:10 AM
@Jan Thanks for the comment. But sorry I don't get it..
5 Views