From the course: Android Compose with Kotlin
Kotlin for Jetpack Compose - Kotlin Tutorial
From the course: Android Compose with Kotlin
Kotlin for Jetpack Compose
- [Instructor] Jetpack Compose is deeply integrated with Kotlin. Kotlin offers unique conventions that simplify writing, effective Compose code. Building a stronger understanding of Kotlin's style can help you to make the most of Compose development. Specifically, let's look at how the following conventions simplify Compose code. Function default values, trailing lambdas, type inference, and higher order functions. This is composed code, and so is this. They both emit the same UI, yet one is taking full advantage of Kotlin conventions to improve readability and provide concise code. Let's take a closer look. We'll start here on line number 26. We're specifying the dark theme as well as the dynamic color, but if we take a closer look at the red 30 tech theme composable, we'll see that these are the default values. Therefore, we don't have to specify them, so we can just delete them. Now notice how we're getting closer to what we see on the left side. Next for the content value, since this is a trailing lambda, it can be brought outside of the parentheses, meaning we don't need to specify it, and instead we can directly bring up the parentheses. Great. This is exactly what we have in the Red 30 Tech app file on line number 23. The next thing we want to take a look at is jumping down to line number 28. We are defining the inner padding as padding values. However, the explicit parameter types are only necessary whenever the inference is impossible or to improve code clarity. In this case, we don't need it, so it can be removed. Now I did something special here. On line number 36, we're using the create image function composable. Now this really differs from what you see over here on line number 34 where we're directly calling image, but they do the same thing. Having functions start with an uppercase letter is confusing when you first see Compose code, but this create image is just a type alias of the compose image function. I wanted to show you that the behavior is the same. The uppercase naming is just part of compose convention, and you will get used to it eventually. So we can drop this create image and notice we can now bring in the correct image import. Perfect. If we go down to elevated button on line number 42, it's a higher order function. The on click is a callback for whenever the button is clicked. It takes in a lambda, and then we're providing a Lambda expression. You'll see this pattern used extensively in Compose, so you'll need to get used to seeing these Lambda expressions used for callback values. Now these are just some of the Kotlin conventions that help give Compose its power. Throughout the course we'll cover several more. Understanding the syntax will enable you to read and write Compose code more effectively.
Contents
-
-
-
Kotlin for Jetpack Compose3m 23s
-
Define screens with activities and composables2m 55s
-
(Locked)
Implement designs using composables3m 48s
-
(Locked)
Display UI elements with Column, Row, and Box4m 35s
-
(Locked)
Units of measurement with Compose2m 42s
-
(Locked)
Use modifiers to customize your layout5m 50s
-
(Locked)
Challenge: Bye-bye ConstraintLayout57s
-
(Locked)
Solution: Bye-bye ConstraintLayout2m 53s
-
-
-
-
-
-
-
-
-