-1

I am learning Compose and playing with it. My intention is to create custom textfield with hint animating above textfield itself, but I do not want to use label callback. Below is my composable used in column twice, however that gives me different bottom border line width. Why is that?

@Composable
fun TextfieldCustom() {
    var text by remember { mutableStateOf(("")) }
    var hint by remember { mutableStateOf(("")) }
    var isClicked by remember { mutableStateOf((false)) }

    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Box(modifier = Modifier.height(20.dp).align(Alignment.Start)) {
            this@Column.AnimatedVisibility(
                visible = isClicked,
            ) {
                Text(text = "Hint")
            }
        }
        TextField(
            value = hint,
            onValueChange = {
                text = it
                hint = if (it.isNotEmpty()) {
                    text
                } else {
                    it
                }
            },
            colors = TextFieldDefaults.textFieldColors(
                textColor = Color.Gray,
                disabledTextColor = Color.Transparent,
                backgroundColor = Color.Transparent,
                focusedIndicatorColor = Color.Transparent,
                unfocusedIndicatorColor = Color.Transparent,
                disabledIndicatorColor = Color.Transparent
            ),

            modifier = Modifier
                .onFocusChanged {
                    isClicked = it.isFocused
                }
                .background(Color.White)
                .drawBehind {
                    drawLine(
                        color = Color.Red,
                        start = Offset(0f, size.height),
                        end = Offset(size.width, size.height),
                        strokeWidth = 5.toDp().toPx()
                    )

                },
            placeholder = {
                if (!isClicked) {
                    Text(text = "Hint")
                } else {
                    Text(text = "")
                }
            },
        )
    }
}

Result ->
enter image description here

In Activity ->

            ComposeWrappedUpTheme {
                Column {
                    TextfieldCustom()
                    TextfieldCustom()
                }
            }

Edit: -> I discovered that adding fillMaxSize to Column inside setCotent block fixed the issue. I am not sure why.

yras8
  • 459
  • 4
  • 14

1 Answers1

-1

I know the answer. That it because .drawBehind {} draws behind composable borders. So using 1.dp width on composable with height od 30 fill make it look like 31 dp in fact.

yras8
  • 459
  • 4
  • 14