Working with modifiers
Modifiers allow you to decorate or augment a composable. Using modifiers, you can:
Change the composable's size, layout, behavior, and appearance.
Add information, such as accessibility labels.
Handle user input.
Add high-level interactions like making elements clickable, scrollable, draggable, or zoomable.
Chaining modifiers
Modifiers can be chained together to apply multiple effects:
The order of modifier functions in the chain is significant. Each function makes changes to the Modifier
returned by the previous function, so the sequence of calls directly affects the composable's final behavior and appearance.
Built-in modifiers
Compose Multiplatform provides built-in modifiers, such as size
, padding
, and offset
, for handling common layout and positioning tasks.
Size modifiers
To set a fixed size, use the size
modifier. When constraints need to be overridden, use the requiredSize
modifier:
Padding modifiers
Add padding around an element with the padding
modifier. You can also apply padding dynamically in relation to baselines using paddingFromBaseline
:
Offset modifiers
To adjust the position of a layout from its original position, use the offset
modifier. Specify the offset in the X and Y axes:
Scoped modifiers
Scoped modifiers, also called parent data modifiers, notify the parent layout of specific requirements for a child. For example, to match the size of a parent Box
, use the matchParentSize
modifier:
Another example of a scoped modifier is weight
, available within the RowScope
or ColumnScope
. It determines how much space a composable should occupy relative to its siblings:
Extracting and reusing modifiers
When you chain modifiers together, you can extract the chain into variables or functions for reuse. This improves code readability and may enhance performance by reusing modifier instances.
Custom modifiers
While Compose Multiplatform provides many built-in modifiers for common use-cases right out of the box, you can also create your own custom modifiers.
There are several approaches to creating custom modifiers:
What's next
Learn more about modifiers in Jetpack Compose documentation.