I wanna do some kind of Composable Inheritance. I ...
# compose
p
I wanna do some kind of Composable Inheritance. I need a parent composable with a Scaffold, and the childrens must have the inner content to the scaffold. How can I apply the innerPadding to the inherited children? Parent:
Copy code
@Composable
fun SectionScreen(
    section: Section,
    modifier: Modifier = Modifier,
    content: @Composable (modifier: Modifier) -> Unit,
) {
    Scaffold(
        modifier = modifier,
        topBar = {
            Text(text = "Top Section Header -> sectionID: ${section.sectionID}")
        },
        bottomBar = {
            Text(text = "Top Section Footer -> sectionID: ${section.sectionID}")
        }
    ) { innerPadding ->
        content(Modifier.padding(innerPadding))
    }
}
Children:
Copy code
@Composable
fun InfoSectionScreen(section: Section, modifier: Modifier = Modifier) = SectionScreen(
    section = section,
    modifier = modifier,
) {
    Column(
        modifier = modifier
    ) {
        Text(text = "Section Content -> sectionID: ${section.sectionID}")
    }
}
As you can see, the issue is in this line:
Copy code
content(Modifier.padding(innerPadding))
I need to pass the innerpAdding but I don't know how to receive it on InfoSectionScreen
m
What if you just pass in the padding value as a primitive, and then let the child Composable within
content
apply the modifier itself? Parent:
Copy code
@Composable
fun SectionScreen(
    section: Section,
    modifier: Modifier = Modifier,
    content: @Composable (padding: Dp) -> Unit,
) {
    Scaffold(
        modifier = modifier,
        topBar = {
            Text(text = "Top Section Header -> sectionID: ${section.sectionID}")
        },
        bottomBar = {
            Text(text = "Top Section Footer -> sectionID: ${section.sectionID}")
        }
    ) { innerPadding ->
        content(innerPadding)
    }
}
Child:
Copy code
@Composable
fun InfoSectionScreen(section: Section, modifier: Modifier = Modifier) = SectionScreen(
    section = section,
    modifier = modifier,
) { paddingValue ->
    Column(
        modifier = modifier.padding(paddingValue)
    ) {
        Text(text = "Section Content -> sectionID: ${section.sectionID}")
    }
}
(idk if
Dp
is the right type for the padding param, but hopefully you get the idea)
p
yep it works like that, I was testing that solution seconds ago
thank you very much
🙌 1
instead of
content: @Composable (padding: Dp) -> Unit,
it should be
content: @Composable (paddingValues: PaddingValues) -> Unit,
but the idea is the same, works perfectly 🙂