merge method
- 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 {
const TitleColorTheme({super.key, required this.child, required this.titleColor});
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.
final TextTheme partialTheme = TextTheme(titleLarge: 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(
displayLarge: displayLarge?.merge(other.displayLarge) ?? other.displayLarge,
displayMedium: displayMedium?.merge(other.displayMedium) ?? other.displayMedium,
displaySmall: displaySmall?.merge(other.displaySmall) ?? other.displaySmall,
headlineLarge: headlineLarge?.merge(other.headlineLarge) ?? other.headlineLarge,
headlineMedium: headlineMedium?.merge(other.headlineMedium) ?? other.headlineMedium,
headlineSmall: headlineSmall?.merge(other.headlineSmall) ?? other.headlineSmall,
titleLarge: titleLarge?.merge(other.titleLarge) ?? other.titleLarge,
titleMedium: titleMedium?.merge(other.titleMedium) ?? other.titleMedium,
titleSmall: titleSmall?.merge(other.titleSmall) ?? other.titleSmall,
bodyLarge: bodyLarge?.merge(other.bodyLarge) ?? other.bodyLarge,
bodyMedium: bodyMedium?.merge(other.bodyMedium) ?? other.bodyMedium,
bodySmall: bodySmall?.merge(other.bodySmall) ?? other.bodySmall,
labelLarge: labelLarge?.merge(other.labelLarge) ?? other.labelLarge,
labelMedium: labelMedium?.merge(other.labelMedium) ?? other.labelMedium,
labelSmall: labelSmall?.merge(other.labelSmall) ?? other.labelSmall,
);
}