copyWith method Null safety

TextTheme copyWith(
  1. {TextStyle? displayLarge,
  2. TextStyle? displayMedium,
  3. TextStyle? displaySmall,
  4. TextStyle? headlineLarge,
  5. TextStyle? headlineMedium,
  6. TextStyle? headlineSmall,
  7. TextStyle? titleLarge,
  8. TextStyle? titleMedium,
  9. TextStyle? titleSmall,
  10. TextStyle? bodyLarge,
  11. TextStyle? bodyMedium,
  12. TextStyle? bodySmall,
  13. TextStyle? labelLarge,
  14. TextStyle? labelMedium,
  15. TextStyle? labelSmall,
  16. @Deprecated('Use displayLarge instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? headline1,
  17. @Deprecated('Use displayMedium instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? headline2,
  18. @Deprecated('Use displaySmall instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? headline3,
  19. @Deprecated('Use headlineMedium instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? headline4,
  20. @Deprecated('Use headlineSmall instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? headline5,
  21. @Deprecated('Use titleLarge instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? headline6,
  22. @Deprecated('Use titleMedium instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? subtitle1,
  23. @Deprecated('Use titleSmall instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? subtitle2,
  24. @Deprecated('Use bodyLarge instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? bodyText1,
  25. @Deprecated('Use bodyMedium instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? bodyText2,
  26. @Deprecated('Use bodySmall instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? caption,
  27. @Deprecated('Use labelLarge instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? button,
  28. @Deprecated('Use labelSmall instead. ' 'This feature was deprecated after v3.1.0-0.0.pre.') TextStyle? overline}
)

Creates a copy of this text theme but with the given fields replaced with the new values.

Consider using Typography.black or Typography.white, which implement the typography styles in the Material Design specification, as a starting point.

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

  final Color titleColor;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);
    return Theme(
      data: theme.copyWith(
        textTheme: theme.textTheme.copyWith(
          titleLarge: theme.textTheme.titleLarge!.copyWith(
            color: titleColor,
          ),
        ),
      ),
      child: child,
    );
  }
}

See also:

  • merge is used instead of copyWith when you want to merge all of the fields of a TextTheme instead of individual fields.

Implementation

TextTheme copyWith({
  TextStyle? displayLarge,
  TextStyle? displayMedium,
  TextStyle? displaySmall,
  TextStyle? headlineLarge,
  TextStyle? headlineMedium,
  TextStyle? headlineSmall,
  TextStyle? titleLarge,
  TextStyle? titleMedium,
  TextStyle? titleSmall,
  TextStyle? bodyLarge,
  TextStyle? bodyMedium,
  TextStyle? bodySmall,
  TextStyle? labelLarge,
  TextStyle? labelMedium,
  TextStyle? labelSmall,
  @Deprecated(
    'Use displayLarge instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? headline1,
  @Deprecated(
    'Use displayMedium instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? headline2,
  @Deprecated(
    'Use displaySmall instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? headline3,
  @Deprecated(
    'Use headlineMedium instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? headline4,
  @Deprecated(
    'Use headlineSmall instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? headline5,
  @Deprecated(
    'Use titleLarge instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? headline6,
  @Deprecated(
    'Use titleMedium instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? subtitle1,
  @Deprecated(
    'Use titleSmall instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? subtitle2,
  @Deprecated(
    'Use bodyLarge instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? bodyText1,
  @Deprecated(
    'Use bodyMedium instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? bodyText2,
  @Deprecated(
    'Use bodySmall instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? caption,
  @Deprecated(
    'Use labelLarge instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? button,
  @Deprecated(
    'Use labelSmall instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
  TextStyle? overline,
}) {
  assert(
    (displayLarge == null && displayMedium == null && displaySmall == null && headlineMedium == null &&
        headlineSmall == null && titleLarge == null && titleMedium == null && titleSmall == null &&
        bodyLarge == null && bodyMedium == null && bodySmall == null && labelLarge == null && labelSmall == null) ||
    (headline1 == null && headline2 == null && headline3 == null && headline4 == null &&
        headline5 == null && headline6 == null && subtitle1 == null && subtitle2 == null &&
        bodyText1 == null && bodyText2 == null && caption == null && button == null && overline == null),
    'Cannot mix 2018 and 2021 terms in call to TextTheme() constructor.'
  );
  return TextTheme(
    displayLarge: displayLarge ?? headline1 ?? this.displayLarge,
    displayMedium: displayMedium ?? headline2 ?? this.displayMedium,
    displaySmall: displaySmall ?? headline3 ?? this.displaySmall,
    headlineLarge: headlineLarge ?? this.headlineLarge,
    headlineMedium: headlineMedium ?? headline4 ?? this.headlineMedium,
    headlineSmall: headlineSmall ?? headline5 ?? this.headlineSmall,
    titleLarge: titleLarge ?? headline6 ?? this.titleLarge,
    titleMedium: titleMedium ?? subtitle1 ?? this.titleMedium,
    titleSmall: titleSmall ?? subtitle2 ?? this.titleSmall,
    bodyLarge: bodyLarge ?? bodyText1 ?? this.bodyLarge,
    bodyMedium: bodyMedium ?? bodyText2 ?? this.bodyMedium,
    bodySmall: bodySmall ?? caption ?? this.bodySmall,
    labelLarge: labelLarge ?? button ?? this.labelLarge,
    labelMedium: labelMedium ?? this.labelMedium,
    labelSmall: labelSmall ?? overline ?? this.labelSmall,
  );
}