未解决的 ScaffoldState 与 Material3 的参考错误

Raj*_*nan 8 android material-components-android android-jetpack-compose android-jetpack-compose-material3

Android Studio 抛出Unresolved Reference错误ScaffoldStatewith Material3. 我怎样才能让它发挥作用?

import androidx.compose.foundation.clickable
import androidx.compose.material3.*
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.navigation.NavController
import kotlinx.coroutines.launch

@Composable
fun CustomAppBar(
    title: String,
    backGroundColor: Color = Color.White,
    actions: @Composable () -> Unit = { },
    scaffoldState: ScaffoldState? = null, // Errors here...
    navController: NavController,
) {
    val scope = rememberCoroutineScope()

    SmallTopAppBar(
        title = {
            Text(
                title,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        },
        colors = TopAppBarDefaults.smallTopAppBarColors(
            containerColor = containerBackGroundColor,
            titleContentColor = titleContentColor
        ),
        navigationIcon = if (navController?.previousBackStackEntry != null) {
            {
                IconButton(onClick = { navController.navigateUp() }) {
                    Icon(
                        imageVector = Icons.Filled.ArrowBack,
                        contentDescription = "Back"
                    )
                }
            }
        } else {
            {
                IconButton(onClick = {
                    scope.launch {
                        scaffoldState?.drawerState?.open()
                    }
                }) {
                    Icon(
                        Icons.Filled.Menu,
                        contentDescription = "Nav drawer icon",
                    )
                }
            }
        },
        actions = {
            actions()
        }
    )
}
Run Code Online (Sandbox Code Playgroud)

依赖关系

implementation "androidx.core:core-ktx:1.8.0"
implementation "androidx.compose.ui:ui:1.2.1"
implementation "androidx.compose.material3:material3:1.0.0-beta01"
implementation "androidx.compose.ui:ui-tooling-preview:1.2.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
implementation "androidx.activity:activity-compose:1.3.1"
implementation "androidx.compose.compiler:compiler:1.3.0"
implementation "androidx.navigation:navigation-runtime:2.5.1"
implementation "com.google.accompanist:accompanist-navigation-animation:0.23.1"
Run Code Online (Sandbox Code Playgroud)

Mar*_*ini 4

我已经查看了我的依赖项,并且ScaffoldStatecom.google.android.material:material:$materialVersion".

就我而言,我有 1.7.0-alpha03这样的com.google.android.material:material:1.7.0-alpha03(当时可能有更新的版本)。

我确定您是否添加了该内容,然后进行了正确的导入:

import androidx.compose.material.ScaffoldState你应该能够使用它。

不要忘记同步 Gradle。

  • 我不知道这会违反哪种“最佳”实践。您“可以/应该”拥有一个包含这些依赖项的 gradle 模块,并且您的每个功能(无论它们是否是单独的模块)都将提取此 Material 模块提供的依赖项。并非所有 Material3 都已准备就绪,因此一段时间内您需要同时拥有两者。 (2认同)