I'm trying to create a simple AlertDialog with a single OutlinedTextField which takes some value and performs an action when confirm button is clicked. No matter what I try, it seems the text field is not editable and I can't figure out why.
@Composable
fun InputDialog(
dialogTitle: String,
description: String,
value: MutableState<String>,
show: MutableState<Boolean>,
onConfirm: (String) -> Unit) {
val textFieldValue = TextFieldValue(value.value)
AlertDialog(
//modifier = Modifier.background(Color.Yellow),
onDismissRequest = {
show.value = false
},
confirmButton = {
TextButton(
onClick = {
onConfirm(textFieldValue.text)
}
) {
Text("Save")
}
},
dismissButton = {
TextButton(
onClick = {
show.value = false
}
) {
Text("Cancel")
}
},
text = {
Column {
Text(description)
OutlinedTextField(
enabled = true,
readOnly = false,
value = textFieldValue,
onValueChange = {
value.value = it.text
},
label = {
Text(dialogTitle.toUpperCase(Locale.current))
},
placeholder = {
Text(dialogTitle)
})
}
})
}
The issue is that the value for your textfield is not defined as state.
You have this:
and you should convert it to this:
For more about state in Jetpack Compose, you can read here.