Home > Mobile >  Extending Material Theme with Kotlin extension property doesn't work
Extending Material Theme with Kotlin extension property doesn't work

Time:01-25

The official enter image description here

Can't see what's wrong myself Can anyone?

CodePudding user response:

Well, the Typography class itself has a member named h1 which will be used instead of your extension. You should actually get this warning in Android studio which explains it pretty well:

Extension is shadowed by a member: public final val h1: TextStyle

The documentation you mention is called "Custom design systems in Compose" - this is meant for someone who doesn't want to use the default Typography properties - either needs more text styles than provided or simply wants to name them differently. You should probably look at this section first - it shows how to create your own theme and override the default Typography styles. It should look something like this:

@Composable
fun MyTheme(
    content: @Composable () -> Unit,
) {
    MaterialTheme(
        typography = Typography(
            h1 = TextStyle(...),
        ),
        content = content,
    )
}

@Composable
fun MyContent() {
    MyTheme {
        // MaterialTheme.typography.h1 will be your defined TextStyle here
    }
}
  •  Tags:  
  • Related