如何将Scaffold
中的content
设置为可通过内容填充重用。在声明contentPadding
时,是否可以从可组合对象中提取它?例如MyScaffoldContent
找不到具有此名称的参数:内容填充
@Composable
fun MyReusableScaffold(scaffoldTitle: String, scaffoldContent: @Composable () -> Unit) {
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
LargeTopAppBar(
title = { Text(text = scaffoldTitle) },
scrollBehavior = scrollBehavior
)
},
content = { contentPadding ->
scaffoldContent(contentPadding = contentPadding)
}
)
}
// Example composable
@Composable
fun MyScaffoldContent(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues()
) {
Text(
text = "Header $item",
modifier = Modifier.fillMaxWidth()
)
}
// Example update
@Composable
fun MyHomeScreen(navController: NavController) {
MyReusableScaffold(
scaffoldTitle = "Hello Android",
scaffoldContent = MyScreenContent(contentPadding = ?))
)
}
@Composable
fun MyScreenContent(modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues()) {
Text(
text = "Header",
modifier = Modifier.fillMaxWidth()
)
}
1条答案
按热度按时间eqqqjvef1#
如果您希望
MyHomeScreen
可组合函数使用MyReusableScaffold
中的内容填充值,则必须在scaffoldContent
lambda中定义一个parameter
,并将contentPadding
传递给它。