Repository: android/compose-samples
File: Jetsnack/app/src/main/java/com/example/jetsnack/ui/home/cart/Cart.kt (Kotlin)

Code before:
                            Modifier
                                .height(size * 1.8f)
                                .width(size * 1.8f)
                        }
                        val shape = if (offsetX >= -300) CircleShape else RoundedCornerShape(30.dp)
                        Box(
                            modifier = Modifier
                                .clip(shape)
                                .background(JetsnackTheme.colors.error)
                                .then(sizeModifier)
                        ) {
                            Row(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .fillMaxHeight(),
                                horizontalArrangement = Arrangement.Center,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                if (offsetX < -150f && offsetX > -450) {
                                    val iconAlpha = if (offsetX < -250) 0.5f else 1f
                                    Icon(
                                        imageVector = Icons.Filled.DeleteForever,
                                        modifier = Modifier
                                            .width(16.dp)
                                            .height(18.dp),
                                        tint = JetsnackTheme.colors.uiBackground
                                            .copy(alpha = iconAlpha),
                                        contentDescription = null,
                                    )
                                }
                            }
                            Row(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .fillMaxHeight(),
                                horizontalArrangement = Arrangement.Center,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                val textAlpha = if (offsetX > -450) 0.5f else 1f
                                if (offsetX < -415f) {
                                    Text(
                                        text = stringResource(id = R.string.remove_item),
                                        style = MaterialTheme.typography.subtitle1,
                                        color = JetsnackTheme.colors.uiBackground
                                            .copy(alpha = textAlpha),
                                        textAlign = TextAlign.Center,
                                        modifier = Modifier.padding(15.dp)
                                    )
                                }
                            }
                        }

Review comment (suggestion):
Again, use `Modifier.alpha` (which uses `Modifier.graphicsLayer`) on the `Icon` to change the color and only require re-draw.

Code after:
                                    .fillMaxWidth()
                                    .height(height)
                                    .align(Alignment.Center),
                                shape = CircleShape,
                                color = JetsnackTheme.colors.error
                            ) {
                                Box(
                                    modifier = Modifier.fillMaxSize(),
                                    contentAlignment = Alignment.Center
                                ) {
                                    // Icon must be visible while in this width range
                                    if (offsetX < -40.dp && offsetX > -152.dp) {
                                        // Icon alpha decreases as it is about to disappear
                                        val iconAlpha: Float by animateFloatAsState(
                                            if (offsetX < -120.dp) 0.5f else 1f
                                        )

                                        Icon(
                                            imageVector = Icons.Filled.DeleteForever,
                                            modifier = Modifier
                                                .size(16.dp)
                                                .graphicsLayer(alpha = iconAlpha),
                                            tint = JetsnackTheme.colors.uiBackground,
                                            contentDescription = null,
                                        )
                                    }
                                    /*Text opacity increases as the text is supposed to appear in
                                    the screen*/
                                    val textAlpha by animateFloatAsState(
                                        if (offsetX > -144.dp) 0.5f else 1f
                                    )
                                    if (offsetX < -120.dp) {
                                        Text(
                                            text = stringResource(id = R.string.remove_item),
                                            style = MaterialTheme.typography.subtitle1,
                                            color = JetsnackTheme.colors.uiBackground,
                                            textAlign = TextAlign.Center,
                                            modifier = Modifier
                                                .graphicsLayer(
                                                    alpha = textAlpha
                                                )
                                        )
                                    }
                                }
                            }
                        }
                    }
                },
            ) {
                CartItem(
                    orderLine = orderLine,