Created
April 18, 2023 16:33
-
-
Save veroanggra/c2047d74de95350d74891fa9a8060179 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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