Skip to content

Instantly share code, notes, and snippets.

@skydoves
Created April 19, 2024 08:54
Show Gist options
  • Save skydoves/6b5b07adfee6f186de28b3f247ca945d to your computer and use it in GitHub Desktop.
Save skydoves/6b5b07adfee6f186de28b3f247ca945d to your computer and use it in GitHub Desktop.
shared_bounds_compose
@Composable
fun NavigationComposeShared() {
SharedTransitionLayout {
val pokemons = remember {
listOf(
Pokemon("Pokemon1", R.drawable.pokemon_preview),
Pokemon("Pokemon2", R.drawable.pokemon_preview),
Pokemon("Pokemon3", R.drawable.pokemon_preview),
Pokemon("Pokemon4", R.drawable.pokemon_preview)
)
}
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "home") {
composable("home") {
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
itemsIndexed(pokemons) { index, item ->
Row(
modifier = Modifier.clickable {
navController.navigate("details/$index")
}
.sharedBounds(
rememberSharedContentState(key = "pokemon-$index"),
animatedVisibilityScope = this@composable,
)
.fillMaxWidth()
) {
Image(
painter = painterResource(id = item.image),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier
.padding(horizontal = 20.dp)
.size(100.dp)
)
Text(
text = item.name,
fontSize = 18.sp,
modifier = Modifier
.align(Alignment.CenterVertically)
)
}
}
}
}
composable(
"details/{pokemon}",
arguments = listOf(navArgument("pokemon") { type = NavType.IntType })
) { backStackEntry ->
val pokemonId = backStackEntry.arguments?.getInt("pokemon")
val pokemon = pokemons[pokemonId!!]
Column(
Modifier
.fillMaxWidth()
.clickable {
navController.navigate("home")
}
.sharedBounds(
rememberSharedContentState(key = "pokemon-$pokemonId"),
animatedVisibilityScope = this@composable,
)
) {
Image(
painterResource(id = pokemon.image),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier
.aspectRatio(1f)
.fillMaxWidth()
)
Text(
pokemon.name, fontSize = 18.sp, modifier =
Modifier
.fillMaxWidth()
)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment