Jetpack Compose Material3 TextField 最小高度

huangapple go评论162阅读模式
英文:

Jetpack compose material3 TextField minimum height

问题

所以我有一个带有条件可用的supportingTextOutlinedTextField

    var usernameValue by remember { mutableStateOf(TextFieldValue("")) }
    androidx.compose.material3.OutlinedTextField(
        modifier = Modifier
            .fillMaxWidth()
            .minimumInteractiveComponentSize(),
        value = usernameValue,
        onValueChange = {
            usernameValue = it
        },
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
        colors = androidx.compose.material3.TextFieldDefaults.textFieldColors(
            focusedIndicatorColor = Color.White,
            disabledIndicatorColor = Color.White,
            unfocusedIndicatorColor = Color.White,
            containerColor = Color(0xFFE5EAF0),
            placeholderColor = Color.Transparent
        ),
        shape = RoundedCornerShape(size = 12.dp),
        singleLine = true,
        isError = shouldShowUsernameError,
        supportingText = {
            if (shouldShowUsernameError)
                Text(
                    userNameErrorMessage,
                    color = MaterialTheme.colorScheme.error
                )
        }
    )

这是没有错误时的屏幕。
Jetpack Compose Material3 TextField 最小高度

这是有错误时的屏幕。
Jetpack Compose Material3 TextField 最小高度

如您所见,问题在于当出现错误时,错误消息会改变布局!问题是如何为OutlinedTextField设置最小高度。

英文:

so I have a OutlinedTextField with supportingText which is available with conditions.

var usernameValue by remember { mutableStateOf(TextFieldValue("")) }
androidx.compose.material3.OutlinedTextField(
    modifier = Modifier
        .fillMaxWidth()
        .minimumInteractiveComponentSize(),
    value = usernameValue,
    onValueChange = {
        usernameValue = it
    },
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
    colors = androidx.compose.material3.TextFieldDefaults.textFieldColors(
        focusedIndicatorColor = Color.White,
        disabledIndicatorColor = Color.White,
        unfocusedIndicatorColor = Color.White,
        containerColor = Color(0xFFE5EAF0),
        placeholderColor = Color.Transparent
    ),
    shape = RoundedCornerShape(size = 12.dp),
    singleLine = true,
    isError = shouldShowUsernameError,
    supportingText = {
        if (shouldShowUsernameError)
            Text(
                userNameErrorMessage,
                color = MaterialTheme.colorScheme.error
            )
    }
)

this is the screen when there is no error
Jetpack Compose Material3 TextField 最小高度

and this the screen when there is errorJetpack Compose Material3 TextField 最小高度

as you can see problem is when there is error, error messages change the layout!
and the question is how can I set minimum height for OutlinedTextField

答案1

得分: 1

布局会发生变化,因为当 shouldShowUsernameError=true 时,会出现 supportingText

要避免这个问题,您可以使用类似以下的方法:

supportingText = {
    Text(
        text = if (shouldShowUsernameError) "userNameErrorMessage" else "",
        color = MaterialTheme.colorScheme.error
    )
}

Jetpack Compose Material3 TextField 最小高度

英文:

The layout changes because when shouldShowUsernameError=true there is the supportingText.

To avoid it you can use something like:

supportingText = {
    Text(
        text = if (shouldShowUsernameError) "userNameErrorMessage" else "",
        color = MaterialTheme.colorScheme.error
    )
}

Jetpack Compose Material3 TextField 最小高度

huangapple
  • 本文由 发表于 2023年3月7日 21:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定