Android使用Unresolved Reference为ScaffoldState抛出一个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()
}
)
}属地
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"发布于 2022-08-30 08:45:28
我看了一下我的依赖项,ScaffoldState是com.google.android.material:material:$materialVersion"的一部分。
在我的例子中,我有1.7.0-alpha03所以com.google.android.material:material:1.7.0-alpha03 (可能会有更新的版本)。
我确信,如果您添加了这一项,然后正确的导入:
import androidx.compose.material.ScaffoldState你应该能够使用它。
别忘了同步格莱德尔。
发布于 2022-08-28 19:01:56
是窗户。去File > Invalidate Caches > (Check Everything) Invalidate Caches and Restart。不妨在连接到internet的同时尝试一个Build > Rebuild Project,以确保所有依赖项都被正确下载。希望您不需要从内部文件夹手工分解部分下载的库.就像把头掉进脏衣服里一样。还有点让我想起复仇者队的场景,在那里他们修复了一个被部分炸毁的涡轮推进器。
https://stackoverflow.com/questions/73521366
复制相似问题