Last active
April 2, 2025 21:40
-
-
Save waliahimanshu/1242c9da52bbfaa7cf3823436b13e2f6 to your computer and use it in GitHub Desktop.
Primary Tabs (Instead of bottom navigation item)
This file contains hidden or 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
@Composable | |
@OptIn(ExperimentalMaterial3Api::class) | |
fun HomeTabs(navController: NavHostController, modifier: Modifier = Modifier) { | |
val tabs = homeTabItems | |
var selectedTab: Int by rememberSaveable { mutableIntStateOf(0) } | |
Scaffold( | |
modifier = Modifier.fillMaxSize(), | |
floatingActionButton = { | |
// if (selectedTab == HomeTabItems.AccountFeed.ordinal) { | |
// FloatingActionButton(onClick = { | |
// | |
// }) { | |
// Icon(Icons.Default.Add, contentDescription = "Create Goal") | |
// } | |
// } | |
}, | |
bottomBar = { | |
PrimaryTabRow(selectedTabIndex = selectedTab) { | |
tabs.forEachIndexed { index, tab -> | |
Tab( | |
selected = selectedTab == index, | |
onClick = { selectedTab = index }, | |
selectedContentColor = MaterialTheme.colorScheme.primary, | |
unselectedContentColor = MaterialTheme.colorScheme.outline, | |
text = { Text(text = tab.title) }, | |
icon = { | |
Icon( | |
imageVector = if (selectedTab == index) | |
tab.selectedIcon else tab.unSelectedIcon, | |
contentDescription = "Tab Icon" | |
) | |
} | |
) | |
} | |
} | |
} | |
) { innerPadding -> | |
Box(modifier = Modifier.padding(innerPadding)) { | |
when (selectedTab) { | |
0 -> AccountFeedScreen() | |
}, viewModel = hiltViewModel<FeedTransactionsViewModel>()) | |
1 -> AllFavouriteScreen() | |
} | |
} | |
} | |
} | |
data class HomeTabItem( | |
val title: String, | |
val selectedIcon: ImageVector, | |
val unSelectedIcon: ImageVector | |
) | |
val homeTabItems = listOf( | |
HomeTabItem( | |
title = "HomeFeed", | |
selectedIcon = Icons.Filled.Home, | |
unSelectedIcon = Icons.Outlined.Home | |
), | |
HomeTabItem( | |
title = "Favourite", | |
selectedIcon = Icons.Filled.Favorite, | |
unSelectedIcon = Icons.Outlined.FavoriteBorder | |
), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment