Kevin Worth
05/02/2023, 6:36 PMuiMode
is set to UI_NIGHT_MODE_YES
unless we start the preview with Scaffold
or Surface
. There must be a better way.
Regarding CompositionLocalProvider
, does anyone have any insight into this SO comment I just made?
Basically, it's cool that we can customize/override LocalContentColor
, but the next thing we reached for (to try to get our previews to look right) was something like LocalBackgroundColor
or LocalContainerColor
, etc.Kevin Worth
05/02/2023, 6:41 PMcolor.background
) or Surface
(whose background defaults to color.surface
), or am I just missing something silly?ephemient
05/02/2023, 6:46 PMMaterialTheme
set the composition locals to reasonable values?yschimke
05/02/2023, 7:01 PMKevin Worth
05/02/2023, 7:02 PMMaterialTheme
. That's the idea, you meant, right?ephemient
05/02/2023, 7:03 PM@Preview
@Composable
fun Sample() {
MaterialTheme {
Surface {
// etc.
Kevin Worth
05/02/2023, 7:08 PMSurface
in there. That was what we were hoping to not have to do.
But, additionally, it appears that using MaterialTheme
in the preview does not change LocalContentColor
, so no I don't think that's a solution.ephemient
05/02/2023, 7:18 PMMaterialTheme
lacked the right call… we have our own CompanyNameTheme
wrapper that uses isSystemInDarkTheme()
Kevin Worth
05/02/2023, 10:02 PM@Composable
fun SomePreview() {
AcmeTheme {
// Background is always pure white
// and LocalContentColor is always
// pure black
someComposableToPreview()
Surface {
// Now because we're in a
// Surface, the color behind
// everything is
// color.Surface and
// LocalContentColor is not
// pure black (because it is
// onSurface). Would be
// nice to not have to do this
someComposableToPreview()
}
}
}
Stylianos Gakis
05/03/2023, 8:40 AMHedvigTheme {
Surface(color = MaterialTheme.colorScheme.background) {
// actual preview code
}
}
Here’s an example https://github.com/HedvigInsurance/android/blob/878463340b9e94ad42efa1fd3a36a0e8d0[…]urance/step/terminationsuccess/TerminationSuccessDestination.kt
The live template I use for this is:
@com.hedvig.android.core.designsystem.preview.HedvigPreview
@androidx.compose.runtime.Composable
private fun Preview$NAME$() {
com.hedvig.android.core.designsystem.theme.HedvigTheme {
androidx.compose.material3.Surface(color = androidx.compose.material3.MaterialTheme.colorScheme.background) {
$NAME$($END$)
}
}
}
So all I do is:
• copy the name of the composable I want to preview.
• write prev
and press enter
• paste my clipboard (this fills the preview name + the call inside the function)
• press enter again
And that’s it basically. This has been the easiest thing to do in our case.Chris Sinco [G]
05/03/2023, 1:50 PMChris Sinco [G]
05/03/2023, 1:52 PMChris Sinco [G]
05/03/2023, 1:55 PMKevin Worth
05/03/2023, 2:11 PM// Theme.kt
CompositionLocalProvider(LocalContentColor provides ourColorScheme.ourDefaultContentColor) {
MaterialTheme(
colorScheme = ourColorScheme,
...
)
}
So, can you imagine a world where LocalContainerColor
(or similar) is a thing? That would fix ("fix") our previews all around, I believe.Chris Sinco [G]
05/03/2023, 3:03 PMChris Sinco [G]
05/03/2023, 3:12 PMChris Sinco [G]
05/03/2023, 3:13 PMdorche
05/03/2023, 3:15 PMStylianos Gakis
05/03/2023, 3:22 PMKevin Worth
05/03/2023, 3:32 PMSurface
as their root and are light mode (because the preview's background is very light - if not pure white), so our light themed LocalContentColor is dark on light. Great. But in dark mode previews (where preview's background is that same light color), the light colored LocalContentColor is light on light and cannot be seen well, if at all.
All in all, it was cool to discover we could use LocalContentColor very high up in our theme and that fixed the text color (and other content) in the previews to use our primary content color instead of the default black. It just left us reaching for LocalContainerColor
so that it wouldn't always be required to wrap stuff in something like Scaffold or Surface in order to get the expected, default background color. Hopefully I'm making sense. Sorry if not.Chris Sinco [G]
05/03/2023, 3:45 PMChris Sinco [G]
05/03/2023, 3:47 PMdorche
05/03/2023, 3:48 PMChris Sinco [G]
05/03/2023, 3:56 PMIt just left us reaching for@Kevin Worth is this API necessary though if you have a theme Composable like MaterialTheme in your hierarchy, which should have container colors with LocalColors?so that it wouldn't always be required to wrap stuff in something like Scaffold or Surface in order to get the expected, default background color.LocalContainerColor
Kevin Worth
05/03/2023, 4:24 PMshowBackground
, we may be doing something wrong, but I've been unable to see a difference when toggling that back and forth. My theory has been perhaps my composable is covering up (with the preview not showing anything extra, outside of the previewed composable), but that's where I started to get confused, because like you said, I would think my composable should be transparent, and if it is, then the fact that I'm seeing white in the background tells me that something is going wrong somewhere, and maybe it's in something we're doing that's odd - still new to this project and figuring out what people did before me.Chris Sinco [G]
05/03/2023, 8:43 PMephemient
05/03/2023, 9:03 PM@Preview(uiMode = Configuration.UI_MODE_NIGHT_NO, showBackground = true)
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
fun Sample() {
Box(Modifier.size(20.dp, 20.dp))
}
ephemient
05/03/2023, 9:05 PM@Preview(uiMode = Configuration.UI_MODE_NIGHT_NO, backgroundColor = 0xFFFFFFFF, showBackground = true)
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES, backgroundColor = 0xFF000000, showBackground = true)
ephemient
05/03/2023, 9:06 PMKevin Worth
05/04/2023, 12:10 PMshowBackground = true
hasn't worked. Thanks @ephemient. Also I missed that backgroundColor
was a possible parameter. So thanks for that too.Chris Sinco [G]
05/04/2023, 8:03 PMChris Sinco [G]
05/04/2023, 8:11 PMshowBackground
but no Surface, we will insert a background that uses the default light/dark MaterialTheme.colorScheme.background color. When you don’t have showBackground
it will be transparent, unless you use a Surface.Chris Sinco [G]
05/04/2023, 8:11 PMChris Sinco [G]
05/04/2023, 8:15 PMChris Sinco [G]
05/04/2023, 8:21 PMChris Sinco [G]
05/04/2023, 8:21 PMKevin Worth
05/05/2023, 12:26 PMChris Sinco [G]
05/05/2023, 11:17 PMChris Sinco [G]
05/05/2023, 11:21 PMColton Idle
05/06/2023, 7:06 PMProvideMyAppColors(colors) {
MaterialTheme(MaterialTheme.colors, content = content)
}
so I still need to add some conditional inside of MaterialTheme(
to swtich between material light and dark colors, right? Because I'm using the above, and still getting a light background with dark preview.Colton Idle
05/06/2023, 7:22 PMif (isSystemInDarkTheme()) androidx.compose.material.darkColors() else androidx.compose.material.lightColors(),
Colton Idle
05/06/2023, 7:24 PMProvideMyAppColors(colors) {
MaterialTheme(if (isSystemInDarkTheme()) androidx.compose.material.darkColors() else androidx.compose.material.lightColors()){
androidx.material3.MaterialTheme(
colorScheme = if (isSystemInDarkTheme()) darkColorScheme() else lightColorScheme(), content()
)
}
}
Colton Idle
05/06/2023, 7:26 PMColton Idle
05/06/2023, 7:27 PMChris Sinco [G]
05/06/2023, 7:45 PMColton Idle
05/07/2023, 2:29 PMChris Sinco [G]
05/07/2023, 2:39 PMColton Idle
05/07/2023, 3:58 PM