Class

TextFieldState

Manages editable text, selection, and cursor state for a text field.

Source set: Common
public class TextFieldState internal constructor(
    initialText: String,
    initialSelection: TextRange,
    initialTextUndoManager: TextUndoManager,
    initialTextStyles: TextFieldTextStylesImpl?,
    onTextChanged: ((CharSequence) -> Unit)?,
)

Manages editable text, selection, and cursor state for a text field.

Modify state programmatically using edit, setTextAndSelectAll, setTextAndPlaceCursorAtEnd, or clearText. Read state (text, selection, composition) directly inside Composable functions, or use snapshotFlow to observe changes from outside composition.

Use rememberTextFieldState in composables to automatically save and restore state. For more control over state restoration, use Saver.

Properties

text

Source set: Common
public val text: CharSequence

The current text content. This value will automatically update when the user enters text or otherwise changes the text field contents. To change it programmatically, call edit.

To observe changes to this property outside a restartable function, use snapshotFlow { text }.

selection

Source set: Common
public val selection: TextRange

The current selection range. If the selection is collapsed, it represents cursor location. This value will automatically update when the user enters text or otherwise changes the text field selection range. To change it programmatically, call edit.

To observe changes to this property outside a restartable function, use snapshotFlow { selection }.

composition

Source set: Common
public val composition: TextRange?

The current composing range dictated by the IME. If null, there is no composing region.

To observe changes to this property outside a restartable function, use snapshotFlow { composition }.

textStyles

Source set: Common
public val textStyles: TextFieldTextStyles

Provides access to the styles applied to the text within this TextFieldState.

Use this property when you only need to read or observe the current text styles, such as updating a formatting toolbar based on the style of the currently selected text. If you need to modify the text or its styles, use the edit method instead, which provides both read and write access via TextFieldBuffer.

To observe changes to this property outside a restartable function, use snapshotFlow { textStyles }.

undoState

Source set: Common
public val undoState: UndoState = UndoState(this)

Manages undo and redo history for this state.

Functions

edit

Source set: Common
public inline fun edit(block: TextFieldBuffer.() -> Unit)

Runs block to edit text, selection, and cursor state.

Use TextFieldBuffer operations inside the block to modify content. Avoid calling edit concurrently or recursively to prevent IllegalStateException.

toString

Source set: Common
override fun toString(): String

Members

Saver

Source set: Common
public object Saver : ComposeSaver<TextFieldState, Any>

Saves and restores a TextFieldState for rememberSaveable.

A restored state has no onTextChanged callback. To restore a state that was created with onTextChanged, use the Saver returned by TextFieldState.Saver(onTextChanged).

Functions

save

Source set: Common
override fun SaverScope.save(value: TextFieldState): Any?

restore

Source set: Common
override fun restore(value: Any): TextFieldState?

Companion

Source set: Common
public companion object

Functions

Saver

Source set: Common
public fun Saver(onTextChanged: (CharSequence) -> Unit): ComposeSaver<TextFieldState, Any>

Returns a Saver that saves a TextFieldState the same way as TextFieldState.Saver and passes onTextChanged to the restored state.

A lambda cannot be saved, so a state restored by TextFieldState.Saver has no onTextChanged. Use this function to save a state that was created with onTextChanged, for example with rememberSaveable(saver = TextFieldState.Saver(onTextChanged)). rememberTextFieldState does this.

onTextChanged is called after each commit that changes the text of the restored state: user input, TextFieldState.edit and its helpers, and undo/redo. It is not called when only the selection, composition, or styles change. onTextChanged runs on the thread that made the edit, before the edit call returns. When onTextChanged runs, any InputTransformation has been applied, TextFieldState.text and TextFieldState.selection show the new value, the undo entry is recorded, and the IME has been notified.

onTextChanged must not change the state. Calling TextFieldState.edit, its helpers, or UndoState.undo and UndoState.redo on the state from onTextChanged throws an IllegalStateException. To intercept and modify user input, use InputTransformation.

The CharSequence passed to onTextChanged is provided for convenience and has the same text content as TextFieldState.text.

Parameters

onTextChanged Called after each commit that changes the text of the restored state, receiving the new text with the same content as TextFieldState.text.
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: