Satyam G
05/10/2022, 9:04 AMFilip Wiesner
05/10/2022, 9:09 AMSatyam G
05/10/2022, 9:10 AMRonald Wolvers
05/10/2022, 9:10 AMval snackbarVisibleState = remember {
mutableStateOf(false)
}
AnimatedVisibility(snackbarVisibleState.value) {
Snackbar() {
//Your snackbar content here...
}
LaunchedEffect(snackbarVisibleState) {
//Display the snackbar for 5 seconds.
delay(5000L)
snackbarVisibleState.value = false
}
}
Filip Wiesner
05/10/2022, 9:11 AMit is not giving the visualYou can tweak the
enter
and exit
transitions to achieve the animation you want.Satyam G
05/10/2022, 9:11 AMRonald Wolvers
05/10/2022, 9:12 AMAnimatedVisibility
and LaunchedEffect
. That's as snackbar-like as it gets if you ask me 🙂Satyam G
05/10/2022, 9:12 AMAnimatedVisibility(
visible = isVisible,
enter = slideInVertically() + fadeIn(),
exit = slideOutVertically() + fadeOut()
) {
Card(
elevation = 6.dp,
modifier = Modifier
.padding(16.dp)
.defaultMinSize(minHeight = 48.dp),
backgroundColor = Theme.colors.red,
shape = Theme.roundShapes.large
) {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Row(
modifier = Modifier
.weight(8f)
.padding(8.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
icon?.let {
Icon(
imageVector = icon,
tint = Theme.colors.onRed,
contentDescription = "",
)
}
Text(
text = message,
color = Theme.colors.onRed,
style = Theme.typography.bodyMedium
)
}
if (action != null) {
TextButton(
modifier = Modifier
.weight(2f),
onClick = action,
text = actionLabel,
containerColor = Theme.colors.red,
contentColor = Theme.colors.onRed,
)
}
}
}
}
Filip Wiesner
05/10/2022, 9:14 AMI am not using Snackbar . Just a Normal CardIf you look at the
Snackbar
implementation it is really simmilar to a Card
. Both use Surface
so there is really no difference.Satyam G
05/10/2022, 9:16 AMFilip Wiesner
05/10/2022, 9:18 AMisVisible
boolean?Satyam G
05/10/2022, 9:20 AM@Composable
fun Error(
actionLabel: String = stringResource(id = R.string.lbl_reload),
icon: ImageVector? = <http://Icons.Outlined.Info|Icons.Outlined.Info>,
message: String = stringResource(id = R.string.lbl_error),
action: (() -> Unit)? = null,
isVisible: Boolean = true
) {
Box(modifier = Modifier.fillMaxSize(), Alignment.BottomCenter) {
AnimatedVisibility(
visible = isVisible,
enter = slideInVertically() + fadeIn(),
exit = slideOutVertically() + fadeOut()
) {
Card(
elevation = 6.dp,
modifier = Modifier
.padding(16.dp)
.defaultMinSize(minHeight = 48.dp),
backgroundColor = ApnaTheme.colors.red,
shape = ApnaTheme.roundShapes.large
) {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Row(
modifier = Modifier
.weight(8f)
.padding(8.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
icon?.let {
Icon(
imageVector = icon,
tint = Theme.colors.onRed,
contentDescription = "",
)
}
Text(
text = message,
color = Theme.colors.onRed,
style = Theme.typography.bodyMedium
)
}
if (action != null) {
TextButton(
modifier = Modifier
.weight(2f),
onClick = action,
text = actionLabel,
containerColor = ApnaTheme.colors.red,
contentColor = ApnaTheme.colors.onRed,
)
}
}
}
}
}
}
var error by remember { mutableStateOf(false) }
if(error){
Error(isVisible = error, action = {
error = false
})
}
Filip Wiesner
05/10/2022, 9:24 AMtrue
to false
you just remove the whole AnimatedVisibility
composable. How do you expect it to animate if you remove it as soon as you change the visibility boolean.Satyam G
05/10/2022, 9:25 AMFilip Wiesner
05/10/2022, 9:26 AMAnimatedVisibility
expects that you change the visible
parameter. But you just remove the whole composable and create it again when there is error.Satyam G
05/10/2022, 9:28 AMFilip Wiesner
05/10/2022, 9:29 AMSatyam G
05/10/2022, 9:30 AMFilip Wiesner
05/10/2022, 10:04 AMSatyam G
05/10/2022, 10:05 AM