简体   繁体   中英

How to add a Composable inside a Layout

I am currently writing a custom layout; Therefore, I'm using Layout Composable.

Layout(
    modifier = modifier,
    content = content
) { measurables, constraints ->

// Measure my measurables and place my placeables using layout
}}

Now I want to draw using Canvas using the calculations of positions that were computed earlier x,y of the placeables.

I cannot add Canvas since a composable can only be invoked inside a Composable.

Any idea of how I would do that?

In this case you can use SubcomposeLayout , something like this:

SubcomposeLayout { constraints ->
    val contentMeasurables = subcompose(slotId = "content", content = content)
    val contentPlaceables = contentMeasurables.map { it.measure(constraints) }
    val canvasPlaceable = subcompose(slotId = "canvas") {
        Canvas(Modifier) {
            // draw using contentPlaceables
        }
    // when you now that you only have added a single view inside subcompose, 
    // you can take [0] from subcompose result
    // otherwise you also can use map as with content
    }[0].measure(constraints)
    layout(/*...*/)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM