当前位置:网站首页>Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
2022-06-27 03:36:00 【安果移不动】
Kotlin Compose 隐式传参 CompositionLocalProvider_安果移不动的博客-CSDN博客
Kotlin Compose 自定义 CompositionLocalProvider CompositionLocal_安果移不动的博客-CSDN博客
学习完成上面两个博客知识后。
你会发现。
LocalContext.current指向的是

有时候也许会遇到
staticCompositionLocalOf
compositionLocalOf
两种不同的构建数值的Provider
区别就是
staticCompositionLocalOf 会针对所有的组件进行重绘
而
compositionLocalOf 仅仅会针对单个组件进行重绘


范例代码
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 场景"
staticCompositionLocalOf { Color.Black }
} else {
compositionLocalName = "compositionLocalOf 场景"
compositionLocalOf { Color.Black }
}
//重组标记 组件第一次加载时,recomposeFlog 为Init
//重组之前 也就是第二次加载之前 将recomposeFlog设置为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个 TaggedBox 组件 只有Middle 使用 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("修改颜色 观察组件是否重组")
}
}
}
}
@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()
}
}
}
边栏推荐
- Anaconda3 is missing a large number of files during and after installation, and there are no scripts and other directories
- The use and introduction of pytorch 23 hook and the implementation of plug and play dropblock based on hook
- 2021:Passage Retrieval for Outside-KnowledgeVisual Question Answering通道检索的外部知识视觉问答
- GAMES101作业7提高-微表面材质的实现过程
- Resnet152 pepper pest image recognition 1.0
- Human soberness: bottom logic and top cognition
- Promise source code class version [III. promise source code] [detailed code comments / complete test cases]
- Record the method of reading excel provided by unity and the solution to some pits encountered
- Getting started with Scala_ Immutable list and variable list
- LDR6028 手机设备一边充电一边OTG传输数据方案
猜你喜欢

2019LXMERT:Learning Cross-Modality Encoder Representations from Transformers

PAT甲级 1018 Public Bike Management

2021:passage retrieval for outside knowledgevisual question answering

Career outlook, money outlook and happiness outlook

Pat grade a 1020 tree Traversals

事业观、金钱观与幸福观

MySql的开发环境

JMeter takes the result of the previous request as the parameter of the next request

Pat grade a 1025 pat ranking

2021:Greedy Gradient Ensemble for Robust Visual Question Answering
随机推荐
Pat grade a 1019 general palindromic number
Window 加密壳实现
Mmdetection valueerror: need at least one array to concatenate solution
TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
How can e-commerce products be promoted and advertised on Zhihu?
语义化版本 2.0.0
Further exploration of handler (Part 2) (the most complete analysis of the core principles of handler)
2022年氯碱电解工艺试题及答案
PAT甲级 1026 Table Tennis
Agile development - self use
Mmdetection uses yolox to train its own coco data set
人间清醒:底层逻辑和顶层认知
Products change the world
Anaconda3安裝過程及安裝後缺失大量文件,沒有scripts等目錄
静态时序分析-OCV和time derate
清华&华为等 综述 | 语义通信:原则与挑战
Knowledge of iPhone certificate structure
Stack overflow vulnerability
苹果手机证书构体知识
ESP8266