State of Compose 2023 results are in! Click here to learn more

← Back to Foundation

BasicTextField2

Component
in
Foundation
. Since 1.5.0-alpha01

Overview

Code Examples

BasicTextField2 is a new text input Composable under heavy development. Please refrain from using it in production since it has a very unstable API and implementation for the time being. Many core features like selection, cursor, gestures, etc. may fail or simply not exist.

@param state State object that holds the internal state of a BasicTextField2 @param modifier optional Modifier for this text field. @param enabled controls the enabled state of the BasicTextField2. When false, the text field will be neither editable nor focusable, the input of the text field will not be selectable. @param readOnly controls the editable state of the BasicTextField2. When true, the text field can not be modified, however, a user can focus it and copy text from it. Read-only text fields are usually used to display pre-filled forms that user can not edit @param textStyle Style configuration for text content that's displayed in the editor. @param interactionSource the MutableInteractionSource representing the stream of Interactions for this TextField. You can create and pass in your own remembered MutableInteractionSource if you want to observe Interactions and customize the appearance / behavior of this TextField for different Interactions. @param cursorBrush Brush to paint cursor with. If SolidColor with Color.Unspecified provided, there will be no cursor drawn @param minLines The minimum height in terms of minimum number of visible lines. It is required that 1 <= minLines <= maxLines. @param maxLines The maximum height in terms of maximum number of visible lines. It is required that 1 <= minLines <= maxLines. @param scrollState Scroll state that manages either horizontal or vertical scroll of TextField. If maxLines is 1, this TextField is treated as single line which activates horizontal scroll behavior. In other cases TextField becomes vertically scrollable. @param keyboardOptions software keyboard options that contains configuration such as KeyboardType and ImeAction. @param onTextLayout Callback that is executed when a new text layout is calculated. A TextLayoutResult object that callback provides contains paragraph information, size of the text, baselines and other details. The callback can be used to add additional decoration or functionality to the text. For example, to draw a cursor or selection around the text. Density scope is the one that was used while creating the given text layout. @param decorationBox Composable lambda that allows to add decorations around text field, such as icon, placeholder, helper messages or similar, and automatically increase the hit target area of the text field. To allow you to control the placement of the inner text field relative to your decorations, the text field implementation will pass in a framework-controlled composable parameter "innerTextField" to the decorationBox lambda you provide. You must call innerTextField exactly once.

Overloads

BasicTextField2

@ExperimentalFoundationApi
@OptIn(InternalFoundationTextApi::class)
@Composable
fun BasicTextField2(
    state: TextFieldState,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    textStyle: TextStyle = TextStyle.Default,
    interactionSource: MutableInteractionSource? = null,
    cursorBrush: Brush = SolidColor(Color.Black),
    minLines: Int = DefaultMinLines,
    maxLines: Int = Int.MAX_VALUE,
    scrollState: ScrollState = rememberScrollState(),
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    onTextLayout: Density.(TextLayoutResult) -> Unit = {},
    decorationBox: @Composable (innerTextField: @Composable () -> Unit) -> Unit =
        @Composable { innerTextField -> innerTextField() }
)

Parameters

NameDescription
stateState object that holds the internal state of a BasicTextField2
modifieroptional Modifier for this text field.
enabledcontrols the enabled state of the BasicTextField2. When false, the text field will be neither editable nor focusable, the input of the text field will not be selectable.
readOnlycontrols the editable state of the BasicTextField2. When true, the text field can not be modified, however, a user can focus it and copy text from it. Read-only text fields are usually used to display pre-filled forms that user can not edit
textStyleStyle configuration for text content that's displayed in the editor.
interactionSourcethe MutableInteractionSource representing the stream of Interactions for this TextField. You can create and pass in your own remembered MutableInteractionSource if you want to observe Interactions and customize the appearance / behavior of this TextField for different Interactions.
cursorBrushBrush to paint cursor with. If SolidColor with Color.Unspecified provided, there will be no cursor drawn
minLinesThe minimum height in terms of minimum number of visible lines. It is required that 1 <= minLines <= maxLines.
maxLinesThe maximum height in terms of maximum number of visible lines. It is required that 1 <= minLines <= maxLines.
scrollStateScroll state that manages either horizontal or vertical scroll of TextField. If maxLines is 1, this TextField is treated as single line which activates horizontal scroll behavior. In other cases TextField becomes vertically scrollable.
keyboardOptionssoftware keyboard options that contains configuration such as KeyboardType and ImeAction.
onTextLayoutCallback that is executed when a new text layout is calculated. A TextLayoutResult object that callback provides contains paragraph information, size of the text, baselines and other details. The callback can be used to add additional decoration or functionality to the text. For example, to draw a cursor or selection around the text. Density scope is the one that was used while creating the given text layout.
decorationBoxComposable lambda that allows to add decorations around text field, such as icon, placeholder, helper messages or similar, and automatically increase the hit target area of the text field. To allow you to control the placement of the inner text field relative to your decorations, the text field implementation will pass in a framework-controlled composable parameter "innerTextField" to the decorationBox lambda you provide. You must call innerTextField exactly once