0

我想在 Jetpack Compose 中Scaffold进行导航。BottomNavigation它们位于MainPage 中。但是当我单击MainPage中的内容并导航到DetailPage时,它​​们BottomNavigation也存在,如图所示:

这是带有底部导航的 MainPage

这是从 MainPage 导航的 DetailPage

如何隐藏BottomNavigationDetailPage ?

4

3 回答 3

1

您需要指定要显示的屏幕和不想要的屏幕;否则,它将显示在Scaffold's body 内的所有屏幕上(您拥有的bottomBar)。下面的代码来自我的应用程序。

创建一个状态来观察 navController 上的任何目标更改

在里面when你可以放任何你想显示的屏幕,navigationBar否则只是设置currentScreenNoBottomBar

@Composable
private fun NavController.currentScreen(): State<MainSubScreen> {
    val currentScreen = remember { mutableStateOf<MainSubScreen>(MainSubScreen.Home) }

    DisposableEffect(key1 = this) {
        val listener = NavController.OnDestinationChangedListener { _, destination, _ ->
            when {
                destination.hierarchy.any { it.route == MainSubScreen.Home.route } -> {
                    currentScreen.value = MainSubScreen.Home
                } else -> currentScreen.value = MainSubScreen.NoBottomBar
            }
        }
        addOnDestinationChangedListener(listener)
    }
    return currentScreen
}

在你放置你的底部栏的脚手架上

所以你可以检查是否currentScreenNoBottomBar,不要显示它

// initialized currentScreeen above
val currentScreen by navController.currentScreen()

    Scaffold(
        bottomBar = {
            if (currentScreen != MainSubScreen.NoBottomBar) {
                MainBottomNavigation()
            } else Unit
        }
    ) {
        // Your screen
    }
于 2021-08-31T04:31:43.963 回答
0

这取决于您放置 NavHost 的位置。如果它包裹了你的 Scaffold,那么你导航 Composable 覆盖整个屏幕

NavHost(
    navController = navController,
    startDestination = "start_destination"
) {
     composable(route = "start_destination") { ->
        Scaffold(){...}
    }
}

如果 NavHost 在里面content: @Composable (PaddingValues) -> Unit,当您导航时,您的TopAbbpar,TabsBottomNavigation保持在屏幕上

于 2021-08-31T04:47:56.280 回答
0

我喜欢一种方法。
尝试使用ViewModle来控制BottomNavigation的可见性,进入MainPage时显示,离开MainPage时隐藏

// ViewModel
class VM: ViewModel() {
  private val _state: MutableLiveData<Boolean> = MutableLiveData(true)
  val state: LiveData<Boolean> get() = _state
  fun setState(status: Boolean) {
      _state.postValue(status)
  }
}


// MainPage
@Compose MainPage(vm: VM) {
  LaunchedEffect(key1 = true) {
    vm.setState(true)
  }

  DisposableEffect(key1 = true) {
    onDispose {
      vm.setState(false)
    }
  }
}


// page contains Scaffold
@Composable
fun Greeting(vm: VM) {
  // State of BottomNavigation`s visibility
  val state = remember { mutableStateOf<Boolean>(true) }
  // read the BottomNavigation`s visibility from ViewModel and send to State
  vm.state.observeAsState().value?.let { state.value = it }

  Scaffold(bottomBar = {
    // show / hide BottomNavigation controlled by State
    state.takeIf { it.value }?.let {
        BottomNavigation {
            list.forEachIndexed { index, label ->
                BottomNavigationItem(
                    label = { Text(text = label) },
                    selected = index == selectedItem.value,
                    onClick = { selectedItem.value = index },
                    icon = {
                        Icon(
                            painter = painterResource(id = R.drawable.ic_launcher_foreground),
                            contentDescription = null
                        )
                    })
            }
        }
    }
  }) {
    NavHost(navController = navController, startDestination = "one") {
        composable(route = "one") { PageList(navController, vm) }
        composable(route = "detail") { PageDetail(vm) }
      }
    }
  }

如果您想快速隐藏底部导航,您可以在导航之前隐藏底部导航。

于 2021-09-03T09:11:35.267 回答