https://kotlinlang.org logo
#compose
Title
# compose
c

Colton Idle

09/18/2020, 5:44 AM
What am I doing wrong here? Just want a simple text field that reflects in a text below it as well
Copy code
@Composable
fun MyInput() {
    Column(Modifier.padding(16.dp)) {
        val textState by remember { mutableStateOf(TextFieldValue()) }
        TextField(
            value = textState.text,
            onValueChange = { textState = it) }
        )
        Text("The textfield says: " + textState.text)
    }
}
note: I'm trying to learn via little samples from https://foso.github.io/Jetpack-Compose-Playground/cookbook/textfield_changes/ but let me know if there is another resource or some more official docs with these sorts of samples. I understand theres a bunch of sample apps, but I'm just trying to learn component by component first, and not jumping into an entire application.
m

Marcin Środa

09/18/2020, 6:00 AM
Probably should looks like:
Copy code
@Composable
fun MyInput() {
    Column(Modifier.padding(16.dp)) {
        val textState = remember { mutableStateOf(TextFieldValue()) }
        TextField(
            value = textState.value,
            onValueChange = { textState.value = it }
        )
        Text("The textfield says: " + textState.value.text)
    }
}
t

Timo Drick

09/18/2020, 11:24 AM
Maybe something like this:
Copy code
Column(Modifier.padding(16.dp)) {
    var textState by remember { mutableStateOf(TextFieldValue()) }
    BaseTextField(
        value = textState,
        onValueChange = { textState = it }
    )
    Text("The textfield says: " + textState.text)
}
f

Foso

09/18/2020, 9:13 PM
Hi, i'm the author of the linked page. Maybe you are also missing the imports: import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue At least i had the problem that Android Studio wasn't suggesting these imports. I think i will change the example from BaseTextField to TextField
c

Colton Idle

09/24/2020, 3:11 AM
Just following up on this. @Marcin Środa had the right solution. @Foso I didn't need those imports as Marcins solution worked without it?