Reputation: 244
I'm trying to apply blue color on Theme.of(context).textTheme.headline6
, it can be done in two ways
Text(
"Hello",
style: Theme.of(context)
.textTheme
.headline6!
.copyWith(color: Colors.blue),
),
or
Text(
"Hello",
style: Theme.of(context)
.textTheme
.headline6!
.apply(color: Colors.blue),
),
Which one you prefer copyWith
or apply
method?
Upvotes: 11
Views: 4129
Reputation: 609
The main difference is that apply()
offers several parameters to scale parameters of the source TextStyle
.
For example, copyWith(fontSize: 14)
lets you explicitly set the font size, while apply(fontSizeFactor: 1.6)
lets you scale the source's font size by a custom factor (note that an explicit fontSize
parameter does not exist in apply()
).
Upvotes: 5
Reputation: 7222
apply()
creates a copy of text style replacing or altering the specified properties in it.
copyWith()
creates a copy of text style but with the given fields replaced with the new values.
source Flutter: Apply style as a Theme in a Text widget
Upvotes: 0