首页 > 解决方案 > Flutter TextStyle(不是TextTheme)apply()和copyWith()之间的区别

问题描述

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

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

TextTheme 中还有 2 个同名的方法。我理解他们,但无法理解 TextStyle 中的想法。

TextStyle 中这 2 个的逻辑与 TextTheme 中的不同

谢谢你。

标签: flutterflutter-layoutflutter-themetextstyle

解决方案


查看文档时,它显示apply如果您未指定某些参数,则使用默认值。

TextStyle apply({Color?color,Color?backgroundColor,TextDecoration?decoration,Color?decorationColor,TextDecorationStyle?decorationStyle,双decorationThicknessFactor = 1.0,双decorationThicknessDelta = 0.0,String?fontFamily,List?fontFamilyFallback,双fontSizeFactor = 1.0,双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?高度、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作为参数。


推荐阅读