apply method

TextStyle apply({
  1. Color? color,
  2. Color? backgroundColor,
  3. TextDecoration? decoration,
  4. Color? decorationColor,
  5. TextDecorationStyle? decorationStyle,
  6. double decorationThicknessFactor = 1.0,
  7. double decorationThicknessDelta = 0.0,
  8. String? fontFamily,
  9. List<String>? fontFamilyFallback,
  10. double fontSizeFactor = 1.0,
  11. double fontSizeDelta = 0.0,
  12. int fontWeightDelta = 0,
  13. FontStyle? fontStyle,
  14. double letterSpacingFactor = 1.0,
  15. double letterSpacingDelta = 0.0,
  16. double wordSpacingFactor = 1.0,
  17. double wordSpacingDelta = 0.0,
  18. double heightFactor = 1.0,
  19. double heightDelta = 0.0,
  20. TextBaseline? textBaseline,
  21. TextLeadingDistribution? leadingDistribution,
  22. Locale? locale,
  23. List<Shadow>? shadows,
  24. List<FontFeature>? fontFeatures,
  25. List<FontVariation>? fontVariations,
  26. String? package,
  27. TextOverflow? overflow,
})

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.

If the underlying values are null, then the corresponding factors and/or deltas must not be specified. Additionally, if height is kTextHeightNone it will not be modified by this method.

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,
  FontStyle? fontStyle,
  double letterSpacingFactor = 1.0,
  double letterSpacingDelta = 0.0,
  double wordSpacingFactor = 1.0,
  double wordSpacingDelta = 0.0,
  double heightFactor = 1.0,
  double heightDelta = 0.0,
  TextBaseline? textBaseline,
  TextLeadingDistribution? leadingDistribution,
  Locale? locale,
  List<Shadow>? shadows,
  List<FontFeature>? fontFeatures,
  List<FontVariation>? fontVariations,
  String? package,
  TextOverflow? overflow,
}) {
  assert(fontSize != null || (fontSizeFactor == 1.0 && fontSizeDelta == 0.0));
  assert(fontWeight != null || fontWeightDelta == 0.0);
  assert(letterSpacing != null || (letterSpacingFactor == 1.0 && letterSpacingDelta == 0.0));
  assert(wordSpacing != null || (wordSpacingFactor == 1.0 && wordSpacingDelta == 0.0));
  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 ?? _fontFamily,
    fontFamilyFallback: fontFamilyFallback ?? _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 ?? this.fontStyle,
    letterSpacing: letterSpacing == null ? null : letterSpacing! * letterSpacingFactor + letterSpacingDelta,
    wordSpacing: wordSpacing == null ? null : wordSpacing! * wordSpacingFactor + wordSpacingDelta,
    textBaseline: textBaseline ?? this.textBaseline,
    height: (height == null || height == ui.kTextHeightNone) ? height : height! * heightFactor + heightDelta,
    leadingDistribution: leadingDistribution ?? this.leadingDistribution,
    locale: locale ?? this.locale,
    foreground: foreground,
    background: background,
    shadows: shadows ?? this.shadows,
    fontFeatures: fontFeatures ?? this.fontFeatures,
    fontVariations: fontVariations ?? this.fontVariations,
    decoration: decoration ?? this.decoration,
    decorationColor: decorationColor ?? this.decorationColor,
    decorationStyle: decorationStyle ?? this.decorationStyle,
    decorationThickness: decorationThickness == null ? null : decorationThickness! * decorationThicknessFactor + decorationThicknessDelta,
    overflow: overflow ?? this.overflow,
    package: package ?? _package,
    debugLabel: modifiedDebugLabel,
  );
}