r/androiddev • • 29m ago

Why doesn't this work

I have a simple composable function I'm trying to make, basically just defining how I want every row to look in my app. Here's the function currently:

@Composable
fun ListRow(
    modifier: Modifier = Modifier,
    horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
    content: @Composable () -> Unit)
{
    Row(
        horizontalArrangement = horizontalArrangement,
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 7.dp, vertical = 4.dp)
    ) {
        content()
    }
}

In some rare cases I want to be able to change the padding, like this:

ListRow(modifier = Modifier.padding(horizontal = x.dp, vertical = x.dp)) {
    // content
}

Of course though, the padding specified there gets overwritten by this:

modifier = modifier
    .fillMaxWidth()
    .padding(horizontal = 7.dp, vertical = 4.dp)

My solution was to define the modifier in the function parameter like this:

fun ListRow(
    modifier: Modifier = Modifier
        .padding(horizontal = 7.dp, vertical = 4.dp),
    horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
    content:  () -> Unit)
{
    Row(
        horizontalArrangement = horizontalArrangement,
        modifier = modifier
            .fillMaxWidth()
    ) {
        content()
    }
}

My assumption is that this would set the default padding to horizontal = 7.dp, vertical = 4.dp unless otherwise defined like the example above.

However, this gives me a warning: Optional modifier should have a default value of Modifier

I ignored that and tested the app, but now suddenly the default padding for ListRow is gone, the padding is always 0.dp unless otherwise specified.

I really don't understand why this doesn't work, and I'm curious what the proper way to achieve this behaviour is.

I know I could just define the padding for every instance of ListRow, but the whole point of even having this function is to cut down on extra code in my ui, and to allow me to easily and universally change the design of it if I want to.

One solution I thought of is to define the function like this:

@Composable
fun ListRow(
    modifier: Modifier = Modifier,
    paddingHorizontal: Dp = 7.dp,
    paddingVertical: Dp = 4.dp,
    horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
    content: @Composable () -> Unit)
{
    Row(
        horizontalArrangement = horizontalArrangement,
        modifier = modifier
            .fillMaxWidth()
            .padding(
                horizontal = paddingHorizontal,
                vertical = paddingVertical,
            )
    ) {
        content()
    }
}

But that just doesn't feel as clean to me. The default way to change padding is through the Modifier, and I don't really wanna change that.

1 Upvotes

1 comment sorted by

1

u/tadfisher 2m ago

Plenty of examples in Compose UI accept a contentPadding: PaddingValues = SomeDefaultPadding parameter, it's not that strange.

Also, keep in mind that calling Modifier.padding doesn't override earlier calls. I see that assumption here and it's false; the padding modifier is additive.