Is there any chance to write this ```style = Mate...
# compose
c
Is there any chance to write this
Copy code
style = MaterialTheme.typography.h1,
but have the "h1" be a variable. like val myStyle = "h1" then have
Copy code
style = MaterialTheme.typography.+myStyle
a
You can create an extension function yourself similar to the fromToken() in M3.
👍 3
c
Hm. Trying to do this for colors for example and I can't because it says my function has to be a composable?
Copy code
fun getColorFromString(color: String): Color {
  when (color) {
    "color1" -> { MyTheme.colors.color1 }
  }
}
eh. I guess that's actually a different question entirely. I will use the fromToken solution for Type and try to come up with something else for color. thanks Albert
c
Hmm are you trying to extend MaterialTheme to have more color keys besides the ones included?
c
In this case I was just trying to convert from a string representation of a color (coming from a server) and converting that into a color from my theme.
c
I see - are the colors from the server raw hex values?
c
Nope. They are strings, that match in terms of names with our design system which is why it would be cool to somehow do
Copy code
MyTheme.colors.+"color1"
a
You should create an extension function on
Colors
(or your custom colors class):
Copy code
fun Colors.fromToken(token: ColorKey): Color = when (token) {
    ColorKey.Primary -> primary
    ColorKey.Secondary -> secondary
    ...
}
1