https://kotlinlang.org logo
#compose
Title
# compose
z

Zoltan Demant

06/21/2022, 4:06 AM
Is it possible to change the background color of a
DropdownMenu
? It seems to inherit it from the theme internally. Using
Modifier.background
doesnt work in the sense that Im able to see parts of the inherited background as well.
d

Dmitry Strekha

06/21/2022, 7:25 AM
you can replace your theme with new colors just for
DropdownMenu
using
CompositionLocal
z

Zoltan Demant

06/21/2022, 7:28 AM
@Dmitry Strekha Im not using MaterialTheme actually, just their components. Everything is stylized perfectly... except for
DropdownMenu
.
s

steelahhh

06/21/2022, 7:44 AM
I've had the same issue while using custom theme as well, and ended up wrapping
DropdownMenu
in a custom component and doing this:
Copy code
@Composable
internal fun ProvidersOverrides(
  colors: Colors = MaterialDebugColors,
  content: @Composable () -> Unit
) {
  MaterialTheme(
    colors = colors,
    shapes = Shapes(),
  ) {
    CompositionLocalProvider(
      LocalRippleTheme provides AppRippleTheme,
      LocalTextSelectionColors provides AppTextSelectionColors,
    ) {
      content()
    }
  }
}
With the actual component looking like this:
Copy code
fun DropdownMenu(...proxy params...) {
  ProvidersOverrides(
    colors = MaterialDebugColors.copy(surface = AppTheme.colors.surface)
  ) {
    DropdownMenu(...proxy params...)
  }
}
z

Zoltan Demant

06/21/2022, 7:49 AM
@steelahhh Thanks, Im effectively doing the same thing at the root of my custom theme currently. Id like to get rid of it if possible though as Im also passing in awful colors normally so that I can easily detect if Im relying on the wrong theme somewhere.
6 Views