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

134
Views
Scope composables to a parent composable

I'm making an implementation of PreferencesScreen in Compose and I've made all the components like PreferencesSwitch, CheckBox, etc.
Now I'm wondering if there is any way to make it so all the components can only be used inside the scope of the PreferencesScreen function and cannot be used outside of it.
Like for example, in LazyColumn, items can only be used inside LazyColumnScope. I looked at the implementation of it but it used the annotation @LazyScopeMarker so I'm assuming there's different markers for different scopes?

Expected Behaviour:

PreferencesScreen{
    PreferencesCheckBox(...){ ... }
}

is possible but,

PreferencesCheckBox(...){ ... }

alone is not possible.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can declare some scope same like LazyColumn does:

interface PreferencesScreenScope {
    @Composable
    fun PreferencesCheckBox()
}

private class PreferencesScreenScopeImpl: PreferencesScreenScope {
    @Composable
    override fun PreferencesCheckBox() {

    }
}

interface/class ...Impl is used here to make sure that no other screen can reuse PreferencesScreenScopeImpl, also it adds testing possibility.

Use it in PreferencesScreen:

@Composable
fun PreferencesScreen(content: @Composable PreferencesScreenScope.() -> Unit ) {
    PreferencesScreenScopeImpl().content()
}

Use PreferencesScreen like this:

PreferencesScreen {
    PreferencesCheckBox()
}
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!