Android Compose 공부 -1 기본

Gooak·2024년 2월 1일

플러터로 회사에 입사하여 지금까지 개발을 하고있지만
안드로이드 네이티브를 해보고싶은 마음은 꼭 있었다.
하지만 선언형 프로그래밍이 익숙한 나에겐 명령형 프로그래밍을 하느란 쉽지 않았다
그러던 와중 jetpact Compose가 선언형 프로그래밍이라고 하여
틈틈히 공부해 나가기로 하였다!


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            GooakProjectTheme {
                val context = LocalContext.current //context 선언

                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    Greeting(context, Message("name", "compose"))
                }
            }
        }
    }
}

data class Message(val author: String, val body: String) //데이터 클래스 정의

val imageModifier = Modifier  //Modifier를 이렇게 미리 선언해두고 두고 두고 쓸수 있을듯 하다.
    .size(40.dp)
    .border(1.dp, Color.Black, CircleShape)
    .clip(CircleShape)
    .background(Color.Yellow)

@Composable
fun Greeting(context : Context, mag : Message) {
    Box(
        modifier = Modifier
            .padding(15.dp)

    )
    {
        Button(
            onClick = {
                toastMessage(context, "메세지 테스트")
            },
            colors = ButtonDefaults.buttonColors(Color.LightGray)
        ) {
            Row(
                verticalAlignment = Alignment.Top,
//                modifier = Modifier
//                    .clickable(
//                        //터치 이벤트
//                        interactionSource = remember {
//                            MutableInteractionSource() //터치이벤트 제거
//                        },
//                        indication = null,
//                        onClick = {
//                            toastMessage(context, "메세지 테스트")
//                        },
//                    )

            ) {
                AsyncImage(
                    modifier = imageModifier,
                    model = "",
                    contentDescription = "Translated description of what the image contains"
                    //placeholder = painterResource(id = R.drawable.sudoimage),
                    //error = painterResource(id = R.drawable.sudoimage),
                )
                Spacer(modifier = Modifier.width(8.dp))
                Column {
                    Text(
                        text = mag.author
                    )
                    Text(
                        text = mag.body
                    )
                }
            }
        }
    }
}
fun toastMessage(context : Context, text : String){
    Toast.makeText(context ,text, Toast.LENGTH_SHORT).show()
}

//@Preview(showBackground = true)//미리보기 같은 개념 매개변수가 없어야함
//@Composable
//fun GreetingPreview() {
//    GooakProjectTheme {
//        Greeting(Message("name", "compose"))
//    }
//}

구글계열이라 그런지 플러터와 조금 흡사한 부분이 보였다
더 공부를 해서 토이 프로젝트를 한번 개발해봐야겠다..

profile
구악

0개의 댓글