🌈 Create new beautiful Compose Gradients with our new wesite ->
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?,
)

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
@ExperimentalFoundationApi 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 : androidx.compose.runtime.saveable.Saver<TextFieldState, Any>

Saves and restores a TextFieldState for rememberSaveable.

Functions

save

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

restore

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