AmrJyniat
01/06/2023, 8:51 AMTextField
with validations, I want to show error(make isError
property equals to true) only after losing the TextField's focus, not on the initial composition, how I can accomplish that?Zach Klippenstein (he/him) [MOD]
01/06/2023, 9:38 PMonFocusChanged
modifierAmrJyniat
01/09/2023, 9:14 AMonFocusChanged
gives me the current state, so If I depend on hasFocus = false
it'll show the error each time the field has no focus which isn't intendedZach Klippenstein (he/him) [MOD]
01/09/2023, 11:13 PMeach time the field has no focusI don’t understand the difference between those scenarios.
onFocusChanged
gives you a callback when the focus state changes. As far as I can tell what you’re looking for is a signal that the focus state has changed from focused to not focused. Or do you want to get a signal when focus has moved to another text field, but not to potentially something else?AmrJyniat
01/10/2023, 7:06 AMAs far as I can tell what you're looking for is a signal that the focus state has changed from focused to not focused.
Exactly.
The difference here is that onFocusChanged
will produce hasFocus = false
on the initial composition, therefore, if I depend on this to show the error will show the error on all existing Inputs on the initial composition.Zach Klippenstein (he/him) [MOD]
01/10/2023, 5:46 PMAmrJyniat
01/11/2023, 5:10 PMAbdul Hafeez Sajid
01/12/2023, 12:03 PM@Composable
fun MyCustomTextField() {
var text by remember {
mutableStateOf("")
}
var shouldShowError by remember {
mutableStateOf(false)
}
var shouldShowErrorFirstTime by remember {
mutableStateOf(false)
}
TextField(value = text, onValueChange = { text = it }, modifier = Modifier.onFocusChanged {
if (!it.isFocused && shouldShowErrorFirstTime) { // on first onFocusChanged, shouldShowErrorFirstTime is still false so it won't go in if.
// here validate
if(text.length < 5){
shouldShowError = true
shouldShowErrorFirstTime = false
}else{
shouldShowError = false
shouldShowErrorFirstTime = true
}
}else{
shouldShowErrorFirstTime = true // this will happen on first onFocusChanged callback so error will be hidden.
}
}, label = {
Text(text = "Enter Text")
})
Spacer(Modifier.size(16.dp))
ShowError(visible = shouldShowError && !shouldShowErrorFirstTime)
Spacer(Modifier.size(16.dp))
TextField(value = "", onValueChange = {}, label = {
Text(text = "Click to focus out")
})
}
@Composable
fun ShowError(visible: Boolean) {
if(visible){
Text(text = "Error")
}
}
it will skip initial composition and no error will be shown. After first focus In and out, error will be shown if these is.AmrJyniat
01/12/2023, 12:16 PM