Function

forEachChangeReversed

Iterates over all changes in this ChangeList in reverse order of their appearance in the text (from the highest character offset down to the lowest).

BasicTextFieldChangeReverseIterationSample

@Sampled
@Composable
fun BasicTextFieldChangeReverseIterationSample() {
    // Make a text field behave in "insert mode" – inserted text overwrites the text ahead of it
    // instead of being inserted.
    BasicTextField(
        state = rememberTextFieldState(),
        inputTransformation = {
            changes.forEachChangeReversed { range, originalRange ->
                if (!range.collapsed && originalRange.collapsed) {
                    // New text was inserted, delete the text ahead of it.
                    delete(
                        range.end.coerceAtMost(length),
                        (range.end + range.length).coerceAtMost(length),
                    )
                }
            }
        },
    )
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: