How to calculate Card background color if it has non-zero elevation? #50
-
Sorry, couldn't find the answer in the docs. So if Card component has zero elevation, its background color is in Theme.of(context).colorScheme.surface. But with non-zero elevation it gets adjusted to visually reflect elevation. So how to calculate it? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @dannylin108, thanks for your question. Yes, if the This only happens if the used background color of the elevated material has the same color as The Flutter SDK framework provides the algorithm for this Card and Material color change due to elevation via the function /// Applies an opacity to [color] based on [elevation].
static Color _overlayColor(Color color, double elevation) {
// Compute the opacity for the given elevation
// This formula matches the values in the spec:
// https://material.io/design/color/dark-theme.html#properties
final double opacity = (4.5 * math.log(elevation + 1) + 2) / 100.0;
return color.withOpacity(opacity);
} If you have You can see how the Material widget's (Card widget is built using Material Widget) elevation is done here: And the actual color calculation functions are found in the Flutter SDK class Hope this answers your question 😃 💙 BR Mike |
Beta Was this translation helpful? Give feedback.
Hi @dannylin108, thanks for your question.
Yes, if the
useMaterial3
is false, so that we are using Material2 styles, and whenapplyElevationOverlayColor
is true, which it in Material2 should only be in dark mode (FlexColorScheme defaults to this), then the background color will change with higher elevation and get lighter automatically in dark mode.This only happens if the used background color of the elevated material has the same color as
Theme.of(context).colorScheme.surface
. Some FlexColorSchemesurfaceMode
values will use a blend strength forThemeData.backgroundColor
that makes it diverge fromsurface
(andcanvasColor
), such surfaces will then not get an overlay color in dark mode, …