Created
April 26, 2024 02:56
-
-
Save fvilarino/653beed1eabf824a510329014a0137c3 to your computer and use it in GitHub Desktop.
Container Transform - Animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun MorphingFab(modifier: Modifier = Modifier) { | |
val users = remember { mutableStateListOf<User>() } | |
// 1 | |
var showDialog by remember { mutableStateOf(false) } | |
// 2 | |
SharedTransitionLayout( | |
modifier = Modifier | |
.fillMaxSize(), | |
) { | |
Box(modifier = modifier) { | |
UserList( | |
users = users, | |
modifier = Modifier.fillMaxSize(), | |
) | |
// 3 | |
AnimatedContent( | |
targetState = showDialog, | |
modifier = modifier, | |
label = "morphing_fab", | |
) { addingUser -> | |
// 4 | |
if (addingUser) { | |
Box { | |
// 5 | |
InputBox( | |
onAddUser = { | |
users.add(it) | |
showDialog = false | |
}, | |
// 6 | |
onCancel = { showDialog = false }, | |
modifier = Modifier | |
.align(Alignment.Center) | |
.width(320.dp) | |
.shadow(elevation = 4.dp) | |
// 7 | |
.sharedBounds( | |
sharedContentState = rememberSharedContentState(key = "fab"), | |
animatedVisibilityScope = this@AnimatedContent, | |
), | |
) | |
} | |
} else { | |
// 8 | |
Box { | |
FloatingActionButton( | |
modifier = Modifier | |
.align(Alignment.BottomEnd) | |
.padding(all = 16.dp) | |
// 9 | |
.sharedBounds( | |
sharedContentState = rememberSharedContentState(key = "fab"), | |
animatedVisibilityScope = this@AnimatedContent, | |
), | |
onClick = { | |
// 10 | |
showDialog = true | |
} | |
) { | |
Icon( | |
imageVector = Icons.Default.Add, | |
contentDescription = "Add", | |
) | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment