gpo
gpo

Reputation: 3689

How to theme a Compose app with more colors than Material theme allows?

We have an app using Android View system that we want to start migrating to Jetpack Compose soon. The app has some screens that look pretty different so that it doesn't seem to work to use a single Material theme for the whole app. We can't really figure out which color will be primary, secondary and variants. We have more colors than this.

So I see two alternatives: Either don't use Material theme but make own own custom theme for the whole app, or use Material theme but have two or three different themes so each screen can use the relevant one.

It seems pretty easy to for solution 2 (several Material themes in a single app) in Compose. But I'm wondering if it's a good practice, if there are cons that we haven't thought about?

Thanks for your advices!

Upvotes: 0

Views: 2123

Answers (1)

Yaroslav Shulyak
Yaroslav Shulyak

Reputation: 181

Compose library has limited set of available colors in Colors class. If you want to have additional colors there are two solutions I can think of:

  1. Create extensions for Colors class for each specific color. This is fast but downside of this method is that you will have to handle logic with theming inside of each color added. Sample

  2. Create custom Colors class yourself and provide it using CompositionLocal . This way you won't need to include theme checks everywhere, you can do it only once and provide required Colors instance with CompositionLocalProvider

Upvotes: 1

Related Questions