Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine multiple Modifier objects in Jetpack Compose?

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?

like image 910
Duncan Luk Avatar asked May 04 '21 12:05

Duncan Luk


People also ask

What are modifiers in jetpack compose?

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.

What is LazyColumn in jetpack compose?

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.

What is scaffold in jetpack compose?

Scaffold provides a slot for a floating action button. You can use the floatingActionButton slot and a FloatingActionButton : Scaffold( floatingActionButton = { FloatingActionButton(onClick = { /* ...

Is jetpack compose reactive?

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.

What is the use of modifiers in jetpack compose?

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.

Why choose jetpack compose for your next app?

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.

How do I style my composables using modifiers?

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. :]

Does order matter when chaining modifiers to a composable?

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.


Video Answer


2 Answers

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

like image 122
Aman Kapoor Avatar answered Oct 23 '22 05:10

Aman Kapoor


Use the Modifier.composed function.

@Composable
fun MyComposable(
    modifier: Modifier = Modifier,
    content: @Composable BoxScope.() -> Unit,
) {
    OtherComposable(
        modifier = Modifier.fillMaxWidth().composed { modifier },
        content = content,
    )
}
like image 32
Duncan Luk Avatar answered Oct 23 '22 03:10

Duncan Luk