Anees Ahmad
Anees Ahmad

Reputation: 83

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

Upvotes: 3

Views: 4369

Answers (1)

Raj
Raj

Reputation: 1595

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

    }
}

Upvotes: 8

Related Questions