Hi guys! I’m having some weird issue with Gboard w...
# compose
t
Hi guys! I’m having some weird issue with Gboard with auto correct enabled and TextField It happen If I use
TextFieldValue
Copy code
var text by remember { mutableStateOf("") }
TextField(value = TextFieldValue(text), onValueChange = {text = it.text})
f
I think you should use the
TextFieldValue
as state, like so:
Copy code
mutableStateOf(TextFieldValue(""))
👆 3
👍🏽 1
f
Yes, you should mutate the field state, not just keep the text and create new state on every recomposition
👍🏽 1
t
Oh! It’s working now. I’ve always thought that class as a value not a state. Thank you!
🎉 1
One more question. I see that in the
TextField
source code in order to use
TextFieldValue
they created a remembered state like you mentioned above but they have to copy it to another variable to use it. Does anyone know why?
Copy code
var textFieldValueState by remember { mutableStateOf(TextFieldValue(text = value)) }
    val textFieldValue = textFieldValueState.copy(text = value)

    TextField(
        enabled = enabled,
        readOnly = readOnly,
        value = textFieldValue,
        onValueChange = {
            textFieldValueState = it
            if (value != it.text) {
                onValueChange(it.text)
            }
        },
        modifier = modifier,
        singleLine = singleLine,
        textStyle = textStyle,
        label = label,
        placeholder = placeholder,
        leadingIcon = leadingIcon,
        trailingIcon = trailingIcon,
        isError = isError,
        visualTransformation = visualTransformation,
        keyboardOptions = keyboardOptions,
        keyboardActions = keyboardActions,
        maxLines = maxLines,
        interactionSource = interactionSource,
        shape = shape,
        colors = colors
)
f
You could react to
onValueChange
by e.g. capitalizing the value. I think that the updated value wouldn't be reflected in the
TextFieldValue
without the copying.
t
I thought that when the
value
is updated, the remembered
TextFieldValue
will be updated either but apparently it’s not the case.