You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Compose Material BottomSheet导航遇Material3冲突警告,求替代方案

Material3下实现BottomSheet导航路由解决方案

你当前使用compose.material.navigation.bottomSheet导致和Material3库冲突的警告,Material3完全提供了对应的BottomSheet导航功能,只需把相关API切换到Material3版本即可解决:

步骤1:替换依赖与导入包

将原来的androidx.navigation:navigation-compose-material依赖替换为Material3版本:

implementation "androidx.navigation:navigation-compose-material3:2.7.5" // 版本号可按需调整

同时代码中导入包替换为:

import androidx.navigation.compose.material3.rememberBottomSheetNavigator
import androidx.compose.material3.ModalBottomSheetLayout

步骤2:修改代码实现

调整后的完整代码如下:

val bottomSheetNavigator = rememberBottomSheetNavigator()
val navController = rememberNavController(bottomSheetNavigator)

// 使用Material3的ModalBottomSheetLayout
ModalBottomSheetLayout(
    sheetState = bottomSheetNavigator.sheetState,
    sheetContent = { bottomSheetNavigator.sheetContent() }
) {
    NavHost(navController, Destinations.Home) {
        composable(Destinations.Home) {
            HomeScreen(
                showSheet = {
                    navController.navigate(Destinations.Sheet + "?arg=From Home Screen")
                },
                showFeed = { navController.navigate(Destinations.Feed) }
            )
        }
        composable(Destinations.Feed) { Text("Feed!") }
        // 路由定义逻辑与参数传递方式保持不变
        bottomSheet(Destinations.Sheet + "?arg={arg}") { backstackEntry ->
            val arg = backstackEntry.arguments?.getString("arg") ?: "Missing argument :("
            BottomSheet(
                showFeed = { navController.navigate(Destinations.Feed) },
                showAnotherSheet = {
                    navController.navigate(Destinations.Sheet + "?arg=${UUID.randomUUID()}")
                },
                arg = arg
            )
        }
    }
}

关键说明

  • Material3的ModalBottomSheetLayout需要显式传入sheetState和sheetContent,这是和Material版本的核心区别
  • bottomSheet路由的定义、参数传递逻辑完全和之前一致,无需额外修改
  • 警告根源是同时引入Material和Material3库,统一使用Material3的导航API后警告会自动消失

内容的提问来源于stack exchange,提问作者user924

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 16:42:08