https://kotlinlang.org logo
#compose
Title
# compose
g

Guy Bieber

09/28/2020, 6:24 PM
I am trying to set TextStyles by MaterialTheme.typography at init time for a class. Unfortunately, typography is a @Composable function and init cannot be Composable. Any good workarounds for this?
z

Zach Klippenstein (he/him) [MOD]

09/28/2020, 6:26 PM
Just pass the typography object into your constructor as a parameter
Copy code
val typography = MaterialTheme.typography
val yourClass = remember { YourClass(typography) }
g

Guy Bieber

09/28/2020, 6:42 PM
It is in the constructor of a class I am trying to setup in init.
It looks like constructors can’t be @Composable either.
z

Zach Klippenstein (he/him) [MOD]

09/28/2020, 7:09 PM
Yes, init in kotlin is effectively the body of the constructor. Just get whatever data you need out of compose before instantiating your object, and then pass it in (see that code snippet)
g

Guy Bieber

09/28/2020, 7:27 PM
I worked around it by using my non-compose class that defines the typography. Really doesn’t need to be a compose function.
2 Views