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
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
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
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
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
@ExperimentalFoundationApi public val undoState: UndoState = UndoState(this)
Manages undo and redo history for this state.
Functions
edit
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
override fun toString(): String
Members
Saver
public object Saver : androidx.compose.runtime.saveable.Saver<TextFieldState, Any>
Saves and restores a TextFieldState for rememberSaveable.
Functions
save
override fun SaverScope.save(value: TextFieldState): Any?
restore
override fun restore(value: Any): TextFieldState?