Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Modifiers in Compose

Modifiers in Compose

Did you know the order of modifiers matter? And much more.

Beatrice Kinya

November 12, 2023
Tweet

More Decks by Beatrice Kinya

Other Decks in Technology

Transcript

  1. Beatrice Kinya GDE for Android | Android Engineer Also, I

    create content @ Droicon Academy Pro Hiker| Mountaineer… Twitter: @B__Kinya LinkedIn: Beatrice Kinya
  2. Modifiers - Allow you to tweak appearance and behaviour of

    composables. Image( modifier = Modifier .padding(16.dp) .offset {...} .background(color = …) .anchoredDraggable(state, …), )
  3. Chaining Modifiers What will the following code produce? Image( modifier

    = Modifier .padding(16.dp) .background(color = …) .anchoredDraggable(state, …) .offset {... }, ) A
  4. Chaining Modifiers What will the following code produce? Image( modifier

    = Modifier .padding(16.dp) .background(color = …) .anchoredDraggable(state, …) .offset {... }, ) B
  5. Chaining Modifiers - If you chain multiple modifiers, the modifier

    wraps the rest of the chain and the layout node within.
  6. Constraints - Constraints are passed down from parent to child

    in a UI tree. - Constraints determine the maximum and minimum bounds for a node’s width and height.
  7. Constraints: This won’t work Box(modifier = modifier.width(300.dp)) { Image( modifier

    = Modifier .fillMaxWidth() .padding(16.dp), … ) } Image( modifier = Modifier .fillMaxWidth() .size(90.dp), … )
  8. @Composable fun VersionImage( modifier: Modifier = Modifier ) { Box(modifier

    = modifier) { Image( painter = painterResource(id = R.drawable.android_14_preview), contentDescription = null) } }
  9. @Composable fun DraggableSample( modifier: Modifier = Modifier ) { Box(modifier

    = modifier) { VersionImage( modifier = Modifier .size(200.dp) .padding(16.dp) .anchoredDraggable(state, orientation = Orientation.Horizontal) .offset {... }, ) } } A
  10. @Composable fun Sample2( modifier: Modifier = Modifier, ) { Box(modifier

    = modifier.background(color = lightGreen)) { VersionImage( modifier = Modifier .fillMaxWidth() .padding(16.dp), ) } } B
  11. Resources - Docs: Constraints and Modifiers: https://developer.android.com/jetpack/compose/layouts/constraints-modifiers - Always provide

    a modifier parameter: https://chrisbanes.me/posts/always-provide-a-modifier/ - Twitter compose rules: https://twitter.github.io/compose-rules/rules/#modifiers