merge method

Implementation

WiredashTextTheme merge(WiredashTextTheme? other) {
  if (other == null) {
    return this;
  }

  TextStyle? mergeTextStyles(TextStyle? base, TextStyle? addition) {
    final baseFontFamily = base?.fontFamily;
    final additionFontFamily = addition?.fontFamily;

    final result = base?.merge(addition);

    // Removing package if addition changes the fontFamily
    // Bugfix for https://github.com/flutter/flutter/issues/108230
    final basePackage = baseFontFamily?.startsWith('packages/') == true
        ? baseFontFamily?.split('/').first
        : null;
    final additionPackage =
        additionFontFamily?.startsWith('packages/') == true
            ? baseFontFamily?.split('/').first
            : null;

    return TextStyle(
      color: result?.color,
      backgroundColor: result?.backgroundColor,
      fontSize: result?.fontSize,
      fontWeight: result?.fontWeight,
      fontStyle: result?.fontStyle,
      letterSpacing: result?.letterSpacing,
      wordSpacing: result?.wordSpacing,
      textBaseline: result?.textBaseline,
      height: result?.height,
      leadingDistribution: result?.leadingDistribution,
      locale: result?.locale,
      foreground: result?.foreground,
      background: result?.background,
      shadows: result?.shadows,
      fontFeatures: result?.fontFeatures,
      decoration: result?.decoration,
      decorationColor: result?.decorationColor,
      decorationStyle: result?.decorationStyle,
      decorationThickness: result?.decorationThickness,
      debugLabel: result?.debugLabel,
      fontFamilyFallback: result?.fontFamilyFallback,

      // Here starts the custom part of the TextStyle copyWith method
      fontFamily: additionFontFamily ?? baseFontFamily,
      package: () {
        if (additionFontFamily != null || additionPackage != null) {
          // Set addition.package when addition defines a fontFamily
          return additionPackage;
        }
        return basePackage;
      }(),
    );
  }

  return copyWith(
    headlineMedium: mergeTextStyles(headlineMedium, other.headlineMedium),
    headlineSmall: mergeTextStyles(headlineSmall, other.headlineSmall),
    appbarTitle: mergeTextStyles(appbarTitle, other.appbarTitle),
    title: mergeTextStyles(title, other.title),
    button: mergeTextStyles(button, other.button),
    bodyMedium: mergeTextStyles(bodyMedium, other.bodyMedium),
    bodySmall: mergeTextStyles(bodySmall, other.bodySmall),
    body2Medium: mergeTextStyles(body2Medium, other.body2Medium),
    body2Small: mergeTextStyles(body2Small, other.body2Small),
    caption: mergeTextStyles(caption, other.caption),
    input: mergeTextStyles(input, other.input),
    inputError: mergeTextStyles(inputError, other.inputError),
  );
}