https://kotlinlang.org logo
a

alorma

10/21/2020, 9:38 AM
HI! A question for compose: Is this the best way to create custom colors and shapes values?
Copy code
@Composable
val Colors.success: Color
    get() = if (isSystemInDarkTheme()) {
        green200
    } else {
        green800
    }
Copy code
@Composable
val Shapes.fab: Shape
    get() = RoundedCornerShape(percent = 50)
Copy code
MaterialThemes.Colors.success
MaterialThemes.Shapes.fab
a

alorma

10/21/2020, 10:06 AM
that was what I read too... not sure if it make sense to create a custom theme for just a few items 😅
I mean, i want to use
MaterialTheme
with esteroids... adding some properties, as we can do on android iby adding
<attr />
l

Louis Pullen-Freilich [G]

10/21/2020, 12:03 PM
Side note - the first extension could instead be:
Copy code
val Colors.success: Color
    get() = if (!isLight) {
        green200
    } else {
        green800
    }
Extensions are reasonable for small additions, but they don't scale well as soon as you start having multiple themes. It also becomes easy to change the value in one theme but not change the extensions, since they are loosely connected (in your case by using
isSystemInDarkTheme
). If it is really just a few items, then extensions / providing a separate
Ambient
along with the existing theme works, but normally 'just a few items' becomes a lot of items, and at that point it would have been a lot more type safe / expressive / scalable to build a custom theme object in the first place 🙂
a

alorma

10/21/2020, 12:19 PM
Thanks!
Will custom theme work, for example, for Surface automatic onColor? @Louis Pullen-Freilich [G]
Or for material components, like TopAppBar?
l

Louis Pullen-Freilich [G]

10/21/2020, 12:26 PM
If you create a new theme that is unrelated to the existing
MaterialTheme.colors
, then no you will need to pass these values explicitly to the components, you can't 'extend' this behavior
a

alorma

10/21/2020, 12:28 PM
Uhmm, and can MaterialThemming be extended?
As I may want to add "warning" and "success" colors
l

Louis Pullen-Freilich [G]

10/21/2020, 12:29 PM
Nope, you should just create your own theme similar to the Jetsnack example
a

alorma

10/21/2020, 12:29 PM
grr
then i will go to my approach on create few "attr" colors
many thanks
4 Views