如何在 Jetpack Compose 中获取带有渐变边框的 OutlinedTextField?

发布于 2025-01-13 20:55:47 字数 1107 浏览 3 评论 0原文

我正在尝试获取一个带有渐变边框的 OutlinedTextField ,如下所示:

OutlinedTextField with Gradient Border


但我只能向轮廓添加单一颜色,

OutlinedTextField(
    value = email,
    onValueChange = {email = it},
    label = { Text(text = "Email Address") },
    modifier = Modifier
        .fillMaxWidth(.8f)
        .padding(4.dp),
    colors = TextFieldDefaults.outlinedTextFieldColors(unfocusedBorderColor = Color.Green)
)

无法添加渐变


如果我使用 modifier 添加边框,则渐变将应用于边框,而不是轮廓:

Modifier.border(width = 1.dp, brush = gradient, shape = RoundedCornerShape(12.dp))

gradient notworking


如何向轮廓添加渐变颜色?

I'm trying to get an OutlinedTextField with gradient border like this one:

OutlinedTextField with Gradient Border


But I can add only a single color to the outline,

OutlinedTextField(
    value = email,
    onValueChange = {email = it},
    label = { Text(text = "Email Address") },
    modifier = Modifier
        .fillMaxWidth(.8f)
        .padding(4.dp),
    colors = TextFieldDefaults.outlinedTextFieldColors(unfocusedBorderColor = Color.Green)
)

Can't add gradient


If I add a border with modifier then the gradient is applied to the border, not to the outline:

Modifier.border(width = 1.dp, brush = gradient, shape = RoundedCornerShape(12.dp))

gradient not working


How can I add gradient color to the outline?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏已如深秋| 2025-01-20 20:55:47

到目前为止,我知道 OutlinedTextField 不支持渐变边框。但如果真的想在文本字段中使用渐变边框,您可以尝试 BasicTextField

    var name by remember {
        mutableStateOf("")
    }
    BasicTextField(
        value = name,
        onValueChange = { name = it },
        cursorBrush = Brush.horizontalGradient(listOf(Color.Red, Color.Green)),
        modifier = Modifier
            .fillMaxWidth(),
        decorationBox = { innerTextField ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .border(
                        brush = Brush.horizontalGradient(listOf(Color.Green, Color.Blue)),
                        width = 1.dp,
                        shape = CircleShape
                    )
                    .padding(16.dp)
            ) {
                Icon(Icons.Default.Email, contentDescription = "")
                Spacer(modifier = Modifier.padding(3.dp))
                innerTextField()
            }
        }
    )

这就是最终结果

Jetpack compose BasicTextField边框

you can learn more about basic text field by looking the sample

[BasicTextField Samples][2]

so far that i know OutlinedTextField does't support gradient border. But if really want to use gradient border in text field you can try BasicTextField

    var name by remember {
        mutableStateOf("")
    }
    BasicTextField(
        value = name,
        onValueChange = { name = it },
        cursorBrush = Brush.horizontalGradient(listOf(Color.Red, Color.Green)),
        modifier = Modifier
            .fillMaxWidth(),
        decorationBox = { innerTextField ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .border(
                        brush = Brush.horizontalGradient(listOf(Color.Green, Color.Blue)),
                        width = 1.dp,
                        shape = CircleShape
                    )
                    .padding(16.dp)
            ) {
                Icon(Icons.Default.Email, contentDescription = "")
                Spacer(modifier = Modifier.padding(3.dp))
                innerTextField()
            }
        }
    )

and this is the final result

Jetpack compose BasicTextField Border

you can learn more about basic text field by looking the sample

[BasicTextField Samples][2]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文