Abu Anwar
Abu Anwar

Reputation: 244

flutter apply or copyWith method

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?

Preview on image

Upvotes: 11

Views: 4129

Answers (2)

David Schneider
David Schneider

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

Hamed Rezaee
Hamed Rezaee

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

Related Questions