Tarec00


SUBMITTED BY: Guest

DATE: Sept. 14, 2024, 10:47 a.m.

FORMAT: Text only

SIZE: 3.9 kB

HITS: 109

  1. @Composable
  2. fun BottomNavBar(navController: NavController) {
  3. val items = listOf(
  4. Screen.Home,
  5. Screen.Settings,
  6. Screen.Logs
  7. )
  8. val haptic = LocalHapticFeedback.current
  9. Surface {
  10. NavigationBar(
  11. containerColor = MaterialTheme.colorScheme.tertiaryContainer.copy(0.1f),
  12. modifier = Modifier.consumeWindowInsets(WindowInsets(0, 0, 0, 0))
  13. ) {
  14. val currentRoute = navController.currentBackStackEntryAsState().value?.destination?.route
  15. items.forEach { screen ->
  16. NavigationBarItem(
  17. selected = currentRoute == screen.route,
  18. onClick = {
  19. if (currentRoute != screen.route) {
  20. // Trigger haptic feedback
  21. haptic.performHapticFeedback(HapticFeedbackType.LongPress)
  22. // Navigate to the selected route
  23. navController.navigate(screen.route) {
  24. popUpTo(navController.graph.startDestinationId) {
  25. saveState = true
  26. }
  27. restoreState = true
  28. launchSingleTop = true
  29. }
  30. }
  31. },
  32. label = {
  33. Text(
  34. screen.route.replaceFirstChar {
  35. if (it.isLowerCase()) it.titlecase(
  36. Locale.ROOT
  37. ) else it.toString()
  38. },
  39. fontSize = 10.sp, // Set the font size of the label
  40. maxLines = 1, // Set the maximum number of lines for the label
  41. )
  42. },
  43. icon = {
  44. Crossfade(
  45. targetState = currentRoute == screen.route,
  46. animationSpec = tween(600), label = "" // 600ms animation duration
  47. ) { isSelected ->
  48. val icon: Painter = when (screen) {
  49. Screen.Home -> {
  50. if (isSelected) {
  51. painterResource(R.drawable.ic_home_filled_2)
  52. } else {
  53. painterResource(R.drawable.ic_home_empty_2)
  54. }
  55. }
  56. Screen.Settings -> {
  57. if (isSelected) {
  58. painterResource(R.drawable.ic_settings_filled_2)
  59. } else {
  60. painterResource(R.drawable.ic_settings_empty_2)
  61. }
  62. }
  63. Screen.Logs -> {
  64. if (isSelected) {
  65. painterResource(R.drawable.ic_log_filled_2)
  66. } else {
  67. painterResource(R.drawable.ic_log_empty_2)
  68. }
  69. }
  70. }
  71. Icon(
  72. painter = icon,
  73. contentDescription = screen.route,
  74. modifier = Modifier.size(18.dp) // Set the size of the icon
  75. )
  76. }
  77. }
  78. )
  79. }
  80. }
  81. }
  82. }

comments powered by Disqus