Apologize if this has been answered before, but i’...
# compose
m
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
You can still access the
Context
in a traditional way
m
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
What does your function look like?
m
this is mostly an issues for the default value of the ambient
l
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
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
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
Right but why can’t you get a
Context
not from an ambient to build your color palette?
m
Copy code
fun lightColors(): MyColors =
    MyColors(
        primaryButtonColor = MyColorPallete.blueSky,
        primaryButtonTextColor = MyColorPallete.white
    )
I can’t reference an ambient in there.
r
Just pass a
Context
to your function?
m
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
You could also simply initialize your list of colors using the Activity’s or the Application’s
Context
m
Copy code
@Composable
fun MyTheme(
    colors: MyColors = MyTheme.colors,
r
(just like you would have done before Compose)
m
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