Skip to content

Using FlowRow To Solve Compose Quadrants Causes Weird Behaviour #23

@wscullen

Description

@wscullen

I'm trying to use FlowRow to build the grid of cards instead of using 2 hardcoded Rows with a Column layout.

It works as expected, except when trying to dynamically set the height of the rows to fill the available space using the fillMaxHeight modifier.

@Composable
fun ComposeQuadrantsCell(
    title: String,
    body: String,
    color: Int,
    modifier: Modifier = Modifier
) {
    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = modifier
            .fillMaxSize()
            .background(color = colorResource(color))
            .padding(16.dp)
    ) {
        Text(
            text = title,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.padding(bottom = 16.dp)
        )
        Text(text = body, textAlign = TextAlign.Justify)
    }
}

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun ComposeQuadrants(
    modifier: Modifier = Modifier
) {

    FlowRow(
        modifier = modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .padding(0.dp)
            .border(2.dp, Color.Red),
        horizontalArrangement = Arrangement.Start,
        maxItemsInEachRow = 2
    ) {
        ComposeQuadrantsCell(
            title = stringResource(R.string.text_composable),
            body = stringResource(R.string.text_composable_description),
            color = R.color.quadrant1,
            modifier = Modifier.fillMaxHeight(0.4999999f).fillMaxWidth(0.5f)
        )
        ComposeQuadrantsCell(
            title = stringResource(R.string.image_composable),
            body = stringResource(R.string.image_composable_description),
            color = R.color.quadrant2,
            modifier = Modifier.fillMaxHeight(0.4999999f).fillMaxWidth(0.5f)
        )
        ComposeQuadrantsCell(
            title = stringResource(R.string.row_composable),
            body = stringResource(R.string.row_composable_description),
            color = R.color.quadrant3,
            modifier = Modifier.fillMaxHeight(0.4999999f).fillMaxWidth(0.5f)

        )
        ComposeQuadrantsCell(
            title = stringResource(R.string.column_composable),
            body = stringResource(R.string.column_composable_description),
            color = R.color.quadrant4,
            modifier = Modifier.fillMaxHeight(0.4999999f).fillMaxWidth(0.5f)
        )

    }
}

@Preview(showBackground = true)
@Composable
fun ComposeQuadrantsPreview() {
    GreetingCardTheme {
        ComposeQuadrants()
    }
}

When I set the fillMaxHeight to exactly 0.5f, the preview Composable looks fine, but when I run it in the emulator, the bottom 2 cells are not shown. If I set the height to 0.4999999f, it works correctly.

2 questions:

  1. Any idea why this workaround is required?
  2. When using the weight modifier with FlowRow, it seems to affect both width and height, as I could no longer change the height with the fillMaxHeight modifier when using the weight modifier. Is this correct?
Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions