

Jetpack Compose: Modifiers fundamentals
source link: https://www.rockandnull.com/jetpack-compose-modifier/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Jetpack Compose: Modifiers fundamentals
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. But they are quite different. In this post, I will cover the biggest differences I noticed, that makes them way more powerful and flexible than attributes.
Repeatability
Although it looks like the Modifier
follows the builder pattern, it's not. You are not setting a value to a property when calling each method. You are applying operations when calling those methods. This means you can call each method multiple times and each method will be applied again and again.
Where might this be useful? One interesting example is the use of decorating modifiers to decorate your composables. For instance, in the snippet below we can create two borders around our Text
by calling border
multiple times.
Text(
"Hello word",
modifier = Modifier
.border(1.dp, Color.Black)
.padding(16.dp)
.border(1.dp, Color.Black)
)

Ordering
Ordering matters (you might have figured it out from the previous snippet already). This is interesting in the sense that dedicated XML attributes of the old Android UI world can be expressed by different ordering of Modifier
methods.
For instance, there are no different padding and margin Modifier
methods. You accomplish the same results using the different ordering of the padding
method.
Text(
"Hello world",
modifier = Modifier
.padding(8.dp) // "Margin"
.clip(RoundedCornerShape(4.dp))
.background(Color.Gray)
.padding(16.dp) // "Padding"
)

Consistency
The list of built-in Compose Modifiers can be applied to (almost) every composable that accepts a Modifier
(with the exception of some Modifier
s that are restricted to specific scopes, e.g. RowScope, BoxScope). This is unlike the XML attributes that had specific attributes for specific elements. It creates uniformity and it's easier to remember how to accomplish common tasks.
Such a common task is how to set a click handler. Just set the clickable
method and you are done.
Text(
"Hello world",
modifier = Modifier
.clickable(
onClick = {
// Do things
})
)
Hopefully, by now you have grasped some fundamentals on how Modifer
works that will make your life navigating around Compose a bit easier.
Happy coding!
Recommend
-
36
Google 在 I/O 2019 上宣布“Kotlin-first”,并表示接下来许多新的 Jetpack API 和功能将首先在 Kotlin 中提供,当天谷歌就开源 Jetpack Compose...
-
58
r/Kotlin: Discussion about Kotlin, statically typed programming language for the JVM, Android and the browser.
-
51
-
27
去年,我们推出了 Android Jetpack ,这是一套软件组件,旨在加速 Android 开发并简化高质量应用程序的编写。Jetpack 是...
-
14
Photo by Dayne Topkin on UnsplashHow to master Swipeable and NestedScro...
-
2
Learning Android DevelopmentTesting Composable in Jetpack Compose Android - Testing FundamentalsDon’t judge a UI by its cover!
-
8
Fundamentals of Compose layouts and modifiersEpisode 1 of MAD Skills: Compose Layouts and Modifiers
-
9
Layout modifiers in Jetpack ComposeCommunity PostIn this article, Jorge Castillo shares his...
-
6
Jetpack Compose Tricks: Conditionally Applying Modifiers for Dynamic UIsStreamline your UI code
-
7
Unleashing Creativity with Custom Modifiers in Android Jetpack ComposeElevate Your UI Design — From Gradient Backgrounds to Interactive Gestures, Harness the Power of Custom Modifiers
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK