【问题标题】:Can I put animation fade in with duration between button state enable and disable in jetpack compose?我可以在 Jetpack Compose 中使用按钮状态启用和禁用之间的持续时间淡入动画吗?
【发布时间】:2022-11-22 07:06:57
【问题描述】:

这就是我在撰写中创建按钮的方式

 Button(
        modifier = modifier,
        enabled = enabled.value,
        onClick = {}
    ) {
        Text(text = text)
    }

【问题讨论】:

    标签: android android-jetpack-compose android-jetpack android-compose-button


    【解决方案1】:

    Button 的背景色基于ButtonDefaults.buttonColors() 中定义的颜色。

    解决方法是为 disabledBackgroundColorbackgroundColor 定义相同的颜色。
    就像是:

    var isButtonEnabled by remember { mutableStateOf(true) }
    val animateStateButtonColor = animateColorAsState(
        targetValue = if (isButtonEnabled) Color.Blue else Teal200,
        animationSpec = tween(2000, 0, LinearEasing)
    ) 
    
    Button(
        colors = ButtonDefaults.buttonColors(
            backgroundColor = animateStateButtonColor.value,
            disabledBackgroundColor = animateStateButtonColor.value,
        ),
        enabled = isButtonEnabled,
        onClick = { /* ... */ }
    ) {
        Text(text = "Button")
    }
    
       
    

    【讨论】:

    • 有没有办法在声明 var isButtonEnabled 时提供动画?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    相关资源
    最近更新 更多