apply method

TextStyle apply ({Color color, Color backgroundColor, TextDecoration decoration, Color decorationColor, TextDecorationStyle decorationStyle, double decorationThicknessFactor: 1.0, double decorationThicknessDelta: 0.0, String fontFamily, List<String> fontFamilyFallback, double fontSizeFactor: 1.0, double fontSizeDelta: 0.0, int fontWeightDelta: 0, double letterSpacingFactor: 1.0, double letterSpacingDelta: 0.0, double wordSpacingFactor: 1.0, double wordSpacingDelta: 0.0, double heightFactor: 1.0, double heightDelta: 0.0 })

Creates a copy of this text style replacing or altering the specified properties.

The non-numeric properties color, fontFamily, decoration, decorationColor and decorationStyle are replaced with the new values.

foreground will be given preference over color if it is not null and background will be given preference over backgroundColor if it is not null.

The numeric properties are multiplied by the given factors and then incremented by the given deltas.

For example, style.apply(fontSizeFactor: 2.0, fontSizeDelta: 1.0) would return a TextStyle whose fontSize is style.fontSize * 2.0 + 1.0.

For the fontWeight, the delta is applied to the FontWeight enum index values, so that for instance style.apply(fontWeightDelta: -2) when applied to a style whose fontWeight is FontWeight.w500 will return a TextStyle with a FontWeight.w300.

The numeric arguments must not be null.

If the underlying values are null, then the corresponding factors and/or deltas must not be specified.

If foreground is specified on this object, then applying color here will have no effect and if background is specified on this object, then applying backgroundColor here will have no effect either.

Implementation

TextStyle apply({
  Color color,
  Color backgroundColor,
  TextDecoration decoration,
  Color decorationColor,
  TextDecorationStyle decorationStyle,
  double decorationThicknessFactor = 1.0,
  double decorationThicknessDelta = 0.0,
  String fontFamily,
  List<String> fontFamilyFallback,
  double fontSizeFactor = 1.0,
  double fontSizeDelta = 0.0,
  int fontWeightDelta = 0,
  double letterSpacingFactor = 1.0,
  double letterSpacingDelta = 0.0,
  double wordSpacingFactor = 1.0,
  double wordSpacingDelta = 0.0,
  double heightFactor = 1.0,
  double heightDelta = 0.0,
}) {
  assert(fontSizeFactor != null);
  assert(fontSizeDelta != null);
  assert(fontSize != null || (fontSizeFactor == 1.0 && fontSizeDelta == 0.0));
  assert(fontWeightDelta != null);
  assert(fontWeight != null || fontWeightDelta == 0.0);
  assert(letterSpacingFactor != null);
  assert(letterSpacingDelta != null);
  assert(letterSpacing != null || (letterSpacingFactor == 1.0 && letterSpacingDelta == 0.0));
  assert(wordSpacingFactor != null);
  assert(wordSpacingDelta != null);
  assert(wordSpacing != null || (wordSpacingFactor == 1.0 && wordSpacingDelta == 0.0));
  assert(heightFactor != null);
  assert(heightDelta != null);
  assert(heightFactor != null || (heightFactor == 1.0 && heightDelta == 0.0));
  assert(decorationThicknessFactor != null);
  assert(decorationThicknessDelta != null);
  assert(decorationThickness != null || (decorationThicknessFactor == 1.0 && decorationThicknessDelta == 0.0));

  String modifiedDebugLabel;
  assert(() {
    if (debugLabel != null)
      modifiedDebugLabel = '($debugLabel).apply';
    return true;
  }());

  return TextStyle(
    inherit: inherit,
    color: foreground == null ? color ?? this.color : null,
    backgroundColor: background == null ? backgroundColor ?? this.backgroundColor : null,
    fontFamily: fontFamily ?? this.fontFamily,
    fontFamilyFallback: fontFamilyFallback ?? this.fontFamilyFallback,
    fontSize: fontSize == null ? null : fontSize * fontSizeFactor + fontSizeDelta,
    fontWeight: fontWeight == null ? null : FontWeight.values[(fontWeight.index + fontWeightDelta).clamp(0, FontWeight.values.length - 1)],
    fontStyle: fontStyle,
    letterSpacing: letterSpacing == null ? null : letterSpacing * letterSpacingFactor + letterSpacingDelta,
    wordSpacing: wordSpacing == null ? null : wordSpacing * wordSpacingFactor + wordSpacingDelta,
    textBaseline: textBaseline,
    height: height == null ? null : height * heightFactor + heightDelta,
    locale: locale,
    foreground: foreground,
    background: background,
    shadows: shadows,
    fontFeatures: fontFeatures,
    decoration: decoration ?? this.decoration,
    decorationColor: decorationColor ?? this.decorationColor,
    decorationStyle: decorationStyle ?? this.decorationStyle,
    decorationThickness: decorationThickness == null ? null : decorationThickness * decorationThicknessFactor + decorationThicknessDelta,
    debugLabel: modifiedDebugLabel,
  );
}