copyWith method Null safety

TextTheme copyWith (
  1. {TextStyle? headline1,
  2. TextStyle? headline2,
  3. TextStyle? headline3,
  4. TextStyle? headline4,
  5. TextStyle? headline5,
  6. TextStyle? headline6,
  7. TextStyle? subtitle1,
  8. TextStyle? subtitle2,
  9. TextStyle? bodyText1,
  10. TextStyle? bodyText2,
  11. TextStyle? caption,
  12. TextStyle? button,
  13. TextStyle? overline,
  14. @Deprecated('This is the term used in the 2014 version of material design. The modern term is headline1. ' 'This feature was deprecated after v1.13.8.') TextStyle? display4,
  15. @Deprecated('This is the term used in the 2014 version of material design. The modern term is headline2. ' 'This feature was deprecated after v1.13.8.') TextStyle? display3,
  16. @Deprecated('This is the term used in the 2014 version of material design. The modern term is headline3. ' 'This feature was deprecated after v1.13.8.') TextStyle? display2,
  17. @Deprecated('This is the term used in the 2014 version of material design. The modern term is headline4. ' 'This feature was deprecated after v1.13.8.') TextStyle? display1,
  18. @Deprecated('This is the term used in the 2014 version of material design. The modern term is headline5. ' 'This feature was deprecated after v1.13.8.') TextStyle? headline,
  19. @Deprecated('This is the term used in the 2014 version of material design. The modern term is headline6. ' 'This feature was deprecated after v1.13.8.') TextStyle? title,
  20. @Deprecated('This is the term used in the 2014 version of material design. The modern term is subtitle1. ' 'This feature was deprecated after v1.13.8.') TextStyle? subhead,
  21. @Deprecated('This is the term used in the 2014 version of material design. The modern term is subtitle2. ' 'This feature was deprecated after v1.13.8.') TextStyle? subtitle,
  22. @Deprecated('This is the term used in the 2014 version of material design. The modern term is bodyText1. ' 'This feature was deprecated after v1.13.8.') TextStyle? body2,
  23. @Deprecated('This is the term used in the 2014 version of material design. The modern term is bodyText2. ' 'This feature was deprecated after v1.13.8.') TextStyle? body1}
)

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.

/// 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 {
  TitleColorThemeCopy({Key? key, required this.child, required this.titleColor}) : super(key: key);

  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(
          headline6: theme.textTheme.headline6!.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? headline1,
  TextStyle? headline2,
  TextStyle? headline3,
  TextStyle? headline4,
  TextStyle? headline5,
  TextStyle? headline6,
  TextStyle? subtitle1,
  TextStyle? subtitle2,
  TextStyle? bodyText1,
  TextStyle? bodyText2,
  TextStyle? caption,
  TextStyle? button,
  TextStyle? overline,
  @Deprecated(
    'This is the term used in the 2014 version of material design. The modern term is headline1. '
    'This feature was deprecated after v1.13.8.'
  )
  TextStyle? display4,
  @Deprecated(
    'This is the term used in the 2014 version of material design. The modern term is headline2. '
    'This feature was deprecated after v1.13.8.'
  )
  TextStyle? display3,
  @Deprecated(
    'This is the term used in the 2014 version of material design. The modern term is headline3. '
    'This feature was deprecated after v1.13.8.'
  )
  TextStyle? display2,
  @Deprecated(
    'This is the term used in the 2014 version of material design. The modern term is headline4. '
    'This feature was deprecated after v1.13.8.'
  )
  TextStyle? display1,
  @Deprecated(
    'This is the term used in the 2014 version of material design. The modern term is headline5. '
    'This feature was deprecated after v1.13.8.'
  )
  TextStyle? headline,
  @Deprecated(
    'This is the term used in the 2014 version of material design. The modern term is headline6. '
    'This feature was deprecated after v1.13.8.'
  )
  TextStyle? title,
  @Deprecated(
    'This is the term used in the 2014 version of material design. The modern term is subtitle1. '
    'This feature was deprecated after v1.13.8.'
  )
  TextStyle? subhead,
  @Deprecated(
    'This is the term used in the 2014 version of material design. The modern term is subtitle2. '
    'This feature was deprecated after v1.13.8.'
  )
  TextStyle? subtitle,
  @Deprecated(
    'This is the term used in the 2014 version of material design. The modern term is bodyText1. '
    'This feature was deprecated after v1.13.8.'
  )
  TextStyle? body2,
  @Deprecated(
    'This is the term used in the 2014 version of material design. The modern term is bodyText2. '
    'This feature was deprecated after v1.13.8.'
  )
  TextStyle? body1,
}) {
  assert(
    (headline1 == null && headline2 == null && headline3 == null && headline4 == null && headline5 == null && headline6 == null &&
     subtitle1 == null && subtitle2 == null &&
     bodyText1 == null && bodyText2 == null) ||
    (display4 == null && display3 == null && display2 == null && display1 == null && headline == null && title == null &&
     subhead == null && subtitle == null &&
     body2 == null && body1 == null), 'Cannot mix 2014 and 2018 terms in call to TextTheme.copyWith().');
  return TextTheme(
    headline1: headline1 ?? display4 ?? this.headline1,
    headline2: headline2 ?? display3 ?? this.headline2,
    headline3: headline3 ?? display2 ?? this.headline3,
    headline4: headline4 ?? display1 ?? this.headline4,
    headline5: headline5 ?? headline ?? this.headline5,
    headline6: headline6 ?? title ?? this.headline6,
    subtitle1: subtitle1 ?? subhead ?? this.subtitle1,
    subtitle2: subtitle2 ?? subtitle ?? this.subtitle2,
    bodyText1: bodyText1 ?? body2 ?? this.bodyText1,
    bodyText2: bodyText2 ?? body1 ?? this.bodyText2,
    caption: caption ?? this.caption,
    button: button ?? this.button,
    overline: overline ?? this.overline,
  );
}