toTextFieldBuffer

Function

Common
fun TextFieldState.toTextFieldBuffer(): TextFieldBuffer

Creates a temporary, mutable TextFieldBuffer representing the current state of this TextFieldState.

Use a TextFieldBuffer to:

  • Apply transformations for testing purposes
  • Preview how the TextField would render with a specific OutputTransformation

This is similar to calling TextFieldState.edit, but without committing the changes back to the TextFieldState.

Important: A TextFieldBuffer is intended for short-term use. Let the garbage collector dispose of it when you're finished to avoid unnecessary memory usage.

Code Examples

TextFieldStateApplyOutputTransformation

@Suppress("UNUSED_VARIABLE")
@Composable
fun TextFieldStateApplyOutputTransformation() {
    val state = TextFieldState("Hello, World")
    val outputTransformation = OutputTransformation { insert(0, "> ") }
    val buffer = state.toTextFieldBuffer()
    with(outputTransformation) { buffer.transformOutput() }
    val transformedText = buffer.asCharSequence()
    val transformedSelection = buffer.selection
}