I'm creating theme for my app.
I'm confusing these 2 methods (apply, copyWith) of TextStyle. What should be used?
There're also 2 methods with the same names in TextTheme. I understand them, but can not get the idea in TextStyle.
Logic of these 2 in TextStyle is different than in TextTheme
Thank you.
When looking at the docs it shows that apply
uses default values for some parameters if you don't specify them.
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
does not use default values and uses (copies) the values already defined in the original TextStyle object.
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
Edit: It also seems that they have different parameters, for example apply
doesn't have fontSize
and fontWeight
as a parameter.
apply() creates a copy of text style replacing all the specified properties in it.
copyWith()creates a copy of text style, but it only replaces the given values with new values
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.