apply() 和 copyWith() 之间的 Flutter TextStyle(不是 TextTheme)区别

Flutter TextStyle (not TextTheme) difference between apply() and copyWith()

我正在为我的应用创建主题。

我混淆了 TextStyle 的这两种方法(apply、copyWith)。 应该使用什么?

TextTheme中还有2个同名方法。 我理解它们,但在 TextStyle 中无法理解。

这两个在 TextStyle 中的逻辑与在 TextTheme 中不同

谢谢。

查看文档时,它显示 apply 对某些参数使用默认值,如果您不指定它们的话。

TextStyle apply( {Color? color, Color? backgroundColor, TextDecoration? decoration, Color? decorationColor, TextDecorationStyle? decorationStyle, double decorationThicknessFactor = 1.0, double decorationThicknessDelta = 0.0, String? fontFamily, List? fontFamilyFallback, double fontSizeFactor = 1.0, double fontSizeDelta = 0.0, int fontWeightDelta = 0, FontStyle? fontStyle, double letterSpacingFactor = 1.0, double letterSpacingDelta = 0.0, double wordSpacingFactor = 1.0, double wordSpacingDelta = 0.0, double heightFactor = 1.0, double heightDelta = 0.0, TextBaseline? textBaseline, TextLeadingDistribution? leadingDistribution, Locale? locale, List? shadows, List? fontFeatures} )

https://api.flutter.dev/flutter/painting/TextStyle/apply.html

copywith 不使用默认值并使用(复制)原始 TextStyle 对象中已定义的值。

TextStyle copyWith( {bool? inherit, Color? color, Color? backgroundColor, String? fontFamily, List? fontFamilyFallback, double? fontSize, FontWeight? fontWeight, FontStyle? fontStyle, double? letterSpacing, double? wordSpacing, TextBaseline? textBaseline, double? height, TextLeadingDistribution? leadingDistribution, Locale? locale, Paint? foreground, Paint? background, List? shadows, List? fontFeatures, TextDecoration? decoration, Color? decorationColor, TextDecorationStyle? decorationStyle, double? decorationThickness, String? debugLabel} )

https://api.flutter.dev/flutter/painting/TextStyle/copyWith.html

编辑: 它们似乎也有不同的参数,例如 apply 没有 fontSizefontWeight 作为参数。

apply() 创建文本样式的副本,替换其中的 all 指定属性。

copyWith()创建文本样式的副本,但它只会用新值替换给定值