如何处理在Jetpack Compose中显示和隐藏自定义可重用的快讯栏?

huangapple go评论91阅读模式
英文:

How to handle displaying and hiding a custom reusable snack bar in jetpack compose?

问题

我有一个可组合的 snackbar。以下是代码:

  1. @Composable
  2. fun CealSnackbar(title: String, performAction: () -> Unit) {
  3. val counterState = remember { mutableStateOf(true) }
  4. LaunchedEffect(Unit) {
  5. while(true) {
  6. delay(4000)
  7. counterState.value = false
  8. performAction()
  9. }
  10. }
  11. AnimatedVisibility(
  12. visible = counterState.value,
  13. enter = slideInVertically() + fadeIn(),
  14. exit = slideOutVertically() + fadeOut(),
  15. ) {
  16. Card(
  17. modifier = Modifier
  18. .absoluteOffset(x = 0.dp, y = 10.dp)
  19. .padding(horizontal = 20.dp)
  20. .fillMaxWidth()
  21. .statusBarsPadding()
  22. .background(
  23. color = MaterialTheme.colors.primaryVariant,
  24. shape = RoundedCornerShape(10.dp)
  25. ), elevation = 20.dp
  26. ) {
  27. Row(
  28. modifier = Modifier
  29. .background(color = MaterialTheme.colors.primaryVariant)
  30. .padding(12.dp),
  31. horizontalArrangement = Arrangement.Start,
  32. verticalAlignment = Alignment.CenterVertically
  33. ) {
  34. Image(
  35. painter = painterResource(id = R.drawable.error_circle),
  36. contentDescription = title
  37. )
  38. Text(
  39. text = title,
  40. fontFamily = FontFamily(Font(R.font.inter_medium)),
  41. fontSize = 12.sp,
  42. color = MaterialTheme.colors.primary,
  43. modifier = Modifier.padding(horizontal = 10.dp)
  44. )
  45. }
  46. }
  47. }
  48. }

我想要 snackbar 出现和消失时有动画效果,并且它应该可以在任何组件中使用。

因此,当我在其他可组合中使用它时,我尝试在出现错误消息时显示它,错误消息是一个状态流:

  1. if(errorMsg.isNotEmpty()) {
  2. CealSnackbar(
  3. title = errorMsg, performAction = {
  4. registrationViewModel.setErrorMsg("")
  5. }
  6. )
  7. }

如果我像上面这样使用它,动画不会发生,我应该如何重构它,以便在出现错误消息时可以在任何可组合中使用它。

英文:

I have a composable of snackbar. Below is the code

  1. @Composable
  2. fun CealSnackbar(title: String,performAction: () -> Unit,) {
  3. val counterState = remember { mutableStateOf(true) }
  4. LaunchedEffect(Unit) {
  5. while(true) {
  6. delay(4000)
  7. counterState.value = false
  8. performAction()
  9. }
  10. }
  11. AnimatedVisibility(
  12. visible = counterState.value,
  13. enter = slideInVertically() + fadeIn(),
  14. exit = slideOutVertically() + fadeOut(),
  15. ) {
  16. Card(
  17. modifier = Modifier
  18. .absoluteOffset(x = 0.dp, y = 10.dp)
  19. .padding(horizontal = 20.dp)
  20. .fillMaxWidth()
  21. .statusBarsPadding()
  22. .background(
  23. color = MaterialTheme.colors.primaryVariant,
  24. shape = RoundedCornerShape(10.dp)
  25. ), elevation = 20.dp
  26. ) {
  27. Row(
  28. modifier = Modifier
  29. .background(color = MaterialTheme.colors.primaryVariant)
  30. .padding(12.dp),
  31. horizontalArrangement = Arrangement.Start,
  32. verticalAlignment = Alignment.CenterVertically
  33. ) {
  34. Image(
  35. painter = painterResource(id = R.drawable.error_circle),
  36. contentDescription = title
  37. )
  38. Text(
  39. text = title,
  40. fontFamily = FontFamily(Font(R.font.inter_medium)),
  41. fontSize = 12.sp,
  42. color = MaterialTheme.colors.primary,
  43. modifier = Modifier.padding(horizontal = 10.dp)
  44. )
  45. }
  46. }
  47. }
  48. }

I want the animation when the snackbar appears and disappears and it should be usable in any component

So while using this in other composable I am trying to display it when there is an error msg, the error msg is a state flow

  1. if(errorMsg.isNotEmpty()){
  2. CealSnackbar(
  3. title = errorMsg, performAction = {
  4. registrationViewModel.setErrorMsg("")
  5. }
  6. )
  7. }

If I use it like above the animation does not take place, how should I refactor it so that I use it any composable when there is an error msg

答案1

得分: 1

使用 SnackbarHost

  1. val snackState = remember { SnackbarHostState() }
  2. val coroutineScope = rememberCoroutineScope()
  3. SnackbarHost(
  4. hostState = snackState
  5. ) {
  6. CealSnackbar(
  7. title = errorMsg, performAction = {
  8. registrationViewModel.setErrorMsg("")
  9. }
  10. )
  11. }
英文:

Use SnackbarHost

  1. val snackState = remember { SnackbarHostState() }
  2. val coroutineScope = rememberCoroutineScope()
  3. SnackbarHost(
  4. hostState = snackState
  5. ){
  6. CealSnackbar(
  7. title = errorMsg, performAction = {
  8. registrationViewModel.setErrorMsg("")
  9. }
  10. )
  11. }

huangapple
  • 本文由 发表于 2023年2月18日 13:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定