Untitled

 avatar
unknown
kotlin
a year ago
3.8 kB
13
Indexable
//SCREENS

@Composable

fun ScreenA( onButtonClick: () -> Unit = { } ){
    Row {
        Text(text = "ScreenA", fontSize = 7.5.em)
        Button(onClick = onButtonClick)
        {
            Text(text = "ScreenB")
        }
    }

}


@Composable

fun ScreenB(){

    Text(text = "ScreenB", fontSize = 7.5.em)
}


//DESTINATIONS 

interface RallyDestination {
    val icon: ImageVector
    val route: String
}


object A: RallyDestination {
    override val icon = Icons.Filled.Build
    override val route = "destination_A"
}

object B: RallyDestination {
    override val icon = Icons.Filled.Email
    override val route = "destination_B"
}

val allDestinations = listOf(A, B)


//NAVIGATION EXTENTION FUNCTION
fun NavController.navigateSingleToTop(route: String){
    this.navigate(route){
        launchSingleTop=true;
        popUpTo(this@navigateSingleToTop.graph.startDestinationId){
            saveState= true
        }
        restoreState= true
    }
}

//MAIN ACTIVITY

class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TestTheme {
                val navController = rememberNavController()
                val currentBackStack by navController.currentBackStackEntryAsState()
                val currentDestination = currentBackStack?.destination
                val currentRoute = currentDestination?.route ?: A.route

                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {

                  Scaffold(
                      bottomBar = {
                          NavigationBar {
                              allDestinations.forEach { navDestination ->
                                  NavigationBarItem(
                                      icon = {
                                          Icon(
                                              //Kako se pravi ikonica
                                              //slike ikonica Icons.tipSlike
                                              //a da bih napravila composable od slike ovako
                                              imageVector = navDestination.icon,
                                              contentDescription = null,
                                          )
                                      },
                                      label = { Text(text = navDestination.route) },
                                      //selected = curentRoute == navDestiantion.route
                                      selected = currentRoute.startsWith(navDestination.route),
                                      onClick = {
                                          val newRoute = navDestination.route
                                          navController.navigateSingleToTop(newRoute)
                                      },
                                  )
                              }
                          }
                      }
                  ) {
                      NavHost(navController = navController, startDestination = A.route, modifier = Modifier.padding(it)){
                          composable(A.route){
                              ScreenA(onButtonClick = { navController.navigate(B.route)})
                          }
                          composable(B.route){
                              ScreenB()
                          }
                      }
                  }


                }
            }
        }
    }
}
Editor is loading...
Leave a Comment