copyWith method

LightGradient copyWith({
  1. List<LightColorXy>? points,
  2. String? mode,
  3. List<String>? modeValues,
  4. bool copyOriginalValues = true,
})

Returns a copy of this object with its field values replaced by the ones provided to this method.

copyOriginalValues is true if you want to maintain the original object's initial values. This is useful if you plan on using this object in a PUT request.

Throws GradientException if points has more than 5 elements.

Throws InvalidValueException if the LightGradient copy has a mode that is not in its modeValues array.

Implementation

LightGradient copyWith({
  List<LightColorXy>? points,
  String? mode,
  List<String>? modeValues,
  bool copyOriginalValues = true,
}) {
  // Make sure the gradient is valid.
  if (!Validators.isValidGradientDraft(points)) {
    throw GradientException.withValue(points?.length ?? -1);
  }

  // Make sure the mode is valid.
  if (modeValues != null &&
      mode == null &&
      !Validators.isValidValue(this.mode, modeValues)) {
    throw InvalidValueException.withValue(this.mode, modeValues);
  }

  LightGradient toReturn = LightGradient(
    points: copyOriginalValues
        ? originalPoints
            .map((point) =>
                point.copyWith(copyOriginalValues: copyOriginalValues))
            .toList()
        : (points ??
            this
                .points
                .map((point) =>
                    point.copyWith(copyOriginalValues: copyOriginalValues))
                .toList()),
    mode: copyOriginalValues ? originalMode : (mode ?? this.mode),
    modeValues: modeValues ?? List<String>.from(this.modeValues),
  );

  if (copyOriginalValues) {
    toReturn.points = points ??
        this
            .points
            .map((point) =>
                point.copyWith(copyOriginalValues: copyOriginalValues))
            .toList();
    toReturn.mode = mode ?? this.mode;
  }

  return toReturn;
}