Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
How can you create a reusable modifier in Compose?

Most of my screens(views) start out with a Box composable to contain all the child composables(components).

// background
Box(
        modifier = Modifier
            .fillMaxSize()
            .background(customColor)
    ) 
{
// foreground
Column()

}

Instead of having to create the modifier properties on every screen, how would you create a reusable modifier?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

In such a simple case, you can do the following (no need to use then):

fun Modifier.myModifier(customColor: Color) =
    this.fillMaxSize()
        .background(customColor)

In case you need to save any state in your modifier using remember or other state builders annotated with @Composable, you can do it with Modifier.composed. For example here's how you can add animation to your modifier:

fun Modifier.myModifier(customColor: Color): Modifier = composed {
    val animatedColor by animateColorAsState(customColor)
    this.fillMaxSize()
        .background(animatedColor)
}

Note, that inside composed you should only use state annotated composables, not views. More details can be found here.

over 4 years ago · Santiago Trujillo Report

0

You can create it like this

fun Modifier.myModifier(customColor: Color) = this.then(
    Modifier.fillMaxSize().background(customColor)
)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!