【问题标题】:Why bottom sheet not work with bottom navigation in jetpack compose?为什么底部表不适用于jetpack compose中的底部导航?
【发布时间】:2022-04-15 02:58:19
【问题描述】:

我最近尝试学习jetpack compose,并且我在jetpack compose中有一个简单的项目,我的项目中有底部工作表,但是当我使用底部导航时,它不起作用,我在许多网站和stackoverflow中搜索,特别是,但我没有找到任何解决方案,我不知道我错过了什么?有什么想法吗?

 @Composable
fun BSDataScreen() {
    val modalBottomSheetState = rememberModalBottomSheetState(initialValue = 
 ModalBottomSheetValue.Hidden)
    val scope = rememberCoroutineScope()

    ModalBottomSheetLayout(
        sheetContent = {

            SheetScreen()
        },
        sheetState = modalBottomSheetState,
        sheetShape = RoundedCornerShape(topStart = 15.dp, topEnd = 15.dp),
        sheetBackgroundColor = Color.White,
      
    ) {
        Scaffold(

            backgroundColor =  Color.White,
        ) {
            DataScreen(
                scope = scope, state = modalBottomSheetState)}}}

@Composable
fun DataScreen(
    scope: CoroutineScope,
    state: ModalBottomSheetState
  ) {


    val listOfData = listOf(
        Data( painterResource(R.drawable.image1)),
        Data(painterResource(R.drawable.image2)),
        Data(painterResource(R.drawable.image3),)

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)

    ) {

        LazyColumn(
            modifier = Modifier

        ) {

            items(listOfData.size) { index ->
                DataListItem(listOfData[index]) data: Data->

        scope.launch {
            state.show()
        }
 
     }}}}


@Composable
fun DataListItem(data: Data, onLongClick: (Data) -> Unit) {

    val context = LocalContext.current

    Column(
        modifier = Modifier.padding(5.dp)
    ) {

        Row(

            modifier = Modifier
                .fillMaxWidth()
                .padding(5.dp)
                .combinedClickable(
                    onLongClick= {
               onLongClick(data)
     },)

        ) {

            Image(
                painter = data.painter,
                contentDescription = null,)}}}
      

底部导航:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MainScreen()
        }
    }
}

@Composable
fun MainScreen() {
    val navController = rememberNavController()
    Scaffold(

        bottomBar = { BottomNavigationBar(navController) }
    ) {
        Navigation(navController = navController)
    }
}

 
 @Composable
 fun Navigation(navController: NavHostController) {
     val modalBottomSheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
     val scope = rememberCoroutineScope()
    NavHost(navController, startDestination = NavigationItem.Data.route) {

        composable(NavigationItem.Data.route) {
          DataScreen(
              scope = scope, state = modalBottomSheetState
          )

        }
        composable(NavigationItem.Data2.route) {
            Data2Screen()
        }
        composable(NavigationItem.Data3.route) {
            Data3Screen()
        }
        composable(NavigationItem.Data4.route) {
            Data4Screen()
        }
        composable(NavigationItem.Data5.route) {
            Data5Screen()
        }
    }
 }

@Composable
fun BottomNavigationBar(navController: NavController

    ) {
    val items = listOf(
        NavigationItem.Data,
        NavigationItem.Data2,
        NavigationItem.Data3,
        NavigationItem.Data4,
        NavigationItem.Data5

    )
   
    BottomNavigation(
        backgroundColor = colorResource(id = R.color.white),
        contentColor = colorResource(id = R.color.black)

    ) {
        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val currentRoute = navBackStackEntry?.destination?.route
        items.forEach { item ->
            BottomNavigationItem(
                icon = {
                    Icon(
                        painterResource(id = item.icon),
                        contentDescription = null
                    )
                },
                selectedContentColor = colorResource(id = R.color.red),
                unselectedContentColor = colorResource(id = R.color.blue),
                alwaysShowLabel = true,
                selected = currentRoute == item.route,
                onClick = {
                    navController.navigate(item.route) {

                        navController.graph.startDestinationRoute?.let { route ->
                            popUpTo(route) {
                                saveState = true
                            }
                        }

                        launchSingleTop = true

                        restoreState = true
                    }})}}}
           

【问题讨论】:

    标签: android kotlin android-jetpack-compose android-bottomnav android-bottomsheetdialog


    【解决方案1】:

    我认为您的问题是您的底部工作表出现在底部栏下方。我解决的同样的事情非常简单。

    @Composable
    fun SettingView() {
        val navController = rememberNavController()
        val scaffoldState = rememberScaffoldState()
        Scaffold(
            bottomBar = {
                BottomBar(navController = navController)
            },
            content = {
                Box(modifier = Modifier.padding(it)) {
                    BottomNavGraph(
                        navController = navController,
                        scaffoldState = scaffoldState
                    )
                }
            }
        )
    }
    

    内容屏幕用 Box 和 padding 包裹到脚手架的 PaddingValues。

    【讨论】:

    • tnx 这么多:)
    • 如果我的回答解决了你的问题,我很高兴。
    【解决方案2】:

    只需使用 ModalBottomSheetLayout 包裹整个屏幕。

    你可以输入以下代码:

    Scaffold(
    
            bottomBar = { BottomNavigationBar(navController) }
        ) {
            Navigation(navController = navController)
        }
    

    里面

    ModalBottomSheetLayout(...){
        Scaffold(
    
            bottomBar = { BottomNavigationBar(navController) }
        ) {
            Navigation(navController = navController)
        }
    }
    

    【讨论】:

    • tnc 这么多你的答案,但我还是不明白,你能根据问题更新你的答案吗,我还不明白:)
    • @Skysoft13 我已经更新了答案,我还用内容更改了 sheetContent。
    • 还是不行,不知道
    【解决方案3】:

    尝试使用 Accompanist 提供的 Navigation Material 库。 https://google.github.io/accompanist/navigation-material/

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • @Jeff Brown,你的答案非常重要,但如果可能的话,请在这里给出明确的答案
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    相关资源
    最近更新 更多