ursus
01/27/2025, 11:26 AM@OptIn(ExperimentalMaterialApi::class)
private val MyRippleConfiguration =
RippleConfiguration(color = Color.Red, rippleAlpha = MyRippleAlpha)
In the docs the ripple config instance is cached (in a top level val)
what is the preferred way to cache this if the given color comes from Material.colors
which is @Composable
?
I cannot just access Color.x
statically like they do
The color is effectivelly static all time, so having a single instance for all is reasonable, but remember { .. }
probably doesn' make much sense, since that would scope it to composition, and I have multiple components that need this at the same timeLouis Pullen-Freilich [G]
01/27/2025, 2:06 PMLocalRippleConfiguration
is more for specific per component overrides, it isn’t intended to be used to define complex theming logic.
Material ripples use LocalContentColor
- if this doesn’t work for you at an app-wide level, then you probably would be better off building your own ripple / components that use your rippleLouis Pullen-Freilich [G]
01/27/2025, 2:07 PMursus
01/27/2025, 2:07 PMSelectableCard
which is a wrapper around Card
which adds border when selected - and the border is blue, so I want the ripple to be blue as well
Only for this one componentursus
01/27/2025, 2:08 PM@OptIn(ExperimentalMaterialApi::class)
@Composable
fun SelectableCard(
modifier: Modifier = Modifier,
selected: Boolean,
enabled: Boolean = true,
backgroundColor: Color = MaterialTheme.colors.surface,
onClick: () -> Unit,
content: @Composable () -> Unit
) {
val strokeColor = if (selected) {
MaterialTheme.colors.secondary
} else {
MaterialTheme.colors.whatever
}
CompositionLocalProvider(
LocalRippleConfiguration provides RippleConfiguration(
color = MaterialTheme.colors.secondary
),
) {
Card(
onClick = onClick,
modifier = modifier,
border = BorderStroke(1.dp, SolidColor(value = strokeColor)),
backgroundColor = backgroundColor,
elevation = 0.dp,
content = content
)
}
}
all there is to it
and the question is now how should I cache the RippleConfiguration
instance, or if I should at all?Louis Pullen-Freilich [G]
01/27/2025, 2:09 PMursus
01/27/2025, 2:09 PMLouis Pullen-Freilich [G]
01/27/2025, 2:10 PMursus
01/27/2025, 2:10 PMursus
01/27/2025, 2:10 PMLouis Pullen-Freilich [G]
01/27/2025, 2:13 PM