【问题标题】:Jetpack Compose Dialog view size(Height or width) is not updated after initial recompositionJetpack Compose Dialog 视图大小(高度或宽度)在初始重组后未更新
【发布时间】:2023-02-02 18:41:04
【问题描述】:

我有一个对话框,用户有 2 个选项。每个选项都有不同的 Compose UI 视图。问题是对话框的高度永远不会根据用户选择的选项更新。那么有没有办法根据内容动态改变对话框的高度。

这是我用来显示对话框的代码

var type = remember { mutableStateOf("Adult") }
Dialog(
        onDismissRequest = { onDismiss() }, properties = DialogProperties(
            dismissOnBackPress = true, dismissOnClickOutside = true
        ),
    ) {
        Card(
            shape = RoundedCornerShape(12.dp),
            modifier = Modifier.fillMaxWidth(),
            elevation = 8.dp
        ) {
            Column(
        modifier = Modifier
            .height(IntrinsicSize.Min)
            .background(Color.White)
            .padding(top = 10.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Row() {
            SemiBoldTextSmall(
                text = "Adult",
                textColor = if (type.value == "Adult") Color.Black else Color.LightGray,
                fontSize = 14.sp,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
                    .weight(1f)
                    .clickable {
                        type.value = "Adult"
                    })
            SemiBoldTextSmall(text = "Kids",
                textColor = if (type.value == "Kids") Color.Black else Color.LightGray,
                fontSize = 14.sp,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
                    .weight(1f)
                    .clickable {
                        type.value = "Kids"
                    })
        }
        if (type.value == "Kids") {
            EditBox(modifier = Modifier
                .padding(top = 10.dp, start = 20.dp, end = 20.dp)
                .background(color = Color.White, shape = RoundedCornerShape(10.dp))
                .border(
                    width = 1.dp,
                    color = Color.LightGray,
                    shape = RoundedCornerShape(10.dp)
                )
                .wrapContentSize(),
                placeholder = "Mobile Number",
                text = "",
                textSize = 14.sp,
                keyboardType = KeyboardType.Number,
                onValueChange = {
                })
        } else {
            EditBox(modifier = Modifier
                .padding(top = 30.dp, start = 20.dp, end = 20.dp)
                .background(color = Color.White, shape = RoundedCornerShape(10.dp))
                .border(
                    width = 1.dp,
                    color = Color.LightGray,
                    shape = RoundedCornerShape(10.dp)
                )
                .wrapContentSize(),
                placeholder = "Name",
                text = "",
                textSize = 14.sp,
                onValueChange = {

                })

            EditBox(modifier = Modifier
                .padding(top = 10.dp, start = 20.dp, end = 20.dp)
                .background(color = Color.White, shape = RoundedCornerShape(10.dp))
                .border(
                    width = 1.dp,
                    color = Color.LightGray,
                    shape = RoundedCornerShape(10.dp)
                )
                .wrapContentSize(),
                placeholder = "Mobile Number",
                text = "",
                textSize = 14.sp,
                keyboardType = KeyboardType.Number,
                onValueChange = {
                })
        }

        SemiBoldTextSmall(text = "+ ADD",
            textColor = Color.White,
            fontSize = 14.sp,
            modifier = Modifier
                .padding(top = 30.dp)
                .fillMaxWidth()
                .background(color = greenColor, shape = RectangleShape)
                .padding(10.dp)
                .clickable {
                    onSubmit()
                })
    }
        }
    }

    

【问题讨论】:

    标签: android android-jetpack-compose android-jetpack


    【解决方案1】:

    使用 Modifier.requiredHeight() 插入 height() Modifier.requiredHeight():声明内容的高度与内容的最小或最大固有高度完全相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-25
      • 2022-07-26
      • 1970-01-01
      • 2022-10-13
      • 2022-07-12
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      相关资源
      最近更新 更多