I noticed a visual glitch with TextFields that I'm...
# compose
j
I noticed a visual glitch with TextFields that I'm trying to solve. Basically, the layout animations with the label mess up the size of the TextField. By default its 56dp, but during the focus & unfocus animation it shortly gets larger (57/58dp) and then returns to it's default height of 56dp again when the animation/transition ends. Depending on the textscaling this effect is even more exaggerated. Can I fix this somehow?
Code to reproduce:
Copy code
kotlin
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MaterialTheme {
                Column(Modifier.safeDrawingPadding()) {
                    var input by rememberSaveable(stateSaver = TextFieldValue.Saver) {
                        mutableStateOf(TextFieldValue(""))
                    }
                    val focusRequester = LocalFocusManager.current

                    Button({
                        focusRequester.clearFocus()
                    }) { Text("Unfocus") }

                    TextField(
                        onValueChange = { input = it },
                        value = input,
                        label = { Text("Label") },
                        placeholder = { Text("Placeholder") },
                        modifier = Modifier.fillMaxWidth(),
                    )
                }
            }
        }
    }
}
Doesn't seem to matter if you have a placeholder or not, just having a label and text does the same visual glitch Also tried different compose versions and also still happens in the latest (2024.12.01)
g
https://issuetracker.google.com/issues/354649086 The only thing you can do until that is fixed is probably using a computed height. Something like:
Copy code
val verticalPadding = 16
val lineHeight = LocalTextStyle.current.lineHeight.value
val height = with(LocalDensity.current) {
    (verticalPadding * 2 + lineHeight * fontScale).dp
}
16 is
TextFieldPadding
, which is an internal constant
j
Ah thanks, tried to find if there was an issue report for something alike but couldn't find any. Will try to see if I can get a computed height such as your example working as a temporary workaround until its properly fixed in the library.
g
that one is actually marked as duplicate of another one that is closed, so I don't know if there will be more fixes