3

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
  • 8,784
  • 16
  • 52
  • 79

2 Answers2

3

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.

Er1
  • 2,559
  • 1
  • 12
  • 24
  • I'm still not sure the use case for each one. – vietstone Aug 19 '21 at 09:41
  • ah ok, I get the idea. Thank you. – vietstone Aug 21 '21 at 19:11
  • just 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 ??? – OverLoadedBurden Jan 31 '22 at 11:52
  • @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. – Er1 Jan 31 '22 at 13:09
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