merge method

TextTheme merge (TextTheme other)

Creates a new TextTheme where each text style from this object has been merged with the matching text style from the other object.

The merging is done by calling TextStyle.merge on each respective pair of text styles from this and the other text themes and is subject to the value of TextStyle.inherit flag. For more details, see the documentation on TextStyle.merge and TextStyle.inherit.

If this theme, or the other theme has members that are null, then the non-null one (if any) is used. If the other theme is itself null, then this TextTheme is returned unchanged. If values in both are set, then the values are merged using TextStyle.merge.

This is particularly useful if one TextTheme defines one set of properties and another defines a different set, e.g. having colors defined in one text theme and font sizes in another, or when one TextTheme has only some fields defined, and you want to define the rest by merging it with a default theme.

/// A Widget that sets the ambient theme's title text color for its
/// descendants, while leaving other ambient theme attributes alone.
class TitleColorTheme extends StatelessWidget {
  TitleColorTheme({Key key, this.child, this.titleColor}) : super(key: key);

  final Color titleColor;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    ThemeData theme = Theme.of(context);
    // This partialTheme is incomplete: it only has the title style
    // defined. Just replacing theme.textTheme with partialTheme would
    // set the title, but everything else would be null. This isn't very
    // useful, so merge it with the existing theme to keep all of the
    // preexisting definitions for the other styles.
    TextTheme partialTheme = TextTheme(title: TextStyle(color: titleColor));
    theme = theme.copyWith(textTheme: theme.textTheme.merge(partialTheme));
    return Theme(data: theme, child: child);
  }
}

See also:

Implementation

TextTheme merge(TextTheme other) {
  if (other == null)
    return this;
  return copyWith(
    display4: display4?.merge(other.display4) ?? other.display4,
    display3: display3?.merge(other.display3) ?? other.display3,
    display2: display2?.merge(other.display2) ?? other.display2,
    display1: display1?.merge(other.display1) ?? other.display1,
    headline: headline?.merge(other.headline) ?? other.headline,
    title: title?.merge(other.title) ?? other.title,
    subhead: subhead?.merge(other.subhead) ?? other.subhead,
    body2: body2?.merge(other.body2) ?? other.body2,
    body1: body1?.merge(other.body1) ?? other.body1,
    caption: caption?.merge(other.caption) ?? other.caption,
    button: button?.merge(other.button) ?? other.button,
    subtitle: subtitle?.merge(other.subtitle) ?? other.subtitle,
    overline: overline?.merge(other.overline) ?? other.overline,
  );
}