lerp method

TextStyle lerp (TextStyle a, TextStyle b, double t)

Interpolate between two text styles.

This will not work well if the styles don't set the same fields.

The t argument represents position on the timeline, with 0.0 meaning that the interpolation has not started, returning a (or something equivalent to a), 1.0 meaning that the interpolation has finished, returning b (or something equivalent to b), and values in between meaning that the interpolation is at the relevant point on the timeline between a and b. The interpolation can be extrapolated beyond 0.0 and 1.0, so negative values and values greater than 1.0 are valid (and can easily be generated by curves such as Curves.elasticInOut).

Values for t are usually obtained from an Animation<double>, such as an AnimationController.

If foreground is specified on either of a or b, both will be treated as if they have a foreground paint (creating a new Paint if necessary based on the color property).

If background is specified on either of a or b, both will be treated as if they have a background paint (creating a new Paint if necessary based on the backgroundColor property).

Implementation

static TextStyle lerp(TextStyle a, TextStyle b, double t) {
  assert(t != null);
  assert(a == null || b == null || a.inherit == b.inherit);
  if (a == null && b == null) {
    return null;
  }

  String lerpDebugLabel;
  assert(() {
    lerpDebugLabel = 'lerp(${a?.debugLabel ?? _kDefaultDebugLabel} ⎯${t.toStringAsFixed(1)}→ ${b?.debugLabel ?? _kDefaultDebugLabel})';
    return true;
  }());

  if (a == null) {
    return TextStyle(
      inherit: b.inherit,
      color: Color.lerp(null, b.color, t),
      backgroundColor: Color.lerp(null, b.backgroundColor, t),
      fontFamily: t < 0.5 ? null : b.fontFamily,
      fontFamilyFallback: t < 0.5 ? null : b.fontFamilyFallback,
      fontSize: t < 0.5 ? null : b.fontSize,
      fontWeight: FontWeight.lerp(null, b.fontWeight, t),
      fontStyle: t < 0.5 ? null : b.fontStyle,
      letterSpacing: t < 0.5 ? null : b.letterSpacing,
      wordSpacing: t < 0.5 ? null : b.wordSpacing,
      textBaseline: t < 0.5 ? null : b.textBaseline,
      height: t < 0.5 ? null : b.height,
      locale: t < 0.5 ? null : b.locale,
      foreground: t < 0.5 ? null : b.foreground,
      background: t < 0.5 ? null : b.background,
      decoration: t < 0.5 ? null : b.decoration,
      shadows: t < 0.5 ? null : b.shadows,
      decorationColor: Color.lerp(null, b.decorationColor, t),
      decorationStyle: t < 0.5 ? null : b.decorationStyle,
      decorationThickness: t < 0.5 ? null : b.decorationThickness,
      debugLabel: lerpDebugLabel,
    );
  }

  if (b == null) {
    return TextStyle(
      inherit: a.inherit,
      color: Color.lerp(a.color, null, t),
      backgroundColor: Color.lerp(null, a.backgroundColor, t),
      fontFamily: t < 0.5 ? a.fontFamily : null,
      fontFamilyFallback: t < 0.5 ? a.fontFamilyFallback : null,
      fontSize: t < 0.5 ? a.fontSize : null,
      fontWeight: FontWeight.lerp(a.fontWeight, null, t),
      fontStyle: t < 0.5 ? a.fontStyle : null,
      letterSpacing: t < 0.5 ? a.letterSpacing : null,
      wordSpacing: t < 0.5 ? a.wordSpacing : null,
      textBaseline: t < 0.5 ? a.textBaseline : null,
      height: t < 0.5 ? a.height : null,
      locale: t < 0.5 ? a.locale : null,
      foreground: t < 0.5 ? a.foreground : null,
      background: t < 0.5 ? a.background : null,
      shadows: t < 0.5 ? a.shadows : null,
      decoration: t < 0.5 ? a.decoration : null,
      decorationColor: Color.lerp(a.decorationColor, null, t),
      decorationStyle: t < 0.5 ? a.decorationStyle : null,
      decorationThickness: t < 0.5 ? a.decorationThickness : null,
      debugLabel: lerpDebugLabel,
    );
  }

  return TextStyle(
    inherit: b.inherit,
    color: a.foreground == null && b.foreground == null ? Color.lerp(a.color, b.color, t) : null,
    backgroundColor: a.background == null && b.background == null ? Color.lerp(a.backgroundColor, b.backgroundColor, t) : null,
    fontFamily: t < 0.5 ? a.fontFamily : b.fontFamily,
    fontFamilyFallback: t < 0.5 ? a.fontFamilyFallback : b.fontFamilyFallback,
    fontSize: ui.lerpDouble(a.fontSize ?? b.fontSize, b.fontSize ?? a.fontSize, t),
    fontWeight: FontWeight.lerp(a.fontWeight, b.fontWeight, t),
    fontStyle: t < 0.5 ? a.fontStyle : b.fontStyle,
    letterSpacing: ui.lerpDouble(a.letterSpacing ?? b.letterSpacing, b.letterSpacing ?? a.letterSpacing, t),
    wordSpacing: ui.lerpDouble(a.wordSpacing ?? b.wordSpacing, b.wordSpacing ?? a.wordSpacing, t),
    textBaseline: t < 0.5 ? a.textBaseline : b.textBaseline,
    height: ui.lerpDouble(a.height ?? b.height, b.height ?? a.height, t),
    locale: t < 0.5 ? a.locale : b.locale,
    foreground: (a.foreground != null || b.foreground != null)
      ? t < 0.5
        ? a.foreground ?? (Paint()..color = a.color)
        : b.foreground ?? (Paint()..color = b.color)
      : null,
    background: (a.background != null || b.background != null)
      ? t < 0.5
        ? a.background ?? (Paint()..color = a.backgroundColor)
        : b.background ?? (Paint()..color = b.backgroundColor)
      : null,
    shadows: t < 0.5 ? a.shadows : b.shadows,
    decoration: t < 0.5 ? a.decoration : b.decoration,
    decorationColor: Color.lerp(a.decorationColor, b.decorationColor, t),
    decorationStyle: t < 0.5 ? a.decorationStyle : b.decorationStyle,
    decorationThickness: ui.lerpDouble(a.decorationThickness ?? b.decorationThickness, b.decorationThickness ?? a.decorationThickness, t),
    debugLabel: lerpDebugLabel,
  );
}