0
Material 3

I have the following code snippet of a simple password entry. However, its not building because of the following errors

@Composable invocations can only happen from the context of a @Composable function

However, if I remove the visualTransformations is works ok. I am just wondering why that is?

In the following image you can see there is a lot of red lines for errors on the Text and Image.

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PasswordEntry(
    modifier: Modifier = Modifier,
    passwordValue: String,
    placeholderText: String,
    visibilityTapped: () -> Boolean,
    onPasswordChange: (String) -> Unit,
) {
    OutlinedTextField(
        modifier = modifier.fillMaxWidth().background(
            shape = RoundedCornerShape(10.dp),
            color = MaterialTheme.colorScheme.backgroundInputEntry
        ),
        singleLine = true,
        value = passwordValue,
        onValueChange = { newInput: String ->
            onPasswordChange(newInput)
        },
        placeholder = {
            Text(text = placeholderText, color = MaterialTheme.colorScheme.placeholderEntry)
        },
        trailingIcon = {
            val visibilityIconId = if(visibilityTapped()) {
                R.drawable.hidden
            } else {
                R.drawable.visible
            }
            Image(
                painter = painterResource(id = visibilityIconId),
                contentDescription = "eye open close"
            )
        },
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
        shape = RoundedCornerShape(10.dp),
        visualTransformations = if (visibilityTapped()) {
            PasswordVisualTransformation()
        } else {
            VisualTransformation.None
        }
    )
}

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
ant2009
  • 27,094
  • 154
  • 411
  • 609

1 Answers1

3

You have to use visualTransformation instead of visualTransformations.

OutlinedTextField(
    //...
    visualTransformation = if (visibilityTapped()) {
        PasswordVisualTransformation()
     } else {
        VisualTransformation.None
     }
)
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Thanks, not sure how I missed that. But I don't understand why the IDE did should the error as `visualTransformations` as the parameter name as being incorrect. – ant2009 Feb 23 '23 at 15:28