Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DecorationBox in Jetpack Compose BasicTextField

I use jetpack compose create a editText,I want to show a hint like before "android:hint", so I try to use decorationBox,but after I created it the input isn't displayed and the log can display my input content. this is my code,

val passState= remember { mutableStateOf(TextFieldValue("")) }
BasicTextField(
    decorationBox = {
        Text("password",color = loginGrayColor)
    },
    value = passState.value,
    onValueChange = { passState.value = it ; Log.d("password",it.text) },
    singleLine = true,
    maxLines = 1,
    textStyle = TextStyle(
        fontSize = 15.sp,
        color = loginInputTextColor
    ),
    modifier = Modifier
        .padding(start = 10.dp, top = 10.dp)
        .height(20.dp)
)
like image 758
xinyunlk Avatar asked Dec 30 '22 15:12

xinyunlk


1 Answers

You have to add the innerTextField provided by the decorationBox.
Something like:

var value by remember { mutableStateOf(TextFieldValue("")) }
BasicTextField(
    value = value,
    onValueChange = { value = it },
    decorationBox = { innerTextField ->
        Row(
            Modifier
                .background(Color.LightGray, RoundedCornerShape(percent = 30))
                .padding(16.dp)
        ) {

            if (value.text.isEmpty()) {
                Text("Label")
            }
            innerTextField()  //<-- Add this
        }
    },
)

If you would like to have the cursor start at the beginning of the placeholder label, put the decorationBox content inside a Box rather than a Row.

like image 140
Gabriele Mariotti Avatar answered Mar 19 '23 14:03

Gabriele Mariotti