在使用JetPack组合添加新元素后如何调整大小?

发布于 2025-02-06 21:38:38 字数 1623 浏览 2 评论 0原文

我写了下面的合成(如对话框)。当ViewState.ErrorCode!= 0时,显示了另一个可合转的。这一切都很好,但是当新的组合可见时,盒子的高度无法调整。这会导致“无形的溢出”,从而不再可见许多项目。有没有办法使盒子动态化,以便当新元素变得可见时会调整高度?

Box(modifier = Modifier
    .clip(RoundedCornerShape(4.dp))) {
    Column(
        modifier = Modifier
            .background(MaterialTheme.colors.onPrimary, MaterialTheme.shapes.large)
            .padding(12.dp)
    ) {
        Text(stringResource(R.string.verify_hint, user.email).parseBold(), fontSize = 18.textDp, fontFamily = SourceSans)

        if (viewState.errorCode != 0) {

            AlertMessage(message = stringResource(id = viewState.errorCode), color = errorColor, padding = PaddingValues(top = 12.dp))
        }

        TextField(
            value = code,
            onValueChange = { code = it },
            label = { Text(stringResource(R.string.verification_code)) },
            colors = TextFieldDefaults.textFieldColors(backgroundColor = textFieldColor),
            singleLine = true,
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 12.dp, bottom = 12.dp)
        )

        NMButton(
            onClick = { viewModel.verify(user, code, verifyLogin, language = context.getLanguageBasedOnConfiguration()) },
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 0.dp, end = 0.dp),
            icon = R.drawable.ic_badge_check_solid,
            label = stringResource(R.string.verify)
        )
    }
    if (viewState.loading) {
        Loader()
    }
}

I wrote the composable below (shown as dialog). When viewState.errorCode != 0, another composable is shown. This all works fine, but the height of the box doesn't adjust when the new composable becomes visible.This results in an 'invisible overflow' whereby a number of items are no longer visible. Is there a way to make the box dynamic so that it adjusts in height when a new element becomes visible?

Box(modifier = Modifier
    .clip(RoundedCornerShape(4.dp))) {
    Column(
        modifier = Modifier
            .background(MaterialTheme.colors.onPrimary, MaterialTheme.shapes.large)
            .padding(12.dp)
    ) {
        Text(stringResource(R.string.verify_hint, user.email).parseBold(), fontSize = 18.textDp, fontFamily = SourceSans)

        if (viewState.errorCode != 0) {

            AlertMessage(message = stringResource(id = viewState.errorCode), color = errorColor, padding = PaddingValues(top = 12.dp))
        }

        TextField(
            value = code,
            onValueChange = { code = it },
            label = { Text(stringResource(R.string.verification_code)) },
            colors = TextFieldDefaults.textFieldColors(backgroundColor = textFieldColor),
            singleLine = true,
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 12.dp, bottom = 12.dp)
        )

        NMButton(
            onClick = { viewModel.verify(user, code, verifyLogin, language = context.getLanguageBasedOnConfiguration()) },
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 0.dp, end = 0.dp),
            icon = R.drawable.ic_badge_check_solid,
            label = stringResource(R.string.verify)
        )
    }
    if (viewState.loading) {
        Loader()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2025-02-13 21:38:38

是的。
您可以在盒子修饰符上使用andaTecontentsize
然后,您声明一个动画pec,如下所示:

Box(modifier = Modifier
    .animateContentSize(
        animationSpec = tween(
            durationMillis = 300,
            easing = LinearOutSlowInEasing
        )
    ).clip(RoundedCornerShape(4.dp))
)

现在,如果出现警报,它将为盒子的大小而动画。
您还可以通过这种方法创建可扩展卡。

此视频也可能会为您提供帮助。

Yes there is.
You can use animateContentSize on your box modifier
then you declare an animationSpec like below:

Box(modifier = Modifier
    .animateContentSize(
        animationSpec = tween(
            durationMillis = 300,
            easing = LinearOutSlowInEasing
        )
    ).clip(RoundedCornerShape(4.dp))
)

Now if the AlertMessage appears it will animate the size of the box.
You can also create expandable cards with this approach.

This video may help you too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文