If I want to update all of my `Text` in material t...
# compose
c
If I want to update all of my
Text
in material to be TitleLarge, what's the best way to do that? Use
ProvideTextStyle
or a
CompositionLocalProvider
or something else at the top near my theme declaration?
k
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]rc/commonMain/kotlin/androidx/compose/material3/Text.kt;l=121 The default is set at https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]src%2FcommonMain%2Fkotlin%2Fandroidx%2Fcompose%2Fmaterial3%2F You can use a
CompositionLocalProvider
+
LocalTextStyle provides
to provide your own default text style. However, there is no API to do this only for specific composables like
Text
. Doing the above will affect all composables that are implicitly relying on the default text style.
You can have your own
Text
composable, along with a Lint rule to mandate its usage, and flag any usage of M3 variant as a build error.
c
thanks. thats fine for me actually. I guess CompositionLocalProvider + LocalTextStyle is the way to go. Although I am confused on when I would just use ProvideTextStyle or if its just better to set all of the styles on the theme to just be TitleLarge (i.e. something like ~MaterialTheme(bodyMedium = Material.typography.titleLarge))
k
That's another blunt tool that you can't limit only to
Text
composables (or any other ones). Here, you are redirecting all usages of
bodyMedium
to use
titleLarge
.
Unless you're asking to replace all the typography styles to be
TitleLarge
- all 15 / 30 of them.
k
What's the difference between TextStyle.copy and .merge?
g
@Kyant this doc may help
c
@Grant Toepfer that's definitely the right answer to the question, but on a tangent... Wow, what a questionable API! Explicitly passing values that match the defaults has the same effect as not passing anything?? Seems like explicitly passing the default arguments should be a warning, then, because
theme.merge(color = Color.Unspecified)
being equivalent to
theme.merge()
is going to give someone somewhere quite a headache. It would make sense if it was
fun Theme.merge(other: Theme)
and ignored default values, but with individual named arguments it seems like a needless trap for anyone who didn't read the docs.
g
all the defaults in that one are
null
or
.Unspecified
so 🤷
a
@Colton Idle The 'technical' ways of doing what you asked is covered already (ProvideTextSyle is how I would approach it) But, the question is kinda sus. Are you sure you want to apply titleLarge everywhere instead of changing the size of the bodyLarge (default) instead (or maybe something else)? If you do use titleLarge everywhere, then you lose the concept of title vs body which can make future theming confusing.