Search code examples
android-jetpack-composeandroid-jetpack

JetPack Compose, animateColorAsSate


Getting error when use this line:

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

It showing the following warning:

Property delegate must have a 'getValue(Nothing?, KProperty*>)' method. None of the following functions are suitable. State.getValue(Any?, KProperty<>)   where T = Color for   inline operator fun State.getValue(thisObj: Any?, property: KProperty<>): T defined in androidx.compose.runtime


Solution

  • Make sure you are using correct import statements.

    Compare and try replacing your import statements with correct one from below.

    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 }
        ) {
    
        }
    }