-
Hi, I'm using flex_color_scheme with flutter_screenutil and when I try to apply a fontSizeFactor of 1.sp (scale the Font) I get the following error:
the code: ThemeData light = FlexThemeData.light(
scheme: mainFlexSchema,
surfaceMode: FlexSurfaceMode.levelSurfacesLowScaffold,
blendLevel: 13,
subThemesData: const FlexSubThemesData(
blendOnLevel: 20,
useTextTheme: true,
useM2StyleDividerInM3: true,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
);
ThemeData lightTheme = light.copyWith(
textTheme: light.textTheme.apply(fontSizeFactor: 1.sp),
iconTheme: light.iconTheme.copyWith(size: 24.sp),
); To me it seems that when I call the light.textTheme.apply(fontSizeFactor: 1.sp) light.textTheme is not initialized yet and so don't have any fontSize set. If i replace it with something like ThemeData lightTheme = light.copyWith(
textTheme: Typography.tall2021.apply(fontSizeFactor: 1.sp),
iconTheme: light.iconTheme.copyWith(size: 24.sp),
); it's working (but then the style is different :) ) Does someone already encounter that or someone could guide me through a solution? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
ok my bad, this is happening with default ThemeData, it seems that the way I'm doing it is not correct. Look like textTheme is initialized inside the materialApp and so I cannot apply before the fontSizeFactor. |
Beta Was this translation helpful? Give feedback.
-
Hi @JobiJoba, glad you noticed that, I was about to mention it too that this is the way ThemeData and TextTheme works, not really related to FlexColorScheme. The textTheme does not actually have any size information until it has passed the localization step in the setup of the MaterialApp. You can do what you are trying to do, if you add an extra Builder or a custom wrapper widget that all it does is apply the font size mods you want to add, in a new Depends a bit on what you are trying to accomplish with the The "terse" info about how to do a wrapper is here in the docs: But if I take your example, and create eg a wrapper between the It could look something like this: class TextThemeMod extends StatelessWidget {
const TextThemeMod({
super.key,
required this.themeMode,
required this.onThemeModeChanged,
});
final ThemeMode themeMode;
final ValueChanged<ThemeMode> onThemeModeChanged;
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
return Theme(
data: theme.copyWith(
textTheme: theme.textTheme.apply(fontSizeFactor: 1),
),
child: HomePage(
themeMode: themeMode,
onThemeModeChanged: onThemeModeChanged,
),
);
}
} Above I could control the demo.movHope this helps, perhaps you solved it already. This was a good question though and I though I would answer it and I would like to add it to the Q/A in repo Discussions, so other devs can locate it too. |
Beta Was this translation helpful? Give feedback.
-
Wow! Thank you for taking the time to look at it :) I paused the development of the app that needed this but implemented it right now to check and it works. It might give me motivation to go back onto it :) I'm "overriding" the fontSizeFactor so it scale depending on screen size without the need to set a scale everytime I'm using one of the textTheme property (ex HeadlineLarge.copyWith(fontSize: x.sp) (It's the recommended way in the docs of flutter_screenutil). |
Beta Was this translation helpful? Give feedback.
Hi @JobiJoba, glad you noticed that, I was about to mention it too that this is the way ThemeData and TextTheme works, not really related to FlexColorScheme.
The textTheme does not actually have any size information until it has passed the localization step in the setup of the MaterialApp.
You can do what you are trying to do, if you add an extra Builder or a custom wrapper widget that all it does is apply the font size mods you want to add, in a new
Theme
, basically wrapping everything with an extra theme layer. Don't worry about the overhead for this, many Flutter SDK widgets do this as well internally.Depends a bit on what you are trying to accomplish with the
fontSizeFactor
. Maybe so…