Restrict compose modifier that only accessible from my composable function

105 Views Asked by At

I have a composable function:

@Composable
fun GapText(
    modifier: Modifier,
    text: String,
) {
    Row(modifier = modifier) {
        Text(text = text)
        ...
    }
}

Now I want to create a modifier that only GapText can use:

fun Modifier.customTag(tag: String) =
    this then CustomTag(tag)

But with this function that I wrote every composable function can use it. What should I do?

1

There are 1 best solutions below

1
On

As of now there's no specific modifier for this, but what one could do is:

1 - Declare the Composable on a separated file and make the modifier private:

@Composable
fun GapText(
    modifier: Modifier,
    text: String,
) {
    Row(modifier = modifier) {
        Text(text = text)
        ...
    }
}

private fun Modifier.customTag(tag: String) =
    this then CustomTag(tag)

2 - Declare the modifier function within the Composable:

@Composable
fun GapText(
    modifier: Modifier,
    text: String,
) {
    Row(modifier = modifier) {
        Text(text = text)
        ...
    }

    fun Modifier.customTag(tag: String) =
        this then CustomTag(tag)
}