I am trying to use a dropdown menu as my navigation for an app I am building. However I get an error inside the onClick function of the dropdown menu items saying "unsolved reference".
I have tried following this course but that way didn't work for me. It uses regular buttons to navigate, but i want to use a dropdown. I tried Googling the error and tried a few possible solutions but none of them worked for me.
Below is the code of the dropdown menu
@Composable
fun TopAppBarDropdownMenu() {
val expanded = remember { mutableStateOf(false) }
Box(
Modifier
.wrapContentSize(Alignment.TopEnd)
) {
IconButton(
onClick = {
expanded.value = true
}
) {
Icon(
Icons.Filled.Menu,
contentDescription = "Menu"
)
}
}
DropdownMenu(
expanded = expanded.value,
onDismissRequest = { expanded.value = false}
) {
DropdownMenuItem(
text = {
Text(text = stringResource(R.string.pagina_1))
},
onClick = {
expanded.value = false
}
)
Divider()
DropdownMenuItem(
text = {
Text(text = stringResource(R.string.pagina_2))
},
onClick = {
onNavigatePagina2()
expanded.value = false
}
)
}
}
Here is the code of the NavHost
@Composable
fun PresentTest(
navController: NavHostController = rememberNavController()
) {
val backStackEntry by navController.currentBackStackEntryAsState()
val currentScreen = Pagina.valueOf(
backStackEntry?.destination?.route ?: Pagina.Pagina1.name
)
Scaffold(
topBar = {
PresentTestAppBar(
currentScreen = currentScreen
)
}
) { innerPadding ->
NavHost(
navController = navController,
startDestination = Pagina.Pagina1.name,
modifier = Modifier.padding(innerPadding)
) {
composable(route = Pagina.Pagina1.name) {
Pagina1Screen(
onNavigatePagina2 = {
navController.navigate(Pagina.Pagina2.name)
}
)
}
composable(route = Pagina.Pagina2.name) {
Pagina2Screen(
onNavigatePagina1 = {
navController.navigate(Pagina.Pagina1.name)
}
)
}
}
}
}
In your
DropdownMenuItem, theonNavigatePagina2()function is not defined.In your NavHost, you are passing the
onNavigatePagina2()function to thePagina1Screen. So in thePagina1ScreenComposable, you would be able to call the function. But you are not passing it to theTopAppBarDropdownMenuComposable, so you can't call it there.You can pass the function to your
TopAppBarDropdownMenuto make it available there.First, change your
PresentTestComposable as follows:Then update the
PresentTestAppBarComposable as follows:Finally, modify your
TopAppBarDropdownMenuComposable, and then your code should work: