Paged

fun Paged(modifier: Modifier = Modifier, pageIndex: Int = 0, pageModifier: Modifier = Modifier, clipLastBreakpoint: Boolean = true, drawBreakpoints: Boolean = false, onPageLayout: (pageCount: Int) -> Unit? = null, content: @Composable () -> Unit)

Divides a composable into discreet pages. The content is measured with unbounded height, and then displayed using the current constraints, starting at pageIndex (the content is translated up). The bottom of the content is clipped, but a best-effort attempt is made to not cut any individual composables off in the middle. When the initial "pagination" measurement is complete, onPageLayout is invoked with the total page count.

Pagination

In order to calculate where to clip each page, this composable uses the Compose tooling library to analyze the entire slot table. It collects all the leaf LayoutNodes (those without children) and reports their bounds. The nodes are then sorted by their bottom bound, and nodes that are overlapped are removed (the bottom-most composables are kept). The remaining nodes bounds are returned as PageBreakpoints in a PageLayoutResult. The Modifier.keepOnSamePage modifier can be used to keep a composable's children together.

The PageLayoutResult then iterates through all the breakpoints and determines the offsets of the start of each page (see PageLayoutResult.pageOffsetsPx).

There are some known issues with the current implementation:

  • Only the slot table for the current composition is analyzed. Any children which use subcomposition (e.g. WithConstraints, LazyColumn) will be considered as a "leaf" composable.

  • Large text blocks are treated as a single unit, individual lines will not be broken across pages.

  • Nested Paged composables are not supported (behavior is undefined).

Parameters

modifier

A Modifier that will always be applied to content.

pageIndex

The index of the page to render. Valid values are between 0 and the value passed to the onPageLayout callback.

pageModifier

A Modifier that is applied to each page, and not affected by pagination.

clipLastBreakpoint

If false, the content at the end of the current page will be clipped exactly at the page bounds, not at the nearest breakpoint. True by default.

drawBreakpoints

If true, horizontal lines are drawn at each breakpoint for debugging. False by default.

onPageLayout

Callback that will be invoked after calculating the total number of pages.