VisualTransformations能够与可组合的OutlinedTextField一起使用吗?

huangapple go评论57阅读模式
英文:

Can the VisualTransformations work with a composable OutlinedTextField

问题

Material 3

我有以下的代码片段,一个简单的密码输入。然而,它由于以下错误而无法构建:

@Composable 调用只能在 @Composable 函数的上下文中发生

然而,如果我移除 `visualTransformations`,它可以正常工作。我只是想知道为什么会这样?

在下面的图像中,您可以看到 Text 和 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
        }
    )
}

[![在这里输入图像描述][1]][1]
英文:
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
        }
    )
}

VisualTransformations能够与可组合的OutlinedTextField一起使用吗?

答案1

得分: 3

你需要使用 visualTransformation 而不是 visualTransformations

OutlinedTextField(
    //...
    visualTransformation = if (visibilityTapped()) {
        PasswordVisualTransformation()
    } else {
        VisualTransformation.None
    }
)
英文:

You have to use visualTransformation instead of visualTransformations.

OutlinedTextField(
    //...
    visualTransformation = if (visibilityTapped()) {
        PasswordVisualTransformation()
     } else {
        VisualTransformation.None
     }
)

huangapple
  • 本文由 发表于 2023年2月23日 22:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定