Hi is there way, we can improve the `weight` modif...
# compose
k
Hi is there way, we can improve the
weight
modifier in better way in
Column
.
Copy code
HorizontalPager(
count = 5,
state = pagerState,
) { currentPage ->
    val (descriptionResId, imageResId, stepId) = list[currentPage]
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.height(height = 524.dp)
    ) {
        Image(
            modifier = Modifier.weight(8f),
            painter = painterResource(imageResId),
            contentDescription = null,
        )
        Text(
            modifier = Modifier
                .weight(2f)
                .padding(top = 20.dp),
            text = stringResource(stepId),
        )
        Text(
            modifier = Modifier
                .weight(3f)
                .padding(top = 20.dp),
            text = stringResource(descriptionResId),
            textAlign = TextAlign.Center,
        )
        HorizontalPagerIndicator(
            modifier = Modifier.weight(4f),
            pagerState = pagerState,
        )
    }
}
o
What do you mean by “better way”?
k
Is there any alternate way we can acheive this same behaviour without the use of weight?
o
Could you explain what is the problem so we don’t have to guess?
k
Sorry for not explaining previously. I have a 5 images to display in HorizontalPager. Some images are little bit smaller so height will be not same in all pages. So I am trying to fix to give weight modifier to give proper height..
I don't know is it right way to do that or not.
o
Since you have fixed height there on the Column
Modifier.height(height = 524.dp)
, why don’t you pre-calculate heights of all children and specify them explicitly?
k
if I want to calculate automatically by all children , is it possible to do that and remove fixed heigh?
o
There is no magic 🪄 😢 You have to tell Compose how do you want it to be laid out explicitly. You can have
weight
on all items except the pager, and size pager explicitly. You can set
heightIn
to set minimum height, etc. Depending on what do you want to achieve
k
Ok perfect!! I'll try that..
Thanks for your gudiance..