Ryan Casler
07/10/2024, 6:37 PMFlowRow
with 2 equal size columns. For the child items of the row I tried to set the widths as fillMaxWidth(0.5f)
on some preview sizes it will make it single column. If i update it to be fillMaxWidth(0.4999999f)
it will always be two rows with any screen size. I am just really interested on why for some screen configurations the 0.5 works but not on all. Any insight or thoughts would be appreciated. Know there are other ways just really intrigued why that does not seem to work.Ryan Casler
07/10/2024, 6:38 PM@OptIn(ExperimentalLayoutApi::class)
@Composable
fun FlowRowRounding(maxWidth: Float) {
FlowRow(
modifier = Modifier.fillMaxWidth(),
maxItemsInEachRow = 2,
) {
for (i in 1..2) {
Text(
modifier = Modifier.fillMaxWidth(maxWidth),
text = "Item $i",
textAlign = TextAlign.Center,
)
}
}
}
@Preview(widthDp = 350, showBackground = true)
@Composable
fun FlowRowMaxWidthHalf() {
FlowRowRounding(maxWidth = .5f)
}
@Preview(widthDp = 350, showBackground = true)
@Composable
fun FlowRowMaxWidth49999() {
FlowRowRounding(maxWidth = .4999999f)
}
Snippet showing the issue.Alex Vanyo
07/10/2024, 6:55 PM0.5
, I’m guessing that there are an odd number of pixels available. The pre-rounded solution would be to have each item take up 0.5
of a pixel, but that isn’t possible, so both round up - which then takes 1 more pixel than is available, so it wrapsRyan Casler
07/10/2024, 7:06 PMAlex Vanyo
07/10/2024, 7:59 PM0.49999f
is probably resulting in both sizes rounding down, but that means the sum of item1's width and item2's width can be one smaller than the total amount of available width. This probably doesn’t matter in most cases, but you could end up with some 1 pixel artifacts when backgrounds get involvedRyan Casler
07/10/2024, 8:11 PMRyan Casler
07/10/2024, 8:11 PMRyan Casler
07/10/2024, 8:14 PMRow
with the same 2 items on the same preview device that are 0.5f
they render correctly. So likely something more going on there with regards to the math that goes into calculating maximum width of a FlowRow
vs a Row
Alex Vanyo
07/10/2024, 8:32 PMRow
and Column
do some very specific handling around those accumulated errors when weights are involved:
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]icy.kt;l=222-224;drc=25969d8585b6735cf7b56bf4dc118c68d3a1704f