Skip to content

Instantly share code, notes, and snippets.

@AndroidPoet
Created July 3, 2024 18:05
Show Gist options
  • Save AndroidPoet/d2ec9ab79ace3d5dde50085f147056ff to your computer and use it in GitHub Desktop.
Save AndroidPoet/d2ec9ab79ace3d5dde50085f147056ff to your computer and use it in GitHub Desktop.
[versions]
compose = "1.7.0-alpha01"
[libraries]
compose = { id = "org.jetbrains.compose", version.ref = "compose" }
[plugins]
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
compose = { id = "org.jetbrains.compose", version.ref = "compose" }
@Composable
fun ListToDetailsDemo(modifier: Modifier = Modifier) {
var state: Screen by remember {
mutableStateOf(Screen.List)
}
val images = listOf(
R.drawable.flower,
R.drawable.storm,
R.drawable.tree,
)
SharedTransitionLayout(modifier = modifier.fillMaxSize()) {
AnimatedContent(
state,
label = "",
contentKey = { it.javaClass },
transitionSpec = {
if (initialState == Screen.List) {
slideInHorizontally { -it } + fadeIn() togetherWith slideOutHorizontally { it } + fadeOut()
} else {
slideInHorizontally { it } + fadeIn() togetherWith slideOutHorizontally { -it } + fadeOut()
}
},
) {
when (it) {
Screen.List -> {
LazyColumn {
items(50) { item ->
Row(
modifier = Modifier
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null,
) {
state = Screen.Details(item)
}
.fillMaxWidth(),
) {
Image(
painter = painterResource(images[item % 3]),
modifier = Modifier
.size(100.dp)
.then(
Modifier.sharedElement(
rememberSharedContentState(
key = "item-image$item",
),
this@AnimatedContent,
)
),
contentScale = ContentScale.Crop,
contentDescription = null,
)
Spacer(Modifier.size(15.dp))
Text("Item $item")
}
}
}
}
is Screen.Details -> {
val item = it.item
Column(
modifier = Modifier
.fillMaxSize()
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null,
) {
state = Screen.List
},
) {
Image(
painter = painterResource(images[item % 3]),
modifier = Modifier
.then(
Modifier.sharedElement(
rememberSharedContentState(key = "item-image$item"),
this@AnimatedContent,
),
)
.fillMaxWidth(),
contentScale = ContentScale.Crop,
contentDescription = null,
)
Text(
"Item $item",
fontSize = 23.sp,
)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment