

package kr.co.fastcampus.part1.chapter4_15
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.Crossfade
import androidx.compose.animation.animateColor
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.selection.selectable
import androidx.compose.material.MaterialTheme
import androidx.compose.material.RadioButton
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kr.co.fastcampus.part1.chapter4_15.ui.theme.Animation2Theme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Animation2Theme {
Surface(
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
) {
Animation2Ex()
}
}
}
}
}
@Composable
fun Animation2Ex() {
var isDarkMode by remember { mutableStateOf(false) }
val transition = updateTransition(targetState = isDarkMode, label = "isDarkModeTransition")
val backgroundColor by transition.animateColor(label = "backgroundColor") { state ->
when (state) {
false -> Color.White
true -> Color.Black
}
}
val color by transition.animateColor(label = "text color") { state ->
when (state) {
false -> Color.Black
true -> Color.White
}
}
val alpha by transition.animateFloat(label = "alpha") { state ->
when (state) {
false -> 0.7f
true -> 1f
}
}
Column (modifier = Modifier.background(backgroundColor).alpha(alpha)) {
RadioButtonWithText(text = "일반 모드", color = color, selected = !isDarkMode) {
isDarkMode = false
}
RadioButtonWithText(text = "다크 모드", color = color, selected = isDarkMode) {
isDarkMode = true
}
Crossfade(targetState = isDarkMode) { state ->
when (state) {
false -> {
Row {
Box(
modifier = Modifier
.background(Color.Red)
.size(20.dp)
) {
Text("1")
}
Box(
modifier = Modifier
.background(Color.Magenta)
.size(20.dp)
) {
Text("2")
}
Box(
modifier = Modifier
.background(Color.Blue)
.size(20.dp)
) {
Text("3")
}
}
}
true -> {
Column {
Box(
modifier = Modifier
.background(Color.Red)
.size(20.dp)
) {
Text("1")
}
Box(
modifier = Modifier
.background(Color.Magenta)
.size(20.dp)
) {
Text("2")
}
Box(
modifier = Modifier
.background(Color.Blue)
.size(20.dp)
) {
Text("3")
}
}
}
}
}
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
Animation2Theme {
Animation2Ex()
}
}
@Preview(showBackground = true)
@Composable
fun RadioButtonWithTextPreview() {
Animation2Theme {
RadioButtonWithText(
text = "라디오 버튼",
color = Color.Red,
selected = true,
onClick = {}
)
}
}
@Composable
fun RadioButtonWithText(
text: String,
color: Color = Color.Black,
selected: Boolean,
onClick: () -> Unit
) {
Row(
modifier = Modifier.selectable(
selected = selected,
onClick = onClick
),
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(selected = selected, onClick = onClick)
Text(text = text, color = color)
}
}