Nat Strangerweather
05/03/2021, 2:11 PMboardWidth
value elsewhere.Nat Strangerweather
05/03/2021, 2:11 PM@Composable
fun CustomLayout(modifier: Modifier, children: @Composable () -> Unit) {
Layout(children, modifier) { measurables, constraints ->
layout(constraints.maxWidth, constraints.maxHeight) {
val boardWidth = constraints.maxWidth
val tileSize = constraints.maxWidth / 7
val childConstraints = constraints.copy(
minWidth = minOf(constraints.minWidth, tileSize),
maxWidth = tileSize
)
require(measurables.size == 2)
measurables[0].measure(childConstraints).place(0, 0)
measurables[1].measure(childConstraints).place(boardWidth - tileSize, 0)
}
}
}
Kirill Grouchnikov
05/03/2021, 2:28 PMNat Strangerweather
05/03/2021, 2:31 PMKirill Grouchnikov
05/03/2021, 2:34 PMNat Strangerweather
05/03/2021, 2:37 PMAdam Powell
05/03/2021, 2:37 PMAdam Powell
05/03/2021, 2:38 PMlayout
function at the end of the layout dsl block ๐Kirill Grouchnikov
05/03/2021, 2:39 PMNat Strangerweather
05/03/2021, 2:39 PMKirill Grouchnikov
05/03/2021, 2:39 PMNat Strangerweather
05/03/2021, 2:40 PMKirill Grouchnikov
05/03/2021, 2:41 PMNat Strangerweather
05/05/2021, 9:59 AM@Composable
fun GameBoard() {
BoxWithConstraints(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(0.75f)
.padding(20.dp)
.background(Color.DarkGray)
.clipToBounds()
) {
val boxWidth = constraints.maxWidth
println(boxWidth)
CustomLayout(
Modifier
.fillMaxSize()
) {
Column {
repeat(7) {
Tile(Color.Magenta)
}
}
Column {
repeat(7) {
Tile(Color.Yellow)
}
}
}
}
}
Many thanks for your help! ๐Adam Powell
05/05/2021, 2:57 PMLayout
Adam Powell
05/05/2021, 2:58 PMCustomLayout
will only see two direct children - those columns - and won't have any visibility to the `Tile`s, which seems to be the end goalAdam Powell
05/05/2021, 3:02 PMColumn
is going to measure and place the tiles within it. The code above is delegating ownership of tile measurement/placement responsibility to Column
, but you want to keep that ownership to yourself so that you can move and place things yourself. There's no need to retrieve positions from anything else if you're the one that placed them in the first place - in that arrangement you already have that data.Adam Powell
05/05/2021, 3:06 PMBoxWithConstraints
to tell you the available constraints; any invocation of Layout
already gets that data too. You should never need to "leapfrog" layout information across layers this wayNat Strangerweather
05/05/2021, 3:13 PM