@Composable
fun MyTabScreen() {
var tabIndex by remember { mutableIntStateOf(0) }
val tabs = listOf("Home", "Products", "Settings")
Column(modifier = Modifier.fillMaxWidth()) {
TabRow(
selectedTabIndex = tabIndex,
modifier = Modifier,
containerColor = Color.DarkGray,
contentColor = Color.Yellow,
indicator = { tabPositions ->
TabRowDefaults.apply {
Divider(Modifier
.height(2.dp)
.padding(horizontal = 16.dp)
.tabIndicatorOffset(tabPositions[tabIndex]),
color = MaterialTheme.colorScheme.primary
)
}
},
divider ={}
) {
tabs.forEachIndexed { index, title ->
Tab(text = { Text(title) },
selected = tabIndex == index,
onClick = { tabIndex = index },
icon = {
when (index) {
0 -> Icon(imageVector = Icons.Default.Home, contentDescription = null)
1 -> Icon(imageVector = Icons.Default.List, contentDescription = null)
2 -> Icon(imageVector = Icons.Default.Settings,contentDescription = null)
}
}
)
}
}
when (tabIndex) {
0 -> HomeScreen()
1 -> ProductsScreen()
2 -> SettingsScreen()
}
}
}
@Composable
fun HomeScreen() {
Column(
Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Home", fontSize = 25.sp)
}
}
@Composable
fun ProductsScreen() {
Column(
Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Products", fontSize = 25.sp)
}
}
@Composable
fun SettingsScreen() {
Column(
Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Settings", fontSize = 25.sp)
}
}