简体   繁体   English

在 jetpack compose 中使用 .shadow 和 Button 会导致问题

[英]Using .shadow with Button in jetpack compose causes an issue

I want to use the .shadow operator to add a shadow to my button, but as you can see in the image, I got a white background.我想使用.shadow运算符为我的按钮添加阴影,但正如您在图像中看到的那样,我有一个白色背景。 在此处输入图像描述

As many Modifiers in Jetpack Compose order of Modifier.shadow() matters. Jetpack Compose 中的Modifier.shadow()顺序中的许多修饰符很重要。 use shadow first.首先使用阴影

Button(
    modifier = Modifier
        .shadow(2.dp, RoundedCornerShape(2.dp))
        .height(36.dp),
    onClick = { /*TODO*/ }) {
    Text("Button")
}

vs对比

Button(
    modifier = Modifier
        .height(36.dp)
        .shadow(2.dp, RoundedCornerShape(2.dp)),
    onClick = { /*TODO*/ }) {
    Text("Button")
}

在此处输入图像描述

But this is not how you set elevation for button.但这不是您为按钮设置高度的方式。 This is for demonstrating how Modifier.shadow() order changes outcome, maybe helpful with some Composables but with Button you need to use这是为了演示 Modifier.shadow() 顺序如何改变结果,可能对一些 Composables 有帮助,但对于 Button 你需要使用

Button(
    modifier = Modifier.height(36.dp),
    shape = RoundedCornerShape(2.dp),
    elevation = ButtonDefaults.elevation(...),
    onClick = { /*TODO*/ }) {
    Text("Button")
}

and elevation function has properties for different states such as高程函数具有不同状态的属性,例如

  @Composable
    fun elevation(
        defaultElevation: Dp = 2.dp,
        pressedElevation: Dp = 8.dp,
        disabledElevation: Dp = 0.dp,
        hoveredElevation: Dp = 4.dp,
        focusedElevation: Dp = 4.dp,
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM