How transition animations work with TransformItemView
mainTo achieve a smooth transition (similar to WeChat Moments) where the image expands from its original position, use TransformItemView in your list and PreviewerState.enterTransform(index) to open it.
Critical Requirement: The key provided to TransformItemView must match the key returned by the getKey lambda in rememberPreviewerState.
If you have the same key appearing in multiple places on the same screen, you must provide a unique ItemStateMap to each PreviewerState via transformItemStateMap and wrap the corresponding UI in a CompositionLocalProvider(LocalTransformItemStateMap provides ...) to avoid incorrect animation positioning.
// Setup state with key mapping
val state = rememberPreviewerState(
pageCount = { images.size },
getKey = { index -> images[index].first } // Returns the unique key
)
// In your list/grid
images.forEachIndexed { index, image ->
TransformItemView(
key = image.first, // Must match getKey result
transformState = state,
itemState = rememberTransformItemState(intrinsicSize = painter.intrinsicSize),
// ...
) { /* content */ }
}
// Trigger transition
scope.launch { state.enterTransform(index) }