New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lerping between flex_color_scheme-generated themes causes error #9
Comments
Thanks Jack @Screendead for the report with the nice minimum reproducible test case. I think I know what might be causing it. I have seen some mention of a similar issue not related to FlexColorScheme elsewhere. I actually don't have access to a Mac or physical iOS device, but I think I can setup a test case in the used GitHub actions CI/CD and try to reproduce it there with a cloud Mac build. I should add Mac cloud builds to the final test pipe anyway. In any case, I might still need your help to verify that the fix works, I'll get back to you later on that. Did you try also with latest Flutter version 2.2.2? I don't think it actually matters for this though, just curious. |
Thanks @rydmike for your quick response! Issue persists on Flutter 2.2.2. On checking with an Android emulator, I can confirm the issue occurs on Android too. To reproduce, open the app, then go to settings and change the theme to dark mode, then switch back to the app and you'll see the error screen flash up briefly. Happy to help verify a fix. |
Hi @Screendead what you discovered is actually a bug (of sorts) in the Flutter SDK. I had noticed this myself too while developing FlexColorScheme. If you try to lerp between two light and dark ThemeData objects that uses text styles where the light and dark theme use different Typography you will get this error. I don't know if it is a bug in the SDK, or by design. Normally you probably don't want to use different Typography for your light and dark theme mode, so it might be this way by design too. In your example you are running into this situation because you are creating a light theme that uses Flutter SDK default In your example, if you replace: return MaterialApp(
darkTheme: FlexColorScheme.dark(
colors: FlexSchemeColor.from(primary: Colors.blue),
).toTheme, with return MaterialApp(
darkTheme: ThemeData.from(
colorScheme: ColorScheme.dark(primary: Colors.blue),
).copyWith(typography: Typography.material2018()), You get the same situation and same error with plan vanilla Flutter SDK themes, since it is duplicating the problematic case without usage of FlexColorScheme. A barebones minimum example of your setup that avoids the issue would be to apply the return MaterialApp(
darkTheme: FlexColorScheme.dark(
colors: FlexSchemeColor.from(primary: Colors.blue),
typography: Typography.material2014(),
).toTheme, Alternatively you can apply the return MaterialApp(
theme: ThemeData(typography: Typography.material2018()),
darkTheme: FlexColorScheme.dark(
colors: FlexSchemeColor.from(primary: Colors.blue),
).toTheme, In both those cases you can now lerp between the light and dark mode. The light and dark modes you get this way still have several different nuances, so I don't recommend matching light and dark mode As a solution for the example, that keeps everything looking nice and also working OK, I would recommend this: return MaterialApp(
theme: FlexColorScheme.light(
colors: FlexSchemeColor.from(primary: Colors.blue[700]!),
).toTheme,
darkTheme: FlexColorScheme.dark(
colors: FlexSchemeColor.from(primary: Colors.blue[300]!),
).toTheme, There is a brief mention of this Typography lerping issue in the example 5 tutorial walk through in the docs here: I will see what I can do to point it out more in the docs, that if you for some reason just define a dark theme with FlexColorScheme, that while you can, that you then need to adjust the Typography incompatibility with Flutter's old default ThemeData. The only reason Flutter uses the old typography by default is because Flutter dev team probably did not want to break a lot of legacy apps and tests. So they made the new correct Material typography something you have to configure and opt in manually to in your ThemeData setup. I on purpose decided to default to the newer Typography in FlexColorScheme. I was thinking that anybody that uses it, will use it to define both their light and dark theme data, and will not see this issue. 😄 There is not much I can do to fix the underlying SDK issue for the use case you presented. We could file an issue in the Flutter repo. It should in Flutter SDK be possible to take it into consideration via a typography check and skip lerping the light/dark text theme when they use different typography and just switch light/dark text theme without lerp for such use cases. This would be more robust SDK behavior imo. Still Flutter team would probably wonder what the use case is for using different typography in light and dark theme mode... 🤷🏻♂️ Conclusion |
It Works. return MaterialApp(
darkTheme: ThemeData.from(
colorScheme: ColorScheme.dark(primary: Colors.blue),
), |
Hi @MarnoDev, Yes that will work, because both the dark and the light theme, via default theme, use the same Typography in the above example. As shown in this Flutter SDK issue flutter/flutter#89947 (comment) This example: return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData.from(colorScheme: ColorScheme.light(primary: Colors.blue)),
darkTheme: ThemeData.from(
colorScheme: ColorScheme.dark(primary: Colors.blue),
).copyWith(typography: Typography.material2018()),
themeMode: ThemeMode.system,
home: MyHomePage(
title: 'Flutter Demo Home Page',
themeMode: ThemeMode.system,
),
); Will throw an assert error while switching theme mode using device system mode switcher. This is just the way Flutter works at the moment. Read more in the above still open issue on the topic. The setup that caused the issue discussed in this issue above, created the same underlying setup that also causes an issue in plain vanilla Flutter SDK. FlexColorScheme actually only produces a fancy ThemeData object, so it cannot really do anything to the app theme, that you cannot do and accomplish by constructing the same elaborate ThemeData object manually. |
Description
On iOS, switching between light mode and dark mode (CMD + SHIFT + A for iOS Simulator on macOS) with a flex_color_scheme theme for either
theme
ordarkTheme
will causeTextStyle.lerp
to fail. This doesn't occur when the vanillaThemeData.light()
andThemeData.dark()
default themes are used.Minimal Reproducible Example:
Error text
flutter doctor -v
:The text was updated successfully, but these errors were encountered: