Open philipy355 opened 8 months ago
delegate property by를 사용하면 변수로 바꾼다.
var isFavorite by rememberSaveable { mutableStateOf(false) }
callback
@Composable fun ImageCard( isFavorite : Boolean, //callBack onTabFavorite: (Boolean) -> Unit ){ IconButton(onClick = { //callBack onTabFavorite(!isFavorite) }){ }
전체코드
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { //delegate property by를 사용하면 변수로 바꾼다. //알아서 set, get을 해준다. var isFavorite by rememberSaveable { mutableStateOf(false) } ImageCard( isFavorite = isFavorite, ){ favorite -> isFavorite = favorite } } } } @Composable fun ImageCard( isFavorite : Boolean, //callBack onTabFavorite: (Boolean) -> Unit ){ Card( modifier = Modifier .fillMaxWidth(0.5f) .padding(16.dp), shape = RoundedCornerShape(8.dp), elevation = CardDefaults.cardElevation(5.dp) ){ Box( modifier = Modifier.height(200.dp) ){ Image( painter = painterResource(id = R.drawable.poster), contentDescription = "poster", contentScale = ContentScale.Crop ) Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.TopEnd ){ IconButton(onClick = { //callBack onTabFavorite(!isFavorite) }){ Icon( imageVector = if (isFavorite) Icons.Default.Favorite else Icons.Default.FavoriteBorder, contentDescription = "favorite", tint = Color.White ) } } } } }
전체코드(@Composable ImageCard의 modifier를 외부에서 주입받게 수정)
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { //delegate property by를 사용하면 변수로 바꾼다. //알아서 set, get을 해준다. var isFavorite by rememberSaveable { mutableStateOf(false) } ImageCard( modifier = Modifier.fillMaxWidth(0.5f) .padding(16.dp), isFavorite = isFavorite, ){ favorite -> isFavorite = favorite } } } } @Composable fun ImageCard( modifier: Modifier = Modifier, isFavorite : Boolean, //callBack onTabFavorite: (Boolean) -> Unit ){ Card( modifier = modifier, shape = RoundedCornerShape(8.dp), elevation = CardDefaults.cardElevation(5.dp) ){ Box( modifier = Modifier.height(200.dp) ){ Image( painter = painterResource(id = R.drawable.poster), contentDescription = "poster", contentScale = ContentScale.Crop ) Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.TopEnd ){ IconButton(onClick = { //callBack onTabFavorite(!isFavorite) }){ Icon( imageVector = if (isFavorite) Icons.Default.Favorite else Icons.Default.FavoriteBorder, contentDescription = "favorite", tint = Color.White ) } } } } }
delegate property by를 사용하면 변수로 바꾼다.
callback
전체코드
전체코드(@Composable ImageCard의 modifier를 외부에서 주입받게 수정)