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

mattinger

08/27/2020, 5:04 PM
Apologize if this has been answered before, but i’m busy creating my own custom theme, modeling it after how MaterialTheme is done (our UX doesn’t use material design unfortunately). I’m defining my various color schemes, and one thing i’m struggling with is how to get colors from resource. MaterialTheme has all it’s colors defined in code, and i’m importing a library which has a colors.xml in it, and would like to take from there. Because the color pallete functions (like lightColors) are not composable, I can’t refer to the Ambient context.
r

romainguy

08/27/2020, 5:06 PM
You can still access the
Context
in a traditional way
m

mattinger

08/27/2020, 5:09 PM
I’m not sure what you mean. I tried passing in the Context to the functions which build the color palletes. However, the ambient wouldn’t have a context that it can pass in, as it can’t access other ambients.
r

romainguy

08/27/2020, 5:11 PM
What does your function look like?
m

mattinger

08/27/2020, 5:11 PM
this is mostly an issues for the default value of the ambient
l

Louis Pullen-Freilich [G]

08/27/2020, 5:11 PM
Yeah, probably easier to just not use a default then, and just make sure to provide the value to the ambient in your theme function. Or you can just use a default that doesn't require any ambients / context
m

mattinger

08/27/2020, 5:14 PM
Unfortunately, the default really should be taken from the colors file. Our UX is defining colors and that undergoes a transformation to .xml files, swift files, css files which get published out and we’re using them.
The default is a very specific set of color choices.
I’m starting right now with primaryButtonColor and primaryButtonColorText
l

Louis Pullen-Freilich [G]

08/27/2020, 5:15 PM
You can still define that default, but it doesn't need to be defined in the ambient default factory - it depends how you are setting / retrieving the theme elsewhere
r

romainguy

08/27/2020, 5:15 PM
Right but why can’t you get a
Context
not from an ambient to build your color palette?
m

mattinger

08/27/2020, 5:17 PM
Copy code
fun lightColors(): MyColors =
    MyColors(
        primaryButtonColor = MyColorPallete.blueSky,
        primaryButtonTextColor = MyColorPallete.white
    )
I can’t reference an ambient in there.
r

romainguy

08/27/2020, 5:17 PM
Just pass a
Context
to your function?
m

mattinger

08/27/2020, 5:17 PM
But i suppose i could have the ambient have a null default value.
that’s what as getting me
Copy code
internal val MyColorsAmbient =
    staticAmbientOf {
        lightColors()
    }
because in here, i can’t use an ambient because it’s not composable.
I suppose i can instead have a null value, and use the ambient context in the theme to build the instance
r

romainguy

08/27/2020, 5:19 PM
You could also simply initialize your list of colors using the Activity’s or the Application’s
Context
m

mattinger

08/27/2020, 5:20 PM
Copy code
@Composable
fun MyTheme(
    colors: MyColors = MyTheme.colors,
r

romainguy

08/27/2020, 5:20 PM
(just like you would have done before Compose)
m

mattinger

08/27/2020, 5:20 PM
i’d either pass a context object to the theme, or just have no default value and make it be passed in.
maybe a second version of the theme function so that you either have to pass in a context or a MyColors object
I think that’s viable
so i just need one Theme function, but the default value will create the lightColors with the ambient context instead of the ambient having a default value
2 Views