# TextFieldState

Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/input/TextFieldState.kt+class:androidx.compose.foundation.text.input.TextFieldState) Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0) Common/All
- Cmn

  ```
  class TextFieldState
  ```

*** ** * ** ***

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

Modify state programmatically using `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndSelectAll(kotlin.String)`, `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndPlaceCursorAtEnd(kotlin.String)`, or `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).clearText()`. Read state (`https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#text()`, `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#selection()`, `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#composition()`) directly inside Composable functions, or use `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#snapshotFlow(kotlin.Function0)` to observe changes from outside composition.

Use `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/rememberTextFieldState.composable#rememberTextFieldState(kotlin.String,androidx.compose.ui.text.TextRange)` in composables to automatically save and restore state. For more control over state restoration, use `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState.Saver`.

```kotlin
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.sp

class SearchViewModel(val searchFieldState: TextFieldState = TextFieldState()) {
    private val queryValidationRegex = """\w+""".toRegex()

    // Use derived state to avoid recomposing every time the text changes, and only recompose
    // when the input becomes valid or invalid.
    val isQueryValid by derivedStateOf {
        // This lambda will be re-executed every time inputState.text changes.
        searchFieldState.text.matches(queryValidationRegex)
    }

    var searchResults: List<String> by mutableStateOf(emptyList())
        private set

    /** Called while the view model is active, e.g. from a LaunchedEffect. */
    suspend fun run() {
        snapshotFlow { searchFieldState.text }
            .collectLatest { queryText ->
                // Start a new search every time the user types something valid. If the previous
                // search is still being processed when the text is changed, it will be
                // cancelled
                // and this code will run again with the latest query text.
                if (isQueryValid) {
                    searchResults = performSearch(query = queryText)
                }
            }
    }

    fun clearQuery() {
        searchFieldState.setTextAndPlaceCursorAtEnd("")
    }

    private suspend fun performSearch(query: CharSequence): List<String> {
        TODO()
    }
}

@Composable
fun SearchScreen(viewModel: SearchViewModel) {
    Column {
        Row {
            BasicTextField(viewModel.searchFieldState)
            IconButton(onClick = { viewModel.clearQuery() }) {
                Icon(Icons.Default.Clear, contentDescription = "clear search query")
            }
        }
        if (!viewModel.isQueryValid) {
            Text("Invalid query", style = TextStyle(color = Color.Red))
        }
        LazyColumn { items(viewModel.searchResults) { TODO() } }
    }
}
```

## Summary

| ### Nested types |
|---|
| `object https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState.Saver : https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver<https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>` Saves and restores a `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState` for `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)`. |

| ### Public constructors |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/annotation/RememberInComposition https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#TextFieldState(kotlin.String,androidx.compose.ui.text.TextRange)(initialText: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, initialSelection: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextRange)` | Cmn |

| ### Public functions |
|---|---|---|
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)(block: https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Runs `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)` to edit text, selection, and cursor state. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#toString()()` | Cmn |

| ### Public properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextRange?` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#composition()` The current composing range dictated by the IME. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextRange` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#selection()` The current selection range. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#text()` The current text content. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldTextStyles` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#textStyles()` Provides access to the styles applied to the text within this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState`. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/UndoState` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#undoState()` Manages undo and redo history for this state. | Cmn |

| ### Extension functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState.https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#(androidx.compose.foundation.text.input.TextFieldState).clearText()()` Deletes all the text in the state. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState.https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#(androidx.compose.foundation.text.input.TextFieldState).setTextAndPlaceCursorAtEnd(kotlin.String)(text: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Sets the text in this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState` to `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndPlaceCursorAtEnd(kotlin.String)`, replacing any text that was previously there, and places the cursor at the end of the new text. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState.https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#(androidx.compose.foundation.text.input.TextFieldState).setTextAndSelectAll(kotlin.String)(text: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Sets the text in this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState` to `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndSelectAll(kotlin.String)`, replacing any text that was previously there, and selects all the text. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState.https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#(androidx.compose.foundation.text.input.TextFieldState).toTextFieldBuffer()()` Creates a temporary, mutable `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer` representing the current state of this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState`. | Cmn |

## Public constructors

### TextFieldState

Cmn Added in [1.9.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.9.0)

```
@RememberInComposition
TextFieldState(
    initialText: String = "",
    initialSelection: TextRange = TextRange(initialText.length)
)
```

## Public functions

### edit

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0)

```
inline fun edit(block: TextFieldBuffer.() -> Unit): Unit
```

Runs `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)` to edit text, selection, and cursor state.

Use `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer` operations inside the block to modify content. Avoid calling `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)` concurrently or recursively to prevent `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html`.

```kotlin
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.delete
import androidx.compose.foundation.text.input.insert
import androidx.compose.material3.Text
import androidx.compose.ui.text.TextRange

val state = TextFieldState("hello world!")
state.edit {
    // Insert a comma after "hello".
    insert(5, ",") // = "hello, world!"

    // Delete the exclamation mark.
    delete(12, 13) // = "hello, world"

    // Add a different name.
    insert(length, "Compose") // = "hello, Compose"

    // Say goodbye.
    replace(0, 5, "goodbye") // "goodbye, Compose"

    // Select the new name so the user can change it by just starting to type.
    selection = TextRange(9, 16) // "goodbye, ̲C̲o̲m̲p̲o̲s̲e"
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndPlaceCursorAtEnd(kotlin.String)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndSelectAll(kotlin.String)` |   |

### toString

Cmn

```
open fun toString(): String
```

## Public properties

### composition

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0)

```
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 }`.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#snapshotFlow(kotlin.Function0)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldCharSequence#composition()` |   |

### selection

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0)

```
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 `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)`.

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

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#snapshotFlow(kotlin.Function0)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldCharSequence#selection()` |   |

### text

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0)

```
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 `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)`.

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

```kotlin
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.unit.sp

class SearchViewModel {
    val searchFieldState = TextFieldState()
    var searchResults: List<String> by mutableStateOf(emptyList())
        private set

    /** Called while the view model is active, e.g. from a LaunchedEffect. */
    suspend fun run() {
        snapshotFlow { searchFieldState.text }
            // Let fast typers get multiple keystrokes in before kicking off a search.
            .debounce(500)
            // collectLatest cancels the previous search if it's still running when there's a
            // new change.
            .collectLatest { queryText -> searchResults = performSearch(query = queryText) }
    }

    private suspend fun performSearch(query: CharSequence): List<String> {
        TODO()
    }
}

@Composable
fun SearchScreen(viewModel: SearchViewModel) {
    Column {
        BasicTextField(viewModel.searchFieldState)
        LazyColumn { items(viewModel.searchResults) { TODO() } }
    }
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#snapshotFlow(kotlin.Function0)` |   |

### textStyles

Cmn Added in [1.12.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.12.0)

```
val textStyles: TextFieldTextStyles
```

Provides access to the styles applied to the text within this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/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 `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)` method instead, which provides both read and write access via `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer`.

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

```kotlin
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.ExpandPolicy
import androidx.compose.foundation.text.input.TextFieldBuffer
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.Button
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.Text
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp

// This sample demonstrates a realistic rich-text editor scenario using the `TrackedRange` and
// `TextFieldTextStyles` APIs. It implements a "Toggle Bold" formatting function on the current
// selection.

// For simplicity, this sample keeps bold styles non-overlapping and contiguous, assuming they
// are
// applied exclusively through this method.
val state = rememberTextFieldState("Hello World")

// This derived state calculates whether the current selection is completely covered by
// bold text styles. This ensures the "Bold" toggle button accurately reflects the
// state of the selected text.
val isSelection100PercentBold by remember {
    derivedStateOf {
        val selection = state.selection
        if (selection.collapsed) {
            false
        } else {
            val spanStyles = state.textStyles.getSpanStyles(selection)
            var boldCoverage = 0
            for (style in spanStyles) {
                if (style.item.fontWeight == FontWeight.Bold) {
                    val overlapStart = maxOf(style.start, selection.min)
                    val overlapEnd = minOf(style.end, selection.max)
                    if (overlapEnd > overlapStart) {
                        boldCoverage += (overlapEnd - overlapStart)
                    }
                }
            }
            boldCoverage == selection.length
        }
    }
}

fun TextFieldBuffer.unBoldSelection() {
    // Query existing bold styles in the selection
    val intersectingStyles =
        getSpanStyles(selection).filter { it.spanStyle.fontWeight == FontWeight.Bold }
    // We modify or remove existing styles to exclude the selected range
    for (style in intersectingStyles) {
        val range = style.textRange
        if (range.start >= selection.min && range.end <= selection.max) {
            // The style is fully inside the selection. Remove it.
            removeStyle(style)
        } else if (range.start < selection.min && range.end > selection.max) {
            // The style completely covers the selection. We need to split it.
            val oldEnd = range.end
            // Truncate the start part
            style.textRange = TextRange(range.start, selection.min)
            // Add a new style for the end part
            addStyle(
                SpanStyle(fontWeight = FontWeight.Bold),
                TextRange(selection.max, oldEnd),
                ExpandPolicy.AtEnd,
            )
        } else if (range.start < selection.min) {
            // The style overlaps with the start of the selection. Truncate it.
            style.textRange = TextRange(range.start, selection.min)
        } else {
            // The style overlaps with the end of the selection. Truncate it.
            style.textRange = TextRange(selection.max, range.end)
        }
    }
}

fun TextFieldBuffer.boldSelection() {
    // Query existing bold styles in the selection
    val intersectingStyles =
        getSpanStyles(selection).filter { it.spanStyle.fontWeight == FontWeight.Bold }
    // To keep bold styles non-overlapping, we merge any intersecting bold
    // styles with the new selection range into a single contiguous bold style.
    var mergedStart = selection.min
    var mergedEnd = selection.max

    for (style in intersectingStyles) {
        mergedStart = minOf(mergedStart, style.textRange.start)
        mergedEnd = maxOf(mergedEnd, style.textRange.end)
        // Remove the fragmented style
        removeStyle(style)
    }

    addStyle(
        SpanStyle(fontWeight = FontWeight.Bold),
        TextRange(mergedStart, mergedEnd),
        ExpandPolicy.AtEnd,
    )
}

Column {
    Button(
        onClick = {
            state.edit {
                val selection = this.selection
                if (selection.collapsed) return@edit
                if (isSelection100PercentBold) {
                    unBoldSelection()
                } else {
                    boldSelection()
                }
            }
        }
    ) {
        Text(
            "B",
            fontWeight = if (isSelection100PercentBold) FontWeight.Bold else FontWeight.Normal,
        )
    }

    BasicTextField(state = state, textStyle = LocalTextStyle.current)
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#snapshotFlow(kotlin.Function0)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldTextStyles` |   |

### undoState

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0)

```
val undoState: UndoState
```

Manages undo and redo history for this state.

```kotlin
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

val state = rememberTextFieldState()

Column(Modifier.padding(8.dp)) {
    Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
        Button(onClick = { state.undoState.undo() }, enabled = state.undoState.canUndo) {
            Text("Undo")
        }

        Button(onClick = { state.undoState.redo() }, enabled = state.undoState.canRedo) {
            Text("Redo")
        }

        Button(
            onClick = { state.undoState.clearHistory() },
            enabled = state.undoState.canUndo || state.undoState.canRedo,
        ) {
            Text("Clear History")
        }
    }

    BasicTextField(
        state = state,
        modifier =
            Modifier.fillMaxWidth()
                .border(1.dp, Color.LightGray, RoundedCornerShape(6.dp))
                .padding(8.dp),
        textStyle = TextStyle(fontSize = 16.sp),
    )
}
```

## Extension functions

### TextFieldState.clearText

Cmn Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/input/TextFieldState.kt+function:clearText)

```
fun TextFieldState.clearText(): Unit
```

Deletes all the text in the state.

To perform more complicated edits on the text, call `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)`. This function is equivalent to calling:

```kotlin
edit {
  delete(0, length)
  placeCursorAtEnd()
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndPlaceCursorAtEnd(kotlin.String)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndSelectAll(kotlin.String)` |   |

### TextFieldState.setTextAndPlaceCursorAtEnd

Cmn Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/input/TextFieldState.kt+function:setTextAndPlaceCursorAtEnd)

```
fun TextFieldState.setTextAndPlaceCursorAtEnd(text: String): Unit
```

Sets the text in this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState` to `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndPlaceCursorAtEnd(kotlin.String)`, replacing any text that was previously there, and places the cursor at the end of the new text.

To perform more complicated edits on the text, call `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)`. This function is equivalent to calling:

```kotlin
edit {
  replace(0, length, text)
  placeCursorAtEnd()
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndSelectAll(kotlin.String)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).clearText()` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldBuffer).placeCursorAtEnd()` |   |

### TextFieldState.setTextAndSelectAll

Cmn Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/input/TextFieldState.kt+function:setTextAndSelectAll)

```
fun TextFieldState.setTextAndSelectAll(text: String): Unit
```

Sets the text in this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState` to `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndSelectAll(kotlin.String)`, replacing any text that was previously there, and selects all the text.

To perform more complicated edits on the text, call `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)`. This function is equivalent to calling:

```kotlin
edit {
  replace(0, length, text)
  selectAll()
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).setTextAndPlaceCursorAtEnd(kotlin.String)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldState).clearText()` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldBuffer).selectAll()` |   |

### TextFieldState.toTextFieldBuffer

Cmn Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/input/TextFieldState.kt+function:toTextFieldBuffer)

```
fun TextFieldState.toTextFieldBuffer(): TextFieldBuffer
```

Creates a temporary, mutable `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer` representing the current state of this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState`.

Use a `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer` to:

- Apply transformations for testing purposes

- Preview how the TextField would render with a specific `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/OutputTransformation`

This is similar to calling `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)`, but without committing the changes back to the `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState`.

**Important:** A `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer` is intended for short-term use. Let the garbage collector dispose of it when you're finished to avoid unnecessary memory usage.

```kotlin
import androidx.compose.foundation.text.input.OutputTransformation
import androidx.compose.foundation.text.input.TextFieldBuffer
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.insert
import androidx.compose.foundation.text.input.toTextFieldBuffer
import androidx.compose.material3.Text

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
```