Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java
package ir.stacklearncode.socialmedia.ui.components

import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Icon
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavController
import ir.stacklearncode.socialmedia.ui.utils.NavigationItem
import androidx.compose.material3.Icon
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
import androidx.compose.material3.BottomAppBar



@Composable
fun BottomNavigationView(navController: NavController) {
    var selectedRoute by remember {
        mutableSetOf("home")
    }
    val items = listOf(
        NavigationItem.Home,
        NavigationItem.Search,
        NavigationItem.Add,
        NavigationItem.Activities,
        NavigationItem.Profile
    )
    BottomAppBar {
        items.forEach {
            BottomNavigationItem(
                icon = (
                        Icon(painter = painterResource(id = it.icon), contentDescription = it.title)
                        ),
                selected = selectedRoute == it.route,
                onClick = {
                    navController.navigate(it.route) {
                        navController.graph.startDestinationRoute?.let { route ->
                            selectedRoute = route
                            popUpTo(route) {
                                saveState = true
                            }
                        }
                        launchSingleTop = true
                        restoreState = true
                    }
                }
            )
        }
    }


What I have tried:

I try to use var instead of val but word root still has error.
Posted
Updated 29-Aug-23 0:37am
v2
Comments
Richard MacCutchan 24-Aug-23 9:26am    
What error, and which line does it occur on?
ali rg 2023 25-Aug-23 5:05am    
my error is in {var selectedRoute by remember {
mutableSetOf("home")} it says change var to val and then i have error in selectedRoute = route it is not about var items

1 solution

Try:
var items = List.of(
   ...
 
Share this answer
 
Comments
ali rg 2023 24-Aug-23 14:40pm    
hi but how can i use list.of!
OriginalGriff 24-Aug-23 15:57pm    
Start with the documentation - but if you don't know how to use lists then you probably want to learn some more before you go any further with this project. If you don't, you are probably going to be both confused and frustrated very quickly!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900