Clean Theming in Compose (You're Doing It Wrong!)

preview_player
Показать описание
The default theme in Jetpack Compose gives us access to shapes, colors and typography. However, very often we'd like to extend this with more values we'd like to share in our project like dimension or spacing values.
In this video you will learn how you can do exactly that in a very clean and concise way.

⭐ Get certificates for your future job
⭐ Save countless hours of time
⭐ 100% money back guarantee for 30 days
⭐ Become a professional Android developer now:

💻 Let me personally review your code and provide individual feedback, so it won't backfire and cost you a fortune in future:

Subscribe to my FREE newsletter for regular Android, Kotlin & Architecture advice!

Join this channel to get access to perks:

Regular live codings on Twitch:

Join my Discord server:

You like my free content? Here you can buy me a coffee:
Рекомендации по теме
Комментарии
Автор

If you found things in this video a bit off, in particular, the explanation for the default, I think that might be because some of the explanation were wrong (imho):
1. @6:26 Once you have this:
val LocalSpacing = compositionLocalOf(
defaultFactory = {
Spacing() //Notice this line here. We define a default value for the LocalSpacing.
}
)
You don't necessarily need the lines:
// CompositionLocalProvider(
// LocalSpacing provides Spacing()
// ) {
//
// }
The last lines enable you to update the value for LocalSpacing or to set it in case one was not set already, but, in this video we already set a default value when we define the CompositionLocal.
2. @9:54 The 2nd explanation as to why not to use LocalComposition - I think he got the documentation wrong.
In the documentation when they say that you better have a default value, I think what they mean is this:
val LocalSpacing = compositionLocalOf(
defaultFactory = {
Spacing() /*Notice this line here. We define a default value for the LocalSpacing. Better have some value here, as opposed to throw an exception. */
}
)
If you don't have a value here, you need to throw an exception in the default factory. Then, whenever some composable calls LocalSpacing.current, developer needs to make sure that some upper composable does indeed set the value for LocalSpacing via provides x) { ... }". From the documentation: "... Not providing a default value can cause problems and frustration when creating tests or previewing a composable that uses that CompositionLocal will always require it to be explicitly provided."

rollebonmarquis
Автор

Hey Philip. It would be cool if you can a video series going through some open source git projects and explaining the project structure. How the data flow works and all that stuff

ramasubramanian
Автор

This is so cool. I've never known about this customization before until I found this video. Keep on it!

ajailani
Автор

This is excellent, now I will make such LoacalCompositions for many other values also.
Thanks Philipp

jatinvashisht
Автор

This is very useful for novices in Compose like me!
Thanks Philipp~😘

simonsloooow
Автор

👉👉Good Morning sir I am from India and whenever I see your video I inspire and wants to become Like you and thanks you are not less than celebrity for us . keep doing ill always support .👈👈

_Mr_Megh_
Автор

Boom! Another great lesson. Thanks, Philipp!

ubersticks
Автор

Is it bad practice to include the top-level Surface inside the ComposeCustomThemingTheme()? It isn't clear or consistent when to use a top level Surface. MainActivity frequently includes this, but not always. Also in Compose previews you need to wrap your call with ComposeCustomThemingTheme { } but not normally use a Surface() {} too.

ubersticks
Автор

Yeah!! I learned a lot from this video!!

jeckonly
Автор

Wow!
Composition Locals = Redux in React and React Native.
Thanks a lot bro.
😁 😁
Your videos are awesome.
I am watching your videos 24/7
Keep it up.
👍

OmarMagdy
Автор

Hi philipp, can you enable subtitle/cc for this video? sorry for my bad english!

anhlongnghien
Автор

why don't directly use these values from data class like:

Spacing().medium

?

sayyid
Автор

Brother as usual you are creative and distinctive 💯

alijfer
Автор

Super great video as well . I really love such way to provide my themes

yehiaahmed
Автор

I just wonder why Spacing is not a part of Material Design concept yet

dmytromarchuk
Автор

This is amazing video. So I want you to make video with work manager and android key chain if you could. thank you.

nakeepanpi
Автор

Your content is always special and unique 👍👍👍👍👍👍👍👍👍👍👍👍👍👍

thegreatwarrior
Автор

Hey Philip, Great tutorial as usual
I want to add my custom colors and I also want them change in dark mode. How can I achieve that? here you showed spacing which doesn't requires modifications in light/dark mode. thanks in advance! :)

KotlinBek
Автор

@7:09. I would like to variations within a theme, for example Button Large, Button Small, Button Expressibve etc. Can you create sub-themes? Also what about custom values that could be used within the context of a more complex composable? Lets say you have a composable built from a textview, a background and a button. Can you match elements with the themes? For example lets say you have a spacer that's between a text and a button; and it could be narrow, wide or huge, and you want to create one style for each. That would mean you would want to have one style, or one base theme and multiple child themes. Can you set something in the theme like textButtonSpacing and then apply it in the composable, while setting in the themes?

davidozersky
Автор

Its amazing and very useful, thank you very much

khalidmohammed