
화면의 dropdownmenu 의 Setting을 누르면 drawer navigation 메뉴중 setting 으로 이동하는걸
원합니다.
- 파일 MainNavigation.kt -
enum class MainRoute(value: String) {
Home("home"),
Navigation("navigation"),
Settings("settings")
}
private data class DrawerMenu(val icon: ImageVector, val title: String, val route: String)
private val menus = arrayOf(
DrawerMenu(Icons.Filled.Home, "Home", MainRoute.Home.name),
DrawerMenu(Icons.Filled.Settings, "Settings", MainRoute.Settings.name),
)
@Composable
private fun DrawerContent(
menus: Array<DrawerMenu>,
onMenuClick: (String) -> Unit
) {
Column(
modifier = Modifier.fillMaxSize()
) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(200.dp)
) {
}
menus.forEach {
NavigationDrawerItem(
label = { Text(text = it.title)},
icon = { Icon(imageVector = it.icon, contentDescription = null)},
selected = false,
onClick = {
onMenuClick(it.route)
}
)
}
}
}
@Composable
fun MainNavigation(
navController: NavHostController = rememberNavController(),
coroutineScope: CoroutineScope = rememberCoroutineScope(),
drawerState: DrawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
) {
ModalNavigationDrawer(
drawerState = drawerState,
drawerContent = {
ModalDrawerSheet {
DrawerContent(menus) { route ->
coroutineScope.launch {
drawerState.close()
}
navController.navigate(route)
}
}
}
) {
NavHost(navController = navController, startDestination = MainRoute.Home.name) {
composable(MainRoute.Home.name) {
HomeScreen(drawerState)
}
composable(MainRoute.Settings.name) {
SettingsScreen(drawerState)
}
}
}
}
- 파일 AppBar.kt -
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainAppBar(drawerState: DrawerState?, title: String) {
val coroutineScope = rememberCoroutineScope()
var expanded by remember { mutableStateOf(false)}
CenterAlignedTopAppBar(
navigationIcon = {
if (drawerState != null) {
IconButton(onClick = {
coroutineScope.launch {
drawerState.open()
}
}) {
Icon(Icons.Filled.Menu, contentDescription = "")
}
}
},
title = { Text(text = title) },
actions = {
IconButton(
onClick = { expanded = true }) {
Icon(Icons.Filled.MoreVert, null)
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
DropdownMenuItem(
text = { Text("Setting") },
onClick = { 여기 }
)
}
}
)
}
onClick에서 뭘해줘야하나요? navcontroller를 넣으면 오류가나서리