https://kotlinlang.org logo
g

Gabriel

11/09/2020, 8:42 PM
What's the best way to pass modifier to the sub children of a composable? I'm passing
modifier: Modifier = Modifier
from the parent to the child but, want to allow some control (background colour for eg) of the subchildren
n

Nils Druyen

11/09/2020, 8:47 PM
You can add Modifier in your child like modifier.background(Color) example: Parent: modifier = Modifier.fillMaxSize().padding(x) then pass it to child Child: modifier = modifier.background(Color).padding(x)
g

Gabriel

11/09/2020, 8:51 PM
Yeh I've got the child taking the modifier, it's the child of the child (confusing I know)
Copy code
@Composable
fun Loading(
	modifier: Modifier = Modifier,
) {
	Card(modifier.fillMaxWidth().preferredHeight(120.dp)) {
		Row(
			horizontalArrangement = Arrangement.Center,
			verticalAlignment = Alignment.CenterVertically,
			modifier = Modifier.background(Color.Black),<------I want to be able to control this modifier when calling Loading
		) {
			CircularProgressIndicator(color = MaterialTheme.colors.background)
		}
	}
}
I could have modifier and childModifier, but that seem a bit ugly, wondering if there's an established pattern for it already
n

Nils Druyen

11/09/2020, 8:57 PM
hmm correct seems ugly but its possible. I can't think of any other solution right now
g

Gabriel

11/09/2020, 9:02 PM
I guess I could provide hard control points, spinnerBackground, spinnerColour etc
z

Zach Klippenstein (he/him) [MOD]

11/09/2020, 9:19 PM
It's not ugly to pass in multiple modifiers, I've seen some official composables do this
I can't remember which ones at the moment
n

Nils Druyen

11/09/2020, 9:22 PM
You could also pass in a composable lambda for the children so you can define your row with progress indicator in a parent add parameter children: @Composable () -> Unit and move the child code.
1
g

Gabriel

11/09/2020, 9:38 PM
Thanks for the ideas guys :D
2 Views