0

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?

Saman Sattari
  • 3,322
  • 6
  • 30
  • 46

1 Answers1

0

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)
}
william xyz
  • 710
  • 5
  • 19
  • I don't know how these two approaches can help. in these two approaches no one cannot use the modifier on GapText fun: ''' GapText( modifier = Modifier.customTag("tag") .... ) ''' – Saman Sattari Jun 06 '23 at 11:59