Bradleycorn
01/09/2021, 4:36 PMenum class Orientation {
HORIZONTAL,
VERTICAL
}
@Composable
fun OrientedContent(
orientation: Orientation,
modifier: Modifier = Modifier,
content: @Composable () -> Unit) = when (orientation) {
Orientation.HORIZONTAL -> Row(modifier = modifier) { content() }
Orientation.VERTICAL -> Column(modifier = modifier) { content () }
}
Dominaezzz
01/09/2021, 4:40 PMAdam Powell
01/09/2021, 4:45 PMRow
when you want horizontal and Column
when you want vertical 😛Bradleycorn
01/09/2021, 4:46 PMAdam Powell
01/09/2021, 4:50 PMvar orientation by remember { mutableStateOf(Orientation.Horizontal) }
OrientedContent(orientation) {
var counter by remember { mutableStateOf(0) }
Button(
onClick = {
counter++
orientation = if (orientation == Orientation.Horizontal) Orientation.Vertical else Orientation.Horizontal
}
) {
Text("Toggle!")
}
Text("Clicked $counter times")
}
Adam Powell
01/09/2021, 4:52 PMDominaezzz
01/09/2021, 4:54 PMBradleycorn
01/09/2021, 4:58 PMRow
or Column
" is obvious now. What led me down the OrientedContent
path was a refactor. I originally had a Composable ( PostMetaData
) that was showing a Row
of several Text
composables. In a new spot, I needed to show the exact same things, just in a column instead of a Row, and so I came up with the above solution, instead of just extracting the Row
from PostMetaData
entirely and wrapping the call(s) to it in the appropriate Row/Column. It allowed me to leave my original calls to PostMetaData
untouched, while allowing the new call to achieve verticality.Dominaezzz
01/09/2021, 4:58 PMAdam Powell
01/09/2021, 4:58 PMAdam Powell
01/09/2021, 4:58 PMAdam Powell
01/09/2021, 4:59 PMcontent
in the different when
branches has different identityBradleycorn
01/09/2021, 5:00 PMBradleycorn
01/09/2021, 5:01 PMDominaezzz
01/09/2021, 5:04 PMAdam Powell
01/09/2021, 5:05 PMAdam Powell
01/09/2021, 5:10 PMBradleycorn
01/09/2021, 5:14 PMAdam Powell
01/09/2021, 5:20 PMBradleycorn
01/09/2021, 5:20 PMBradleycorn
01/09/2021, 5:21 PMAdam Powell
01/09/2021, 5:21 PM