Colton Idle
07/01/2021, 3:28 AMOutlinedTextFieldWithFocusLostError
I have this
var focusLostCount by remember { mutableStateOf(0) }
val shouldDisplayError by remember { derivedStateOf { (focusLostCount > 1) } }
OutlinedTextField( modifier = Modifier.fillMaxWidth(1f)
.onFocusChanged { focusState -> if (!focusState.isFocused) { focusLostCount++ } },
So basically I just want a quick solution on a user clicking on a text field, and then clicking away onto another text field, hence triggering shouldDisplayError which itself runs some validation to check if the input is empty. Having a counter seems a little fishy though, hence the question.David Edwards
07/01/2021, 6:55 AMDavid Edwards
07/01/2021, 7:23 AMColton Idle
07/01/2021, 1:45 PMZach Klippenstein (he/him) [MOD]
07/07/2021, 4:51 PMval isValid: Boolean by remember { mutableStateOf(true) }
OutlinedTextField(….onFocusChanged { if(!isFocused) isValid = isTextValid(text) })
Better yet, move that isValid
state to your view model, give it a validate()
method, and just call that validate method when focus is lost.
class ViewModel {
var text: String by mutableStateOf("") // or however you store the text
var isValid: Boolean by mutableStateOf(true)
private set
fun validate() {
isValid = TODO("validation logic")
}
}
…
OutlinedTextField(….onFocusChanged { if(!isFocused) viewModel.validate() })
Colton Idle
07/07/2021, 4:54 PMZach Klippenstein (he/him) [MOD]
07/07/2021, 4:55 PMZach Klippenstein (he/him) [MOD]
07/07/2021, 4:57 PMemitInitialState
flag could be useful for that modifier.Colton Idle
07/07/2021, 4:59 PMColton Idle
07/07/2021, 5:02 PMColton Idle
07/08/2021, 2:17 PMRalston Da Silva
08/27/2021, 11:22 PMColton Idle
08/29/2021, 7:58 AMColton Idle
08/30/2021, 2:58 AMRalston Da Silva
08/30/2021, 4:51 AM