Skip to content

Instantly share code, notes, and snippets.

@veroanggra
Created April 18, 2023 16:33
Show Gist options
  • Save veroanggra/c2047d74de95350d74891fa9a8060179 to your computer and use it in GitHub Desktop.
Save veroanggra/c2047d74de95350d74891fa9a8060179 to your computer and use it in GitHub Desktop.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
PlayingWithComposeTheme {
FavoriteAppScreen()
}
}
}
@Composable
fun FavoriteAppScreen() {
var favorite by remember { mutableStateOf(false) }
var color by remember { mutableStateOf(Color.Gray) }
ConstraintLayout(Modifier.fillMaxSize()) {
color = if (favorite) {
Color.Red
} else {
Color.Gray
}
val (imageFavorite) = createRefs()
FavoriteButton(modifier = Modifier.constrainAs(imageFavorite) {
start.linkTo(parent.start)
top.linkTo(parent.top)
end.linkTo(parent.end)
bottom.linkTo(parent.bottom)
}, color = color, onFavoriteValueChanged = {
favorite = true
if (color == Color.Red) {
favorite = false
}
}, favorite = favorite)
}
}
@Composable
fun FavoriteButton(
modifier: Modifier = Modifier,
favorite: Boolean,
onFavoriteValueChanged: (Boolean) -> Unit,
color: Color
) {
Image(Icons.Default.Favorite,
contentDescription = null,
colorFilter = ColorFilter.tint(color),
modifier = modifier
.size(200.dp)
.clickable { onFavoriteValueChanged(favorite) }
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment