当前位置:网站首页>Kotlin compose compositionlocalof and staticcompositionlocalof
Kotlin compose compositionlocalof and staticcompositionlocalof
2022-06-27 03:48:00 【Ango cannot move】
Kotlin Compose Implicit parameters CompositionLocalProvider_ Ango Mobile Blog -CSDN Blog
Kotlin Compose Customize CompositionLocalProvider CompositionLocal_ Ango Mobile Blog -CSDN Blog
After learning the above two blog knowledge .
You'll find that .
LocalContext.currentPointing to

Sometimes you may encounter
staticCompositionLocalOf
compositionLocalOf
Two different constructs of numerical Provider
The difference is
staticCompositionLocalOf All components will be redrawn
and
compositionLocalOf Redrawing will only be done for a single component


Example code
package com.anguomob.jecpack.activity.compose.composition.local
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
var isStatic = true
var compositionLocalName = ""
val currentLocalColor = if (isStatic) {
compositionLocalName = "StaticCompositionLocal scene "
staticCompositionLocalOf { Color.Black }
} else {
compositionLocalName = "compositionLocalOf scene "
compositionLocalOf { Color.Black }
}
// Recombinant marker When the component is loaded for the first time ,recomposeFlog by Init
// Before restructuring That is, before the second load take recomposeFlog Set to Recompose
var recomposeFlog = "Init"
@Composable
fun CompositionSample3() {
LocalContext.current
val (color, setColor) = remember {
mutableStateOf(Color.Green)
}
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text(" ${compositionLocalName}")
Spacer(modifier = Modifier.padding(20.dp))
//3 individual TaggedBox Components Only Middle Use CompositionLocal
CompositionLocalProvider(currentLocalColor provides color) {
TaggedBox(tag = "Wrapper $recomposeFlog", size = 400.dp, background = Color.Red) {
TaggedBox(
tag = "Middle $recomposeFlog",
size = 300.dp,
background = currentLocalColor.current
) {
TaggedBox(tag = "Inner $recomposeFlog", size = 200.dp, background = Color.Yellow) {
}
}
}
}
Spacer(modifier = Modifier.padding(20.dp))
Button(onClick = {
setColor(Color.Blue)
recomposeFlog = "Recompose"
}) {
Text(" Change the color Observe whether the components are reassembled ")
}
}
}
}
@Composable
fun TaggedBox(tag: String, size: Dp, background: Color, contnet: @Composable () -> Unit = {}) {
Column(
modifier = Modifier
.size(size)
.background(background),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = tag)
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
contnet()
}
}
}
边栏推荐
- Pat grade a 1019 general palindromic number
- iOS开发:对于动态库共享缓存(dyld)的了解
- Resnet152 pepper pest image recognition 1.0
- 如何系统学习LabVIEW?
- Questions and answers of chlor alkali electrolysis process in 2022
- Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
- 2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering
- PAT甲级 1019 General Palindromic Number
- Is the money invested in financial products guaranteed? Is there no more?
- Is the truth XX coming? Why are test / development programmers unwilling to work overtime? This is a crazy state
猜你喜欢

mysql数据库基础:DQL数据查询语言

IOS development: understanding of dynamic library shared cache (dyld)

PAT甲级 1020 Tree Traversals

元透实盘周记20220627

fplan-Powerplan实例

PAT甲级 1025 PAT Ranking

Resnet152 pepper pest image recognition 1.0

2019LXMERT:Learning Cross-Modality Encoder Representations from Transformers

2021:Passage Retrieval for Outside-KnowledgeVisual Question Answering通道检索的外部知识视觉问答

PAT甲级 1026 Table Tennis
随机推荐
Pat grade a 1020 tree Traversals
手机新领域用法知识
Overview of Tsinghua & Huawei | semantic communication: Principles and challenges
Pat grade a 1019 general palindromic number
乐得瑞LDR6035 USB-C接口设备支持可充电可OTG传输数据方案。
NestJS环境变量配置,解决如何在拦截器(interceptor)注入服务(service)的问题
Logarithm
Questions and answers of chlor alkali electrolysis process in 2022
733. 图像渲染
Description of replacement with STM32 or gd32
Ldr6028 OTG data transmission scheme for mobile devices while charging
解码苹果手机证书文件方法
2022 operation of simulated examination platform for tea artist (Senior) work license question bank
USB DRIVER
Is the money invested in financial products guaranteed? Is there no more?
Window 加密壳实现
Fplan layout
Servlet and JSP final review examination site sorting 42 questions and 42 answers
Record the method of reading excel provided by unity and the solution to some pits encountered
Nestjs environment variable configuration to solve the problem of how to inject services into interceptors