colorize method

List<BoxShadow> colorize(
  1. List<Color> colors, {
  2. bool preserveOpacity = false,
})

Provide a Color or List<Color> 🎨 colors to override this List's BoxShadow.colors.

If 🎨 colors has less entries than this List, colors.last will be applied to the extra BoxShadows.

Optionally

Pass true to ❓ preserveOpacity to maintain this List's BoxShadow.colors' opacities, applying only the RGB from colors.

Implementation

List<BoxShadow> colorize(
  List<Color> colors, {
  bool preserveOpacity = false,
}) {
  if (colors.isEmpty) return this;

  List<BoxShadow> coloredShadows = [];
  int i = 0;
  Color getColor() => (i >= colors.length) ? colors.last : colors[i];

  for (BoxShadow shadow in this) {
    final opacity =
        preserveOpacity ? shadow.color.opacity : getColor().opacity;
    coloredShadows.add(shadow * getColor().withOpacity(opacity));
    i++;
  }

  return coloredShadows;
}