I am trying to make a new app with Jetpack Compose and in this app will be a LazyRow with different items in a Box. With every item that is fully visible the background of the box should be changed from the color saved in the data model.
This is the code:
fun MainScreen(navHostController: NavHostController) {
var bgColor: Color = Color.Red
val state = rememberLazyListState()
val fullyVisibleIndices: List<Int> by remember {
derivedStateOf {
val layoutInfo = state.layoutInfo
val visibleItemsInfo = layoutInfo.visibleItemsInfo
if (visibleItemsInfo.isEmpty()) {
emptyList()
} else {
val fullyVisibleItemsInfo = visibleItemsInfo.toMutableList()
val lastItem = fullyVisibleItemsInfo.last()
val viewportHeight = layoutInfo.viewportEndOffset + layoutInfo.viewportStartOffset
if (lastItem.offset + lastItem.size > viewportHeight) {
fullyVisibleItemsInfo.removeLast()
}
val firstItemIfLeft = fullyVisibleItemsInfo.firstOrNull()
if (firstItemIfLeft != null && firstItemIfLeft.offset < layoutInfo.viewportStartOffset) {
fullyVisibleItemsInfo.removeFirst()
}
fullyVisibleItemsInfo.map { it.index }
}
}
}
addGrunges()
Box(
modifier = Modifier
.fillMaxSize()
.background(bgColor)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(top = 100.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Text(
text = "Devlet seçin", style = MaterialTheme.typography.h3
)
LazyRow(
state = state,
modifier = Modifier
.fillMaxWidth()
.padding(top = 100.dp, bottom = 250.dp)
) {
itemsIndexed(grungesList) { index, items ->
bgColor = if (fullyVisibleIndices.contains(index)) items.color else Color.Red
ListColumn(model = items)
}
}
}
}
}
Thanks for your help!