无法使用Jetpack Compose将行叠加在行上。

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

Unable to stack row on top of row using jetpack compose

问题

以下是翻译好的部分:

我目前有以下代码:

编辑 1:

@Composable
fun CustomerProfile(
    customerProfileViewModel: CustomerProfileViewModel = viewModel()
) {
    val snackbarHostState = remember { SnackbarHostState() }
    val scope = rememberCoroutineScope()

    Scaffold(
        snackbarHost = {
            SnackbarHost(hostState = snackbarHostState)
        }
    ) { _ ->
        Row(
            modifier = Modifier.fillMaxSize()
        ) {
            Row {
                HeaderPlate(
                    scope = scope,
                    snackbarState = snackbarHostState,
                    firstName = customerProfileViewModel.getStateValue().customer.firstName,
                    lastName = customerProfileViewModel.getStateValue().customer.lastName,
                )

                CustomerDetailsList(initialiseCustomerDetailsList(customerProfileViewModel))
            }
        }
    }
}

这段代码创建了以下视图:

无法使用Jetpack Compose将行叠加在行上。

然而,我想要的是将灰色行放在蓝色标题下面。我对Compose还有点不熟悉,所以希望能得到一些帮助!

谢谢!

编辑 1:

我已经按照你的建议更改了代码,现在我得到了以下结果:

无法使用Jetpack Compose将行叠加在行上。

如你所见,列表完全消失了。

英文:

I have the following code currently:

Edit 1:

@Composable
fun CustomerProfile(
    customerProfileViewModel: CustomerProfileViewModel = viewModel()
) {
    val snackbarHostState = remember { SnackbarHostState() }
    val scope = rememberCoroutineScope()

    Scaffold(
        snackbarHost = {
            SnackbarHost(hostState = snackbarHostState)
        }
    ) { _ ->
        Row(
            modifier = Modifier.fillMaxSize()
        ) {
            Row {
                HeaderPlate(
                    scope = scope,
                    snackbarState = snackbarHostState,
                    firstName = customerProfileViewModel.getStateValue().customer.firstName,
                    lastName = customerProfileViewModel.getStateValue().customer.lastName,
                )

                CustomerDetailsList(initialiseCustomerDetailsList(customerProfileViewModel))
            }
        }
    }
}

This creates the following view:

无法使用Jetpack Compose将行叠加在行上。

However, what I want is to have the grey rows below the blue header. I'm a little new to compose still so any help would be appreciated!

Thanks!

Edit 1:

I've changed the code to what you've suggested and now I get the following:

无法使用Jetpack Compose将行叠加在行上。

As you can see, the list has entirely disappeared.

答案1

得分: 0

你应该使用Column而不是Box

@Composable
fun CustomerProfile(
    customerProfileViewModel: CustomerProfileViewModel = viewModel()
) {
    val customerDetailsList: List<CustomerDetails> = initialiseCustomerDetailsList(viewModel = customerProfileViewModel)
    val snackbarHostState = remember { SnackbarHostState() }
    val scope = rememberCoroutineScope()

    Scaffold(
        snackbarHost = {
            SnackbarHost(hostState = snackbarHostState)
        }
    ) { it ->
        // ... 在这里使用 Column ...
        Column(
            modifier = Modifier.fillMaxSize()
        ) {
            HeaderPlate(
                scope = scope,
                snackbarState = snackbarHostState,
                firstName = customerProfileViewModel.getStateValue().customer.firstName,
                lastName = customerProfileViewModel.getStateValue().customer.lastName,
            )

            LazyColumn {
                items(count = customerDetailsList.size) { index ->
                    val customer = customerDetailsList[index]
                    CustomerDetailsCard(
                        icon = customer.icon,
                        title = customer.title,
                        value = customer.value,
                    )
                }
            }
        }
    }
}
英文:

You should use Column instead of Box:

@Composable
fun CustomerProfile(
    customerProfileViewModel: CustomerProfileViewModel = viewModel()
) {
    val customerDetailsList: List&lt;CustomerDetails&gt; = initialiseCustomerDetailsList(viewModel = customerProfileViewModel)
    val snackbarHostState = remember { SnackbarHostState() }
    val scope = rememberCoroutineScope()

    Scaffold(
        snackbarHost = {
            SnackbarHost(hostState = snackbarHostState)
        }
    ) { it -&gt;
        // ... Use Column Here ...
        Column(
            modifier = Modifier.fillMaxSize()
        ) {
            HeaderPlate(
                scope = scope,
                snackbarState = snackbarHostState,
                firstName = customerProfileViewModel.getStateValue().customer.firstName,
                lastName = customerProfileViewModel.getStateValue().customer.lastName,
            )

            LazyColumn {
                items(count = customerDetailsList.size) {index -&gt;
                    val customer = customerDetailsList[index]
                    CustomerDetailsCard(
                        icon = customer.icon,
                        title = customer.title,
                        value = customer.value,
                    )
                }
            }
        }
    }

huangapple
  • 本文由 发表于 2023年6月22日 02:31:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526187.html
匿名

发表评论

匿名网友

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

确定