Skip to content

Commit 23d2ab2

Browse files
committed
Unique key snippet
1 parent 4b60d08 commit 23d2ab2

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/BasicSharedElementSnippets.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package com.example.compose.snippets.animations.sharedelement
2020

2121
import androidx.compose.animation.AnimatedContent
22+
import androidx.compose.animation.AnimatedVisibility
2223
import androidx.compose.animation.AnimatedVisibilityScope
2324
import androidx.compose.animation.ExperimentalSharedTransitionApi
2425
import androidx.compose.animation.SharedTransitionLayout
@@ -38,6 +39,7 @@ import androidx.compose.foundation.shape.CircleShape
3839
import androidx.compose.foundation.shape.RoundedCornerShape
3940
import androidx.compose.material3.Text
4041
import androidx.compose.runtime.Composable
42+
import androidx.compose.runtime.compositionLocalOf
4143
import androidx.compose.runtime.getValue
4244
import androidx.compose.runtime.mutableStateOf
4345
import androidx.compose.runtime.remember
@@ -395,3 +397,44 @@ private fun UnmatchedBoundsExample() {
395397
}
396398
// [END android_compose_animation_shared_element_bounds_unmatched]
397399
}
400+
401+
private object UniqueKeySnippet {
402+
// [START android_compose_shared_elements_unique_key]
403+
data class SnackSharedElementKey(
404+
val snackId: Long,
405+
val origin: String,
406+
val type: SnackSharedElementType
407+
)
408+
409+
enum class SnackSharedElementType {
410+
Bounds,
411+
Image,
412+
Title,
413+
Tagline,
414+
Background
415+
}
416+
417+
@Composable
418+
fun SharedElementUniqueKey() {
419+
// [START_EXCLUDE]
420+
SharedTransitionLayout {
421+
AnimatedVisibility(visible = true) {
422+
// [END_EXCLUDE]
423+
Box(modifier = Modifier
424+
.sharedElement(
425+
rememberSharedContentState(
426+
key = SnackSharedElementKey(
427+
snackId = 1,
428+
origin = "latest",
429+
type = SnackSharedElementType.Image
430+
)
431+
),
432+
animatedVisibilityScope = this@AnimatedVisibility
433+
))
434+
// [START_EXCLUDE]
435+
}
436+
}
437+
// [END_EXCLUDE]
438+
}
439+
// [END android_compose_shared_elements_unique_key]
440+
}

0 commit comments

Comments
 (0)