android 如何使填充可重复用于支架内容

ggazkfy8  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(127)

如何将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()
    )
}
eqqqjvef

eqqqjvef1#

如果您希望MyHomeScreen可组合函数使用MyReusableScaffold中的内容填充值,则必须在scaffoldContent lambda中定义一个parameter,并将contentPadding传递给它。

@Composable
fun MyReusableScaffold(scaffoldContent: @Composable ( contentPadding: PaddingValues) -> Unit) {
    Scaffold(
        topBar = {

        },
        content = { contentPadding ->
            scaffoldContent(contentPadding = contentPadding)
        }
    )
}

@Composable
fun MyScaffoldContent(
    modifier: Modifier = Modifier,
    contentPadding: PaddingValues = PaddingValues()
) {
    Text(
        text = "Header",
        modifier = Modifier.fillMaxWidth()
    )
}

@Composable
fun MyHomeScreen(navController: NavController) {
    MyReusableScaffold(
        scaffoldContent = {
            MyScreenContent(contentPadding = it)
        }
    )

//    or 
//    MyReusableScaffold(
//        scaffoldContent = {
//            MyScaffoldContent(contentPadding = it)
//        }
//    )
}

相关问题