Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda