Created
March 4, 2023 12:13
-
-
Save Simba-97/6e7f57b5ffb39a74367d887b1c7358cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalPagerApi::class) | |
@Composable | |
fun MainScreen() { | |
val screens = listOf( | |
BottomBarScreen.Home, | |
BottomBarScreen.Profile, | |
BottomBarScreen.Settings | |
) | |
val pagerState = PagerState(pageCount = screens.size) | |
Scaffold( | |
bottomBar = { BottomBar(pagerState = pagerState, screens = screens) } | |
) { innerPaddingValues -> | |
Box( | |
modifier = Modifier.padding(innerPaddingValues) | |
) { | |
BottomNavGraph(pagerState = pagerState) | |
} | |
} | |
} | |
@OptIn(ExperimentalPagerApi::class) | |
@Composable | |
fun BottomBar( | |
pagerState: PagerState, | |
screens: List<BottomBarScreen> | |
) { | |
Row( | |
modifier = Modifier | |
.height(60.dp) | |
.background(Color.Black) | |
.fillMaxWidth(), | |
horizontalArrangement = Arrangement.SpaceEvenly, | |
verticalAlignment = Alignment.CenterVertically | |
) { | |
screens.forEach { screen -> | |
AddItem( | |
screen = screen, | |
pagerState = pagerState | |
) | |
} | |
} | |
} | |
@OptIn(ExperimentalPagerApi::class) | |
@Composable | |
fun AddItem( | |
screen: BottomBarScreen, | |
pagerState: PagerState | |
) { | |
val coroutineScope = rememberCoroutineScope() | |
val selected = pagerState.currentPage == screen.page | |
val background = if (selected) Color.Magenta else Color.Transparent | |
Box( | |
modifier = Modifier | |
.height(40.dp) | |
.clip(CircleShape) | |
.background(background) | |
.clickable( | |
onClick = { | |
coroutineScope.launch { | |
pagerState.animateScrollToPage(screen.page) | |
} | |
} | |
) | |
) { | |
Row( | |
modifier = Modifier | |
.padding(vertical = 8.dp, horizontal = 10.dp), | |
verticalAlignment = Alignment.CenterVertically, | |
horizontalArrangement = Arrangement.spacedBy(4.dp) | |
) { | |
Icon( | |
imageVector = screen.icon, | |
contentDescription = screen.description, | |
tint = Color.White | |
) | |
Text( | |
text = screen.title, | |
color = Color.White | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment