Flutter TextStyle (not TextTheme) difference between apply() and copyWith()
Solution 1
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.
Solution 2
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
vietstone
Updated on December 01, 2022Comments
-
vietstone over 1 year
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.
-
vietstone over 2 yearsI'm still not sure the use case for each one.
-
vietstone over 2 yearsah ok, I get the idea. Thank you.
-
OverLoadedBurden over 2 yearsjust to make things clear, you saying that
apply
replaces all properties not provided with the default one, means not from the object ? if that so, then what's the point ? isn't this equivalent of creating a new object ??? -
Er1 over 2 years@OverLoadedBurden
apply
uses the default values if those are not provided. Not all of them have default values.copy
uses either the newly provided parameters or the already existing ones. I encourage you to take a look at the implementation in the doc links. They will make it clearer.