
package kr.co.fastcampus.part1.chapter4_12
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import kotlinx.coroutines.launch
import kr.co.fastcampus.part1.chapter4_12.ui.theme.BottomAppBarTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
BottomAppBarTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
BottomAppBarEx()
}
}
}
}
}
@Composable
fun BottomAppBarEx() {
val scaffoldState = rememberScaffoldState()
val coroutineScope = rememberCoroutineScope()
var counter by remember {
mutableStateOf(0)
}
Scaffold(
scaffoldState = scaffoldState,
bottomBar = {
BottomAppBar() {
Text(text = "BottomAppBar")
Button(onClick = {
coroutineScope.launch {
scaffoldState.snackbarHostState.showSnackbar("스낵바입니다.")
}
}
) {
Text("버튼")
}
Button(onClick = {
counter++
}
) {
Text("+1")
}
Button(onClick = {
counter--
}
) {
Text("-1")
}
}
}
) {
Box (modifier = Modifier.fillMaxSize()){
Text(text = "카운터는 ${counter}회 입니다.", modifier = Modifier.align(Alignment.Center))
}
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
BottomAppBarTheme {
BottomAppBarEx()
}
}