asBoxShadows static method

List<BoxShadow> asBoxShadows(
  1. double elevation, {
  2. Color? color,
  3. bool preserveOpacity = true,
})

kElevationToShadow only contains values for

key: 0, 1, 2, 3, 4, 6, 8, 9, 12, 16, 24

Optionally override the color of the kElevationToShadow shadows; further set preserveOpacity false to override the Material-standard shadow opacities with that of color.

If elevation is a value not directly found in kElevationToShadow, asBoxShadows will lerp between the two closest options.

Requests in range 24 < elevation <= 100

lerp between established kElevationToShadow.last and Elevation-created _kArbitraryElevation100.

See 🕴 Elevation documentation for more information.

Implementation

static List<BoxShadow> asBoxShadows(
  double elevation, {
  Color? color,
  bool preserveOpacity = true,
}) {
  final List<Color> _color = (color == null) ? const [] : [color];

  /// Easy case where requested [elevation] has
  /// an exact match in [kElevationToShadow] or if
  /// [elevation] is beyond even [_kArbitraryElevation100]
  if (_elevationKeys.contains(elevation))
    return kElevationToShadow[elevation]!.colorize(
      _color,
      preserveOpacity: preserveOpacity,
    );
  else if (elevation >= 100)
    return _kArbitraryElevation100.colorize(
      _color,
      preserveOpacity: preserveOpacity,
    );

  final _highestKey = _elevationKeys.lastWhere((key) => key <= elevation);
  var _nextHighestKey =
      _elevationKeys.firstWhere((key) => key > elevation, orElse: () => 100);

  /// If requested [elevation] is beyond the scope of [kElevationToShadow],
  /// `lerp` with [_kArbitraryElevation100].
  return BoxShadow.lerpList(
      kElevationToShadow[_highestKey]!.colorize(
        _color,
        preserveOpacity: preserveOpacity,
      ),
      ((_nextHighestKey == 100)
              ? _kArbitraryElevation100
              : kElevationToShadow[_nextHighestKey]!)
          .colorize(
        _color,
        preserveOpacity: preserveOpacity,
      ),
      (elevation - _highestKey) / (_nextHighestKey - _highestKey))!;
}