TextFieldState
A new API in Jetpack Compose for managing text input state. It provides synchronous updates, edit sessions, selection ranges, undo/redo and other capabilities, and is built on the snapshot system for efficient UI updates.
Timeline (last 90 days)
TextFieldState is the text input solution stably provided since Compose 1.7; it updates synchronously as the user types without waiting for an asynchronous round trip, meeting the IME's synchronous requirements.
In addition to the read-only text property, TextFieldState also encapsulates the active editing session, selection range, undo/redo state, and the span styles and paragraph styles used for formatting text.
TextFieldState's internal properties are backed by the Compose snapshot system; when a composable reads the state during composition, layout, or drawing, it automatically subscribes to updates, thereby triggering only the necessary recomposition, layout, or drawing.
The new API introduces InputTransformation, which intercepts user input before it reaches TextFieldState and provides the same API as TextFieldBuffer, including change list capability.
A TextFieldState reference never changes after creation; user input changes its internal properties (such as the text field), so a StateFlow cannot perceive changes to its internal text, and StateFlow cannot be relied upon to observe the text.
When hoisting TextFieldState to a ViewModel, it should be a direct property of the ViewModel rather than being placed in the UI state of an immutable data class, otherwise the copy method will create a new instance and cause loss of selection, cursor, and history.
A ViewModel has no rememberSaveable, so a Saver must be configured manually for TextFieldState to restore state after process death.
In a composable, rememberTextFieldState can be called to create a local TextFieldState, and it also helps with state restoration after process death.
All Facts (8)
TextFieldState is the text input solution stably provided since Compose 1.7; it updates synchronously as the user types without waiting for an asynchronous round trip, meeting the IME's synchronous requirements.
50%UnverifiedIn addition to the read-only text property, TextFieldState also encapsulates the active editing session, selection range, undo/redo state, and the span styles and paragraph styles used for formatting text.
50%UnverifiedTextFieldState's internal properties are backed by the Compose snapshot system; when a composable reads the state during composition, layout, or drawing, it automatically subscribes to updates, thereby triggering only the necessary recomposition, layout, or drawing.
50%UnverifiedThe new API introduces InputTransformation, which intercepts user input before it reaches TextFieldState and provides the same API as TextFieldBuffer, including change list capability.
50%UnverifiedA TextFieldState reference never changes after creation; user input changes its internal properties (such as the text field), so a StateFlow cannot perceive changes to its internal text, and StateFlow cannot be relied upon to observe the text.
50%UnverifiedWhen hoisting TextFieldState to a ViewModel, it should be a direct property of the ViewModel rather than being placed in the UI state of an immutable data class, otherwise the copy method will create a new instance and cause loss of selection, cursor, and history.
50%UnverifiedA ViewModel has no rememberSaveable, so a Saver must be configured manually for TextFieldState to restore state after process death.
50%UnverifiedIn a composable, rememberTextFieldState can be called to create a local TextFieldState, and it also helps with state restoration after process death.
50%