jetpack_compose_tutorial
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| jetpack_compose_tutorial [2023/11/20 15:44] – [Jetpack Compose Tutorial] kalaschnikow | jetpack_compose_tutorial [2023/11/20 15:44] (current) – [Jetpack Compose Tutorial] kalaschnikow | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Jetpack Compose Tutorial ====== | ||
| + | |||
| + | |||
| + | [[https:// | ||
| + | |||
| + | |||
| + | ===== Lesson 1: Composable functions ===== | ||
| + | * To create a composable function just add '' | ||
| + | * In '' | ||
| + | * The '' | ||
| + | * Composable functions can only be called from other composable functions. | ||
| + | * '' | ||
| + | * The function annotated with '' | ||
| + | |||
| + | ===== Lesson 2: Layouts ===== | ||
| + | |||
| + | * In Compose, a UI hierarchy will be built by calling composable functions from other composable functions | ||
| + | | ||
| + | * '' | ||
| + | * author | ||
| + | * text | ||
| + | |||
| + | <code kotlin> | ||
| + | @Composable | ||
| + | fun MessageCard(msg: | ||
| + | Column { | ||
| + | Text(text = msg.author) | ||
| + | Text(text = msg.body) | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | * '' | ||
| + | * author | text | ||
| + | |||
| + | |||
| + | * To decorate or configure a composable Jetpack Compose uses modifiers: | ||
| + | |||
| + | <code kotlin> | ||
| + | @Composable | ||
| + | fun MessageCard(msg: | ||
| + | // Add padding around our message | ||
| + | Row(modifier = Modifier.padding(all = 8.dp)) { | ||
| + | Image( | ||
| + | painter = painterResource(R.drawable.profile_picture), | ||
| + | contentDescription = " | ||
| + | modifier = Modifier | ||
| + | // Set image size to 40 dp | ||
| + | .size(40.dp) | ||
| + | // Clip image to be shaped as a circle | ||
| + | .clip(CircleShape) | ||
| + | ) | ||
| + | |||
| + | // Add a horizontal space between the image and the column | ||
| + | Spacer(modifier = Modifier.width(8.dp)) | ||
| + | |||
| + | Column { | ||
| + | Text(text = msg.author) | ||
| + | // Add a vertical space between the author and message texts | ||
| + | Spacer(modifier = Modifier.height(4.dp)) | ||
| + | Text(text = msg.body) | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Lesson 3: Material design ===== | ||
| + | |||
| + | * Jetpack Compose provides an implementation of Material Design 3 | ||
| + | * To start, wrap the '' | ||
| + | |||
| + | | ||
| + | * Material Design is built around three pilars: | ||
| + | * Color | ||
| + | * '' | ||
| + | * Typography | ||
| + | * '' | ||
| + | * '' | ||
| + | * Shape | ||
| + | * '' | ||
| + | * '' | ||
| + | * Enable dark theme | ||
| + | * '' | ||
| + | |||
| + | ===== Lesson 4: Lists and animations ===== | ||
| + | |||
| + | * Create a list of messages | ||
| + | * Use Compose' | ||
| + | * => efficient | ||
| + | * Animate message while expanding | ||
| + | * to keep track of whether message has been expanded or not, use the Composable functions | ||
| + | * '' | ||
| + | * '' | ||
| + | * Recomposition | ||
| + | * redraw automatically when the value is updated | ||
| + | |||
| + | |||
