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}
)

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 {
  const 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,
}) {
  return TextTheme(
    headline1: headline1 ?? this.headline1,
    headline2: headline2 ?? this.headline2,
    headline3: headline3 ?? this.headline3,
    headline4: headline4 ?? this.headline4,
    headline5: headline5 ?? this.headline5,
    headline6: headline6 ?? this.headline6,
    subtitle1: subtitle1 ?? this.subtitle1,
    subtitle2: subtitle2 ?? this.subtitle2,
    bodyText1: bodyText1 ?? this.bodyText1,
    bodyText2: bodyText2 ?? this.bodyText2,
    caption: caption ?? this.caption,
    button: button ?? this.button,
    overline: overline ?? this.overline,
  );
}