• 《Jetpack Compose从入门到实战》第三章 定制 UI 视图


    在这里插入图片描述

    配置颜色、字体与形状

    -ui.theme.Color.kt

    val pink100 = Color(0xFFFFF1F1)
    val pink900 = Color(0xFF3F2C2C)
    val white = Color(0xFFFFFFFF)
    val white850 = Color(0xD9FFFFFF)
    val gray = Color(0xFF232323)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • ui.theme.Type.kt
      先将Nunito Sans字体家族放入 res/font,再根据设计稿写代码
    val nunitoSansFamily = FontFamily(
        Font(R.font.nunitosans_light, FontWeight.Light),
        Font(R.font.nunitosans_semibold, FontWeight.SemiBold),
        Font(R.font.nunitosans_bold, FontWeight.Bold)
    )
    
    val h1 = TextStyle(
        fontSize = 18.sp,
        fontFamily = nunitoSansFamily,
        fontWeight = FontWeight.Bold
    )
    val h2 = TextStyle(
        fontSize = 14.sp,
        letterSpacing = 0.15.sp,
        fontFamily = nunitoSansFamily,
        fontWeight = FontWeight.Bold
    )
    val subtitle1 = TextStyle(
        fontSize = 16.sp,
        fontFamily = nunitoSansFamily,
        fontWeight = FontWeight.Light
    )
    val body1 = TextStyle(
        fontSize = 14.sp,
        fontFamily = nunitoSansFamily,
        fontWeight = FontWeight.Light
    )
    val body2 = TextStyle(
        fontSize = 12.sp,
        fontFamily = nunitoSansFamily,
        fontWeight = FontWeight.Light
    )
    val button = TextStyle(
        fontSize = 14.sp,
        letterSpacing = 1.sp,
        fontFamily = nunitoSansFamily,
        fontWeight = FontWeight.SemiBold,
        color = white
    )
    val caption = TextStyle(
        fontSize = 12.sp,
        fontFamily = nunitoSansFamily,
        fontWeight = FontWeight.SemiBold
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • ui.theme/Shape.kt

    Welcome Page

    @Preview(showBackground = true)
    @Composable
    fun WelcomeTitle() {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxWidth()
        ) {
            Image(
                painter = rememberVectorPainter(image = ImageVector.vectorResource(id = R.drawable.ic_light_logo)),
                contentDescription = "weclome_logo",
                modifier = Modifier
                    .wrapContentWidth()
                    .height(32.dp)
            )
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(32.dp),
                contentAlignment = Alignment.BottomCenter
            ) {
                Text(
                    text = "Beautiful home garden solutions",
                    textAlign = TextAlign.Center,
                    style = subtitle1,
                    color = gray
                )
            }
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun WelcomeButtons() {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxWidth()
        ){
            Button(
                onClick = { },
                modifier = Modifier
                    .height(48.dp)
                    .padding(horizontal = 16.dp)
                    .fillMaxWidth()
                    .clip(medium),
                colors = ButtonDefaults.buttonColors(backgroundColor = pink900)
            ) {
                Text(
                    text = "Create account",
                    style = button,
                    color = white
                )
            }
            Spacer(modifier = Modifier.height(24.dp))
            TextButton(
                onClick = { },
            ) {
                Text(
                    text = "Log in",
                    style = button,
                    color = pink900,
                )
            }
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun LeafImage() {
        Image(
            painter = rememberVectorPainter(image = ImageVector.vectorResource(id = R.drawable.ic_light_welcome_illos)),
            contentDescription = "weclome_illos",
            modifier = Modifier
                .wrapContentSize()
                .padding(start = 88.dp)
        )
    }
    
    @Preview(showBackground = true)
    @Composable
    fun WelcomeContent() {
        Column(modifier = Modifier
            .fillMaxSize()
        ) {
            Spacer(Modifier.height(72.dp))
            LeafImage()
            Spacer(modifier = Modifier.height(48.dp))
            WelcomeTitle()
            Spacer(modifier = Modifier.height(40.dp))
            WelcomeButtons()
        }
    }
    
    @Composable
    fun WelcomePage() {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(pink100)
        ) {
            Image(
                painter = rememberVectorPainter(image = ImageVector.vectorResource(id = R.drawable.ic_light_welcome_bg)),
                contentDescription = "weclome_bg",
                modifier = Modifier.fillMaxSize()
            )
            WelcomeContent()
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107

    Login Page

    @Preview(showBackground = true)
    @Composable
    fun TopText() {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            var keywordPre = "By Clicking below you agree to our".split(" ")
            var keywordPost = "and consent".split(" ")
            for (word in keywordPre) {
                Text(
                    text = word,
                    style = body2,
                    color = gray,
                )
            }
            Text(
                text = "Terms of Use",
                style = body2,
                color = gray,
                textDecoration = TextDecoration.Underline
            )
            for (word in keywordPost) {
                Text(
                    text = word,
                    style = body2,
                    color = gray,
                )
            }
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun BottomText() {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.Center
        ) {
            Text(text = " to Our ",
                style = body2,
                color = gray
            )
            Text(text = "Privacy Policy.",
                style = body2,
                color = gray,
                textDecoration = TextDecoration.Underline
            )
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun HintWithUnderline() {
        Column(
            modifier = Modifier.paddingFromBaseline(top = 24.dp, bottom = 16.dp)
        ){
            TopText()
            BottomText()
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun LoginInputBox() {
        Column {
            OutlinedTextField(
                value = "",
                onValueChange = {},
                modifier = Modifier
                    .fillMaxWidth()
                    .height(56.dp)
                    .clip(small),
                placeholder = {
                    Text(
                        text = "Email address",
                        style = body1,
                        color = gray
                    )
                }
            )
            Spacer(modifier = Modifier.height(8.dp))
            OutlinedTextField(
                value = "",
                onValueChange = {},
                modifier = Modifier
                    .fillMaxWidth()
                    .height(56.dp)
                    .clip(small),
                placeholder = {
                    Text(
                        text = "Password(8+ Characters)",
                        style = body1,
                        color = gray
                    )
                }
            )
        }
    }
    
    @Preview(showBackground = true, showSystemUi = false)
    @Composable
    fun LoginTitle() {
        Text(
            text = "Log in with email",
            modifier = Modifier
                .fillMaxWidth()
                .paddingFromBaseline(top = 184.dp, bottom = 16.dp),
            style = h1,
            color = gray,
            textAlign = TextAlign.Center
        )
    }
    
    @Preview(showBackground = true)
    @Composable
    fun LoginButton() {
        Button(
            onClick = { },
            modifier = Modifier
                .height(48.dp)
                .fillMaxWidth()
                .clip(medium),
            colors = ButtonDefaults.buttonColors(backgroundColor = pink900)
        ) {
            Text(
                text = "Log in",
                style = button,
                color = white
            )
        }
    }
    
    @Composable
    fun LoginPage() {
        Column(
            Modifier
                .fillMaxSize()
                .background(white)
                .padding(horizontal = 16.dp)
        ) {
            LoginTitle()
            LoginInputBox()
            HintWithUnderline()
            LoginButton()
        }
    }
    
    @Composable
    @Preview
    fun LoginPageLightPreview() {
        BloomTheme() {
            LoginPage()
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155

    Home Page

    val small = RoundedCornerShape(4.dp)
    val medium = RoundedCornerShape(24.dp)
    
    val shapes = Shapes(
        small = RoundedCornerShape(4.dp),
        medium = RoundedCornerShape(4.dp),
        large = RoundedCornerShape(0.dp)
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    data class ImageItem(val name: String, val resId: Int)
    
    val bloomBannerList = listOf(
        ImageItem("Desert chic", R.drawable.desert_chic),
        ImageItem("Tiny terrariums", R.drawable.tiny_terrariums),
        ImageItem("Jungle Vibes", R.drawable.jungle_vibes)
    )
    
    val bloomInfoList = listOf(
        ImageItem("Monstera", R.drawable.monstera),
        ImageItem("Aglaonema", R.drawable.aglaonema),
        ImageItem("Peace lily", R.drawable.peace_lily),
        ImageItem("Fiddle leaf tree", R.drawable.fiddle_leaf),
        ImageItem("Desert chic", R.drawable.desert_chic),
        ImageItem("Tiny terrariums", R.drawable.tiny_terrariums),
        ImageItem("Jungle Vibes", R.drawable.jungle_vibes)
    )
    
    val navList = listOf(
        ImageItem("Home", R.drawable.ic_home),
        ImageItem("Favorites", R.drawable.ic_favorite_border),
        ImageItem("Profile", R.drawable.ic_account_circle),
        ImageItem("Cart", R.drawable.ic_shopping_cart)
    )
    
    @Preview(showBackground = true)
    @Composable
    fun BloomRowBanner() {
        Column {
            Box(
                Modifier.fillMaxWidth()
            ) {
                Text(
                    text = "Browse themes",
                    style = h1,
                    color = gray,
                    modifier = Modifier
                        .fillMaxWidth()
                        .paddingFromBaseline(top = 32.dp)
                )
            }
            Spacer(modifier = Modifier.height(16.dp))
            LazyRow(
                modifier = Modifier.height(136.dp)
            ) {
                items(bloomBannerList.size) {
                    if (it != 0) {
                        Spacer(modifier = Modifier.width(8.dp))
                    }
                    PlantCard(bloomBannerList[it])
                }
            }
        }
    }
    
    
    @Preview(showBackground = true)
    @Composable
    fun BloomInfoList() {
        Column {
            Row(
                Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = "Design your home garden",
                    style = h1,
                    color = gray,
                    modifier = Modifier.paddingFromBaseline(top = 40.dp)
                )
                Icon(
                    painterResource(id = R.drawable.ic_filter_list),
                    "filter",
                    modifier = Modifier
                        .padding(top = 24.dp)
                        .size(24.dp)
                )
            }
            Spacer(modifier = Modifier.height(16.dp))
            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth(),
                contentPadding = PaddingValues(bottom = 56.dp)
            ) {
                items(bloomInfoList.size) {
                    if (it != 0) {
                        Spacer(modifier = Modifier.height(8.dp))
                    }
                    DesignCard(bloomInfoList[it])
                }
            }
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun SearchBar() {
        Box {
            TextField(
                value = "",
                onValueChange = {},
                modifier = Modifier
                    .fillMaxWidth()
                    .height(56.dp)
                    .clip(RoundedCornerShape(4.dp))
                    .border(BorderStroke(1.dp, Color.Black)),
                leadingIcon = {
                    Icon(
                        painter = rememberVectorPainter(image = ImageVector.vectorResource(id = R.drawable.ic_search)),
                        contentDescription = "search",
                        modifier = Modifier.size(18.dp)
                    )
                },
                placeholder = {
                    Text(
                        text = "Search",
                        style = body1,
                        color = gray
                    )
                },
                colors = TextFieldDefaults.outlinedTextFieldColors(
                    backgroundColor = white,
                    unfocusedBorderColor = white,
                    focusedBorderColor = white,
                ),
            )
        }
    }
    
    @Composable
    fun HomePage() {
        Scaffold(
            bottomBar = {
                BottomBar()
            }
        ) {
            Column(
                Modifier
                    .fillMaxSize()
                    .background(white)
                    .padding(horizontal = 16.dp)
            ) {
                Spacer(modifier = Modifier.height(40.dp))
                SearchBar()
                BloomRowBanner()
                BloomInfoList()
            }
        }
    }
    
    @Composable
    fun DesignCard(plant: ImageItem) {
        Row(
            modifier = Modifier.fillMaxWidth()
        ) {
            Image(
                painterResource(id = plant.resId),
                contentScale = ContentScale.Crop,
                contentDescription = "image",
                modifier = Modifier
                    .size(64.dp)
                    .clip(RoundedCornerShape(4.dp))
            )
            Spacer(modifier = Modifier.width(16.dp))
            Column {
                Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
                    Column {
                        Text(
                            text = plant.name,
                            style = h2,
                            color = gray,
                            modifier = Modifier.paddingFromBaseline(top = 24.dp)
                        )
                        Text(
                            text = "This is a description",
                            style = body1,
                            color = gray,
                            modifier = Modifier
                        )
                    }
                    Checkbox(
                        modifier = Modifier
                            .padding(top = 24.dp)
                            .size(24.dp),
                        checked = false,
                        onCheckedChange = {
                            // plant.enable = it
                        },
                        colors = CheckboxDefaults.colors(
                            checkmarkColor = white
                        )
                    )
                }
                Divider(color = gray, modifier = Modifier.padding(top = 16.dp), thickness = 0.5.dp)
            }
        }
    }
    
    
    @Composable
    fun PlantCard(plant: ImageItem) {
        Card(
            modifier = Modifier
                .size(136.dp)
                .clip(RoundedCornerShape(4.dp))
        ) {
            Column {
                Image(
                    painterResource(id = plant.resId),
                    contentScale = ContentScale.Crop,
                    contentDescription = "image",
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(96.dp)
                )
                Box(
                    Modifier
                        .fillMaxWidth()
                        .padding(start = 16.dp)
                ) {
                    Text(
                        text = plant.name,
                        style = h2,
                        color = gray,
                        modifier = Modifier
                            .fillMaxWidth()
                            .paddingFromBaseline(top = 24.dp, bottom = 16.dp)
                    )
                }
            }
        }
    }
    
    @Composable
    fun BottomBar() {
        BottomNavigation(
            elevation = 16.dp,
            modifier = Modifier
                .fillMaxWidth()
                .height(56.dp)
                .background(pink100)
        ) {
            navList.forEach {
                BottomNavigationItem(
                    onClick = {},
                    icon = {
                        Icon(
                            painterResource(id = it.resId),
                            contentDescription = null,
                            modifier = Modifier.size(24.dp),
                        )
                    },
                    label = {
                        Text(
                            it.name,
                            style = caption,
                            color = gray,
                        )
                    },
                    selected = ("Home" == it.name)
                )
            }
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun BottomBarPreview() {
        BloomTheme() {
            BottomBar()
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun DesignCardPreview() {
        BloomTheme() {
            DesignCard(bloomInfoList[0])
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun PlantCardPreview() {
        BloomTheme() {
            PlantCard(bloomBannerList[0])
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun HomePageLightPreview() {
        BloomTheme() {
            HomePage()
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296

    主题

    private val BloomLightColorPaltte = lightColors(
        primary = pink100,
        secondary = pink900,
        background = white,
        surface = white850,
        onPrimary = gray,
        onSecondary = white,
        onBackground = gray,
        onSurface = gray,
    )
    
    private val BloomDarkColorPaltte = darkColors(
        primary = green900,
        secondary = green300,
        background = gray,
        surface = white150,
        onPrimary = white,
        onSecondary = gray,
        onBackground = white,
        onSurface = white850
    )
    
    open class WelcomeAssets private constructor(
        var background: Int,
        var illos: Int,
        var logo: Int
    ) {
        object LightWelcomeAssets : WelcomeAssets(
            background = R.drawable.ic_light_welcome_bg,
            illos = R.drawable.ic_light_welcome_illos,
            logo = R.drawable.ic_light_logo
        )
    
        object DarkWelcomeAssets : WelcomeAssets(
            background = R.drawable.ic_dark_welcome_bg,
            illos = R.drawable.ic_dark_welcome_illos,
            logo = R.drawable.ic_dark_logo
        )
    }
    
    internal var LocalWelcomeAssets = staticCompositionLocalOf { WelcomeAssets.LightWelcomeAssets as WelcomeAssets }
    
    val MaterialTheme.welcomeAssets
        @Composable
        @ReadOnlyComposable
        get() = LocalWelcomeAssets.current
    
    enum class BloomTheme {
        LIGHT, DARK
    }
    
    
    @Composable
    fun BloomTheme(theme: BloomTheme = BloomTheme.LIGHT, content: @Composable() () -> Unit) {
        CompositionLocalProvider(
            LocalWelcomeAssets provides if (theme == BloomTheme.DARK) WelcomeAssets.DarkWelcomeAssets else WelcomeAssets.LightWelcomeAssets,
        ) {
            MaterialTheme(
                colors = if (theme == BloomTheme.DARK) BloomDarkColorPaltte else BloomLightColorPaltte,
                typography = bloomTypoGraphy,
                shapes = shapes,
                content = content
            )
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    CompositionLocal

    CompositionLocal 是 Jetpack Compose 中的一种数据传递方式。它可以在组合组件之间传递可变数据,而无需通过 props 或 state 管理器来传递数据。这个特性比传统的数据传递方式更为高效和方便。
    例如,我们可以通过 CompositionLocal 在应用程序的不同部分中传递数据,如主题、语言环境、字体等。这个特性可以让我们在组合应用程序中更轻松地使用全局状态,而不必每次都传递数据。
    此外,CompositionLocal 还可以用于实现本地化和自定义主题等功能的高效性。因此,它是 Jetpack Compose 中非常重要的一个组成部分。

    • 如果CompositonLocal提供得知发生更改的可能性很小或永远无法改变,利用staticCompositionLocal能显著提升性能

    总结

    • 所有视图组件建议用顶级函数声明

    随笔:

    • 简历 遇到什么问题,用了什么方法,如何进行优化

    《Jetpack Compose从入门到实战》第一章 全新的 Android UI 框架

    《Jetpack Compose从入门到实战》 第二章 了解常用UI组件

    《Jetpack Compose从入门到实战》第三章 定制 UI 视图

    《Jetpack Compose从入门到实战》第八章 Compose页面 导航

    《Jetpack Compose从入门到实战》第九章 Accompanist 与第三方组件库

  • 相关阅读:
    线性代数中涉及到的matlab命令-第三章:矩阵的初等变换及线性方程组
    深入浅出理解 AI 生图模型
    3. 查询处理
    C++类设计:一个比较复杂的日志类 支持多线程、文件切换、信息缓存(源码)
    论文复现|Panoptic Deeplab(全景分割PyTorch)
    C/C++ 文件读写
    c++八股文:c++新特性
    关于git flow的一点思考
    独辟蹊径:逆推Krpano切图算法,实现在浏览器切多层级瓦片图
    php怎么连接sql server
  • 原文地址:https://blog.csdn.net/qq_61735602/article/details/133463431