英文:
Jetpack compose material3 TextField minimum height
问题
所以我有一个带有条件可用的supportingText
的OutlinedTextField
。
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
)
}
)
这是没有错误时的屏幕。
这是有错误时的屏幕。
如您所见,问题在于当出现错误时,错误消息会改变布局!问题是如何为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
and this the screen when there is error
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
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论