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

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

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

问题

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

@Composable
fun CealSnackbar(title: String, performAction: () -> Unit) {

    val counterState = remember { mutableStateOf(true) }

    LaunchedEffect(Unit) {
        while(true) {
            delay(4000)
            counterState.value = false
            performAction()
        }
    }

    AnimatedVisibility(
        visible = counterState.value,
        enter = slideInVertically() + fadeIn(),
        exit = slideOutVertically() + fadeOut(),
    ) {
        Card(
            modifier = Modifier
                .absoluteOffset(x = 0.dp, y = 10.dp)
                .padding(horizontal = 20.dp)
                .fillMaxWidth()
                .statusBarsPadding()
                .background(
                    color = MaterialTheme.colors.primaryVariant,
                    shape = RoundedCornerShape(10.dp)
                ), elevation = 20.dp
        ) {
            Row(
                modifier = Modifier
                    .background(color = MaterialTheme.colors.primaryVariant)
                    .padding(12.dp),
                horizontalArrangement = Arrangement.Start,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Image(
                    painter = painterResource(id = R.drawable.error_circle),
                    contentDescription = title
                )
                Text(
                    text = title,
                    fontFamily = FontFamily(Font(R.font.inter_medium)),
                    fontSize = 12.sp,
                    color = MaterialTheme.colors.primary,
                    modifier = Modifier.padding(horizontal = 10.dp)
                )
            }
        }
    }
}

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

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

if(errorMsg.isNotEmpty()) {
    CealSnackbar(
        title = errorMsg, performAction = {
            registrationViewModel.setErrorMsg("")
        }
    )
}

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

英文:

I have a composable of snackbar. Below is the code

@Composable
fun CealSnackbar(title: String,performAction: () -> Unit,) {

    val counterState = remember { mutableStateOf(true) }

    LaunchedEffect(Unit) {
        while(true) {
            delay(4000)
            counterState.value = false
            performAction()
        }
    }

    AnimatedVisibility(
        visible = counterState.value,
        enter = slideInVertically() + fadeIn(),
        exit = slideOutVertically() + fadeOut(),
    ) {
        Card(
            modifier = Modifier
                .absoluteOffset(x = 0.dp, y = 10.dp)
                .padding(horizontal = 20.dp)
                .fillMaxWidth()
                .statusBarsPadding()
                .background(
                    color = MaterialTheme.colors.primaryVariant,
                    shape = RoundedCornerShape(10.dp)
                ), elevation = 20.dp
        ) {
            Row(
                modifier = Modifier
                    .background(color = MaterialTheme.colors.primaryVariant)
                    .padding(12.dp),
                horizontalArrangement = Arrangement.Start,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Image(
                    painter = painterResource(id = R.drawable.error_circle),
                    contentDescription = title
                )
                Text(
                    text = title,
                    fontFamily = FontFamily(Font(R.font.inter_medium)),
                    fontSize = 12.sp,
                    color = MaterialTheme.colors.primary,
                    modifier = Modifier.padding(horizontal = 10.dp)
                )
            }
        }
    }
}

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

if(errorMsg.isNotEmpty()){
        CealSnackbar(
            title = errorMsg, performAction = {
                registrationViewModel.setErrorMsg("")
            }
        )
    }

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

val snackState = remember { SnackbarHostState() }
val coroutineScope = rememberCoroutineScope()

SnackbarHost(
    hostState = snackState
) {
    CealSnackbar(
        title = errorMsg, performAction = {
            registrationViewModel.setErrorMsg("")
        }
    )
}
英文:

Use SnackbarHost

val snackState = remember { SnackbarHostState() }
    val coroutineScope = rememberCoroutineScope()



SnackbarHost(
            hostState = snackState
        ){
            CealSnackbar(
                title = errorMsg, performAction = {
                    registrationViewModel.setErrorMsg("")
                }
            )
        }

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:

确定