【问题标题】:JetPack Compose, animateColorAsSateJetPack Compose、animateColorAsSate
【发布时间】:2021-12-16 07:24:46
【问题描述】:

使用此行时出错:

val surfaceColor: animateColorAsState( if (isExpanded) MaterialTheme.colors.primary 否则 MaterialTheme.colors.surface, )

它显示以下警告:

属性委托必须有一个 'getValue(Nothing?, KProperty*>)' 方法。以下功能均不适用。 State.getValue(Any?, KProperty>)   其中 T = Color for inline operator fun State.getValue(thisObj: Any?, property: KProperty>): T 在 androidx.compose.runtime 中定义

【问题讨论】:

标签: android-jetpack-compose android-jetpack


【解决方案1】:

确保您使用正确的导入语句。

比较并尝试将您的导入语句替换为下面的正确语句。

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color


@Composable
fun YourFunction() {

    var isExpanded by remember { mutableStateOf(false) }

    val surfaceColor: Color by animateColorAsState(
        if (isExpanded) MaterialTheme.colors.primary else MaterialTheme.colors.surface
    )

    Column(
        modifier = Modifier.clickable { isExpanded = !isExpanded }
    ) {

    }
}

【讨论】:

  • 感谢 Rajasekhar,我刚刚将 import androidx.compose.ui.graphics.Color 替换为 import androidx.compose.graphics.Color 并且可以正常工作。
猜你喜欢
  • 2023-01-22
  • 2021-12-26
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 2021-02-21
  • 2020-12-29
相关资源
最近更新 更多