Текущий код < /p>
@Composable
fun TextDemo() {
var selectedIndex by remember {
mutableIntStateOf(0)
}
Row {
TabText(
text = "First",
isSelected = selectedIndex == 0,
onClick = {
selectedIndex = 0
},
)
TabText(
text = "Second",
isSelected = selectedIndex == 1,
onClick = {
selectedIndex = 1
},
)
}
}
@Composable
fun TabText(
text: String,
isSelected: Boolean,
onClick: () -> Unit,
) {
val tabTextColor by animateColorAsState(
targetValue = if (isSelected) {
Color.Red
} else {
Color.Black
},
animationSpec = tween(
easing = LinearEasing,
),
label = "tab_text_color",
)
Text(
modifier = Modifier
.padding(8.dp)
.clickable {
onClick()
},
text = text,
color = tabTextColor,
)
}
ui для справки
два текста в ряд
recopposition. />
Вопрос
Как уменьшить отменение при изменении цвета текста? Как Alpha , Transition и т. Д., Можно избегать рекомпозиций при анимации с использованием модификатора.graphicslayer {}
Тот же код с Alpha анимация вместо цвета изменяется только один раз на Selection. src = "https://i.sstatic.net/wjxo3.png"/>
code
@Composable
fun TabText(
text: String,
isSelected: Boolean,
onClick: () -> Unit,
) {
val alphaValue by animateFloatAsState(
targetValue = if (isSelected) {
1F
} else {
0.5F
},
animationSpec = tween(
easing = LinearEasing,
),
label = "tab_text_color",
)
Text(
modifier = Modifier
.graphicsLayer {
alpha = alphaValue
}
.padding(8.dp)
.clickable {
onClick()
},
text = text,
)
}
Подробнее здесь: https://stackoverflow.com/questions/772 ... text-color