简体   繁体   English

使用 Jetpack compose 添加新元素后如何调整大小?

[英]How to resize after adding a new element using Jetpack compose?

I wrote the composable below (shown as dialog).我在下面写了可组合的(显示为对话框)。 When viewState.errorCode != 0, another composable is shown.当 viewState.errorCode != 0 时,显示另一个可组合项。 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()
    }
}

Yes there is.就在这里。 You can use animateContentSize on your box modifier then you declare an animationSpec like below:你可以在你的盒子修饰符上使用animateContentSize然后你声明一个 animationSpec 如下:

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.现在,如果出现 AlertMessage,它将为框的大小设置动画。 You can also create expandable cards with this approach.您还可以使用这种方法创建可扩展的卡片。

This video may help you too.该视频也可能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM