"@Composable invocations can only happen from the context of a @Composable function"
I’m new to jetpack compose and constantly getting this error "@Composable invocations can only happen from the context of a @Composable function" on adding icon and label in the Text Field.
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EditNumberField(
value: String,
@StringRes label: Int,
@DrawableRes icon: Int,
keyboardOptions: KeyboardOptions,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier
){
TextField(
value = value,
label = { Text(stringResource(label)) },
icon = { Icon(painter = painterResource(id = icon), contentDescription = null) },
keyboardOptions = keyboardOptions,
onValueChange = onValueChange,
singleLine = true,
modifier = modifier,
colors = TextFieldDefaults.textFieldColors(
containerColor = Color(0xFFBDFCC9))
)
}
"TextField(), label = {Text()}, icon = {Icon()}" these lines are underlined by red showing the error mentioned above.
2
Answers
There is no parameter named icon for
TextField
composable. Instead you can use leadingIcon or trailingIcon according to your needs and then you can resolve the shown error.Here is the
TextField
composable from documentation for material 3:As an answer has already been given by another one, So I modify your code to this.
Use this composable function.
And here is the function calling.
I hope this helps you.