Hello people! I have a wrapper around BasicTextFie...
# compose
i
Hello people! I have a wrapper around BasicTextField that “autofocus” with a LaunchedEffect. The value of the “input” is held by a VM which could initialize it with “something” received by savedState. (Imagine I open a Search Screen with a already-written text). The problem is the cursor does not position itself at the end of the string.. I’ve tried looking if this could be caused be the launchedEffect being triggered before the initial text is set.. but it doesn’t seem to be the problem.
Copy code
@HiltViewModel
class SearchVM @Inject constructor(
    savedStateHandle: SavedStateHandle
) : ViewModel() {

    var query by mutableStateOf(savedStateHandle["query"] ?: "")
        private set
}
the wrapper of BaseTextField is very simple it only has:
Copy code
if (autofocus) {
    LaunchedEffect(Unit) {
        focusRequester.requestFocus()
    }
}
👀 1
z
There are multiple overloads for the (Basic)TextField, one accepts
TextFieldValue
which you can use to specify where the cursor should be initially. Ill attach some code below for how Ive been handling this myself, the important bit being
selection
which is always the length of the input text.
Copy code
var value by remember {
    val textOrEmpty = text.orEmpty()
    
    val fieldValue = TextFieldValue(
        text = textOrEmpty,
        selection = TextRange(textOrEmpty.length),
    )

    mutableStateOf(fieldValue)
}