Class

UndoState

Controls the undo and redo history for a TextFieldState.

BasicTextFieldUndoSample

@Sampled
@Composable
fun BasicTextFieldUndoSample() {
    val state = rememberTextFieldState()

    Column(Modifier.padding(8.dp)) {
        Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
            Button(onClick = { state.undoState.undo() }, enabled = state.undoState.canUndo) {
                Text("Undo")
            }

            Button(onClick = { state.undoState.redo() }, enabled = state.undoState.canRedo) {
                Text("Redo")
            }

            Button(
                onClick = { state.undoState.clearHistory() },
                enabled = state.undoState.canUndo || state.undoState.canRedo,
            ) {
                Text("Clear History")
            }
        }

        BasicTextField(
            state = state,
            modifier =
                Modifier.fillMaxWidth()
                    .border(1.dp, Color.LightGray, RoundedCornerShape(6.dp))
                    .padding(8.dp),
            textStyle = TextStyle(fontSize = 16.sp),
        )
    }
}
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: