Does this have any problem? when I reuse it consec...
# codereview
c
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
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
@Jan Thanks for the comment. But sorry I don't get it..