Nacho Ruiz Martin
03/20/2023, 8:27 AMStateFlow<String>. This flow is collected inside a Composable with .collectAsState(Dispatchers.Main.immediate) .
The string inside is used as the value of a BasicTextField and the ViewModel has a function to update this value in the state.
All goes as expected if the user is the one typing on the keyboard 👍 but, if a button is pressed on the interface, the value of the text field should be cleared a new value should be added as some kind of prefix (editable by the user, just as a helper). When I add this value in the ViewModel, the cursor of the text field stays at the beginning instead of moving to the end of the text.
I have read this article: https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5. But I thought with Dispatchers.Main.immediate it should be all good?
Thanks for the help.Albert Chang
03/20/2023, 9:32 AMTextFieldValue instead of String and use TextFieldValue(text = newString, selection = TextRange(newString.length)) when you replace the text.Nacho Ruiz Martin
03/20/2023, 10:36 AMTextFieldValue there. I can store the string and the range separately, though, but I would like to delegate it to the OS.
Is it expected that the cursor is kept at the beginning in this situation?Albert Chang
03/20/2023, 10:36 AMNacho Ruiz Martin
03/20/2023, 10:37 AMAlbert Chang
03/20/2023, 10:45 AMNacho Ruiz Martin
03/20/2023, 10:47 AMAlbert Chang
03/20/2023, 10:49 AMString overload), the cursor will be kept at its original position, unless the new text is shorter than cursor position, in which case the cursor will be moved to the end of the text.Nacho Ruiz Martin
03/20/2023, 10:51 AMNacho Ruiz Martin
03/20/2023, 10:51 AMAlbert Chang
03/20/2023, 10:58 AMTextFieldValue as state internally, and keyboard input will trigger updates of both text and selection (and potentially composition). The overload that takes String is just a wrapper that synchronizes the external String and the text of internal TextFieldValue.Nacho Ruiz Martin
03/20/2023, 10:59 AMZach Klippenstein (he/him) [MOD]
03/21/2023, 3:30 PMTextFieldValue does support multiplatform, although unfortunately not kotlin/native yet.