v79
09/02/2023, 3:25 PMinvoke on composable functions? I'm creating a desktop status bar row like this...
@Composable
fun StatusBar(
left: @Composable (() -> Unit)? = null,
centre: @Composable (() -> Unit)? = null,
right: @Composable (() -> Unit)? = null
) {
Row(
Modifier.fillMaxWidth().padding(2.dp).border(1.dp, Color.Gray),
horizontalArrangement = Arrangement.SpaceBetween
) {
Box(Modifier.padding(start = 2.dp)) { left?.invoke() }
Box { centre?.invoke() }
Box(Modifier.padding(end = 2.dp)) { right?.invoke() }
}
}
I can't find an example of implementing the built in RowScope or ColumnScope or whatever functions, which is what I think I need. But not sure.v79
09/02/2023, 3:31 PMs3rius
09/02/2023, 4:18 PMfun StatusBar(content: RowScope.() -> Unit) {
Row(...) { content() } // content() can only be called inside Row because it's providing a RowScope
}v79
09/02/2023, 4:21 PMv79
09/02/2023, 4:30 PMrememberStatusBar type function and a MutableStateOf<StatusBarContent>?s3rius
09/02/2023, 4:39 PMModifier which is passed to the immediate child. In your case it would be your Row. You can see that with almost every composable.
invoke() on composables is fine. Depending on the effect you want, you might want to avoid creating the Box if a param is null if (left != null) Box { left() }. But in your case I think you need the boxes that the layout looks as you expect it to.s3rius
09/02/2023, 4:44 PMColor.Gray.v79
09/02/2023, 4:46 PMs3rius
09/02/2023, 5:34 PMborder(1.dp, Theme.colorScheme.outline) for Material3.
Or you can also pass it as a parameter into the composable borderColor: Color = ... if you want to make it easier modifiable.
If you don't want to tie it to Material2/3 then you would also pass it in as a parameter but probably just not provide a default value, so the caller would have to provide it.