Closed GitGud31 closed 1 year ago
Hi @GitGud31,
Thanks for your question. It is a good one and fontFamily
can indeed be confusing. Let me try to explain why.
The observation and analyzer is of course correct, and even expected. There is no fontFamily
property in your lightFlexTheme
since it is a ThemeData
object.
The line:
final lightFlexTheme = FlexThemeData.light(..);
Returns a ThemeData
object and ThemeData
object does not have a property fontFamily
. This is a feature of Flutter SDK, not FlexColorScheme.
This can feel confusing, since the ThemeData()
factory has a ´fontFamilyparameter (and for convenience, so does
FlexThemeDataor actually the
FlexColorSchemeclass it uses), but the produced
ThemeDataobject does not have one. I don't know why the Flutter SDK chose to implement a factory that looks like a default unnamed object constructor and name the actual object constructor under
ThemeData.raw()`. It is certainly confusing.
The ThemeData()
factory, much like FlexColorScheme and its different ThemeData()
factories are basically just like a function that does its own logic to construct and return a ThemeData
object.
In the ThemeData()
factory, fontFamily
is a convenience parameter that can be used to easily configure the two TextTheme
objects textTheme
and primaryTextTheme
that are the actual properties in a ThemeData
object. When the ThemeData
object has been constructed, there is no longer a fontFamily
property in the actual ThemeData
object, only the actual properties it was used for, as a helper, to make.
In the resulting ThemeData
, on the properties textTheme
and primaryTextTheme
you can operate with copyWith
on ThemeData
, to modify them.
Be aware that there are many caveats when working with TextTheme
in ThemeData
, even more so when using a GoogleFonts
textTheme. However, FlexColorScheme
does contain a "fix" for making using GoogleFonts
default constructed TextTheme
s, like e.g. GoogleFonts.montserrat()
easier and work more as expected, but that does not apply to copyWith
on ThemeData
.
I suggest reading this Q&A answer to get up to speed on most of the TextTheme
and GoogleFonts
caveats:
https://github.com/rydmike/flex_color_scheme/discussions/160#discussioncomment-5999257
Hope this helps 😄 and happy theming!
BR Mike
PS. Converting this issue to a Q/A question as well. Maybe it will help others some day too if they find it there.
I'm trying to set different FamilyFonts for different Locales, when I try to use the lightFlexTheme.copyWith(fontFamily: desiredFont) I get an error that says 'fontFamily' is not defined.
am I not using it correctly ? is there a workaround for this ?