I have a composable that passes a Modifier
instance to its child composable as follows:
@Composable
fun MyComposable(
modifier: Modifier = Modifier,
content: @Composable BoxScope.() -> Unit,
) {
Box(
modifier = modifier.fillMaxWidth(),
content = content,
)
}
This adds the fillMaxWidth
modifier to the modifier
argument. However, this is not the desired behaviour because I would like fillMaxWidth
to be the default width, but still allow the caller to override it.
How do I combine/merge the two modifiers while making my local modifiers the default?
Modifiers allow you to decorate or augment a composable. Modifiers let you do these sorts of things: Change the composable's size, layout, behavior, and appearance. Add information, like accessibility labels.
A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It's similar to a Recyclerview in the classic Android View system.
Scaffold provides a slot for a floating action button. You can use the floatingActionButton slot and a FloatingActionButton : Scaffold( floatingActionButton = { FloatingActionButton(onClick = { /* ...
Jetpack Compose is a modern toolkit designed to simplify UI development. It combines a reactive programming model with the conciseness and ease of use of the Kotlin programming language.
Modifiers in Jetpack Compose are great. It's a consistent way to give additional functionality and shape to your composables. Most people compare modifiers with XML attributes in the old Android UI world.
Our experienced software engineers can jumpstart (and maintain) your new app. Get in touch for an honest conversation. Modifiers in Jetpack Compose are great. It's a consistent way to give additional functionality and shape to your composables.
Learn how to style your composables using modifiers. Style Note to make it look like it should in the final design. Add more composables to Jet Notes. From this point on, every composable you complete will be as beautiful as in your design, by adding those modifiers you’ve been hearing about for the past few chapters. :]
order matters when chaining modifiers as they're applied to the composable they modify from earlier to later, meaning that the measurement and layout of the modifiers on the left will affect the modifier on the right. The final size of the composable depends on all modifiers passed as a parameter.
You can simply use Modifier.then(otherModifier)
.
Note: Order is important and you might want to consider what you are adding yourself and what you are adding from outside.
composed
is used for stateful modifiers like when you want to implement custom touch controls where you will be called every-time anything changes.
See Composed Docs
Use the Modifier.composed
function.
@Composable
fun MyComposable(
modifier: Modifier = Modifier,
content: @Composable BoxScope.() -> Unit,
) {
OtherComposable(
modifier = Modifier.fillMaxWidth().composed { modifier },
content = content,
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With