Colton Idle

07/11/2022, 5:09 PM
We're trying to enforce that everyone uses our predefined text styles via a lint rule (that warns if you use a Text composable) and by having this MyText defined.
fun MyText(text: String, style:MyTextStyle, color: Color) {
  val selectedStyle = when (style) {
    MyTextStyle.H1 -> MyTypography.H1 //maps enum to TextStyle
    MyTextStyle.H2 -> MyTypography.H2
  Text(text = text, color = color, style = selectedStyle)
Does this seem like a bad idea to anyone? The map of an enum to a text style feels off, but I think this would get the job done.


07/11/2022, 6:07 PM
You could define an extension property on the enum to return the text style. But I would think that, instead of passing an enum, it would be better to define dedicated composables for each text style, something like this
fun HeaderText(text: String, color: Color, ...) {
        text = text,
        style = MaterialTheme.typography.h1,
and similar for every other one
👍 1

Zoltan Demant

07/12/2022, 5:42 AM
We did exactly that (enum that maps to style) and it's been great. There was a discussion previously on whether you should have multiple composables for the different styles of a composable (outline or normal textfield, etc); it had some good discussions but I can't find it right now (on mobile).
👍 1