I wish to have a font management system of sorts, in which all of the usable fonts are in one single file that can be accessed by others instead of having to rewrite all the fonts for every TextStyle in the entire project. Here is such file:
class MyFonts {
final Color surfaceColor;
MyFonts(this.surfaceColor);
static List<TextStyle> _fonts = [
// style1Header
TextStyle(
fontFamily: 'Work Sans',
fontSize: 37.sp,
fontWeight: FontWeight.bold,
color: surfaceColor,
letterSpacing: 0.25.sp
),
... // many other fonts
Since this isn't a stateful widget, I have to send through the initializer the colour of the context of the page it is being called from using the line Theme.of(context).colorScheme.surfaceFf
.
The issue is that color: surfaceColor
throws an error:
The instance member 'surfaceColor' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression.
Is it wrong of me to send the theme of the surface as an argument? Is there another way to get the context of the app so that I don't have to send it as an argument? How can I fix this?
CodePudding user response:
The color in the static style declaration doesn't make sense. The fonts should be able to have different colors, thus every font needs its own variable. This is what you already did. Delete the line from the static style and apply the color for every font individually using copyWith:
style: _fonts.copyWith(color: surfaceColor)
I don't have the rest of your code, but this should be where you used _fonts
before.