The article discusses how to create a single text theme that works for both Material-based and Cupertino-based platforms in Flutter, using custom fonts.
Abstract
The article begins by explaining the background of Flutter's platform-aware widgets and the Flutter Platform Widgets plugin. The author then demonstrates how to use the Google-specified MaterialBasedCupertinoThemeData function to create a single text theme that works for both Material-based and Cupertino-based platforms. The author also explains how to use custom fonts and the raw TextStyle calls to specify which text styles should be serif and which should be sans fonts. The article concludes with some additional resources and information about the author.
Opinions
The author believes that using the Flutter Platform Widgets plugin is the easiest way to have widgets auto-adjust to the platform the app is executing on.
The author prefers to use the raw TextStyle calls instead of the Google Fonts plugin theme helper to specify which text styles should be serif and which should be sans fonts.
The author believes that the core text theme is not the only text style that needs to be specified, and that the selectedlabel and unselectedlabel text styles also need to be specified.
The author provides some additional resources and information about themselves, including some useful guides and their contributions to Flutter plugins.
One Text Theme To Rule Them All
In Flutter, we have two types of platforms; Material-based and Cupertino-based. Let me show you the trick to write one text Theme that works for both platforms and how to use custom fonts.
Background
Google does cover this in the Flutter training docs. First, the Flutter team had just material widgets for both material-based and cupertino-based platforms. Then Google started implementing cupertino-based widgets.
Then some independent groups of Flutter developers came up with the concept of Platform Aware widgets.
This is why I contribute to the Flutter Platform Widgets plugin and why I use that plugin. It’s the easiest way to have the widgets auto-adjust to which platform the app is executing on, without a bunch of manually recreating the solution each time per app.
I flow my theme through that plugin in a very specific way:
Part of my one color theme trick is that I use a Google specified MaterialBasedCupertinoThemeData function to specify the Material theme I am using for the app and that construct re-populates the Cupertino widgets with the material based color properties of primary, primary variant, secondary, and secondary variant.
I can get one text theme for my custom fonts for both material and cupertino widget using the same trick as the text theme comes in at the base light and dark material themes, which the light theme is the one used in the MaterialBasedCupertinoThemeData:
Now onto the core text Theme.
The Core Text Theme
Here is my base text theme and their text styles:
Notice that I do not use the Google Fonts plugin theme helper, but just do the raw TextStyle calls instead. This way, I can specify which text styles I want as serif and which ones I want as sans fonts.
Conclusion
These are not the only text styles that have to be specified, as we still have the selectedlabel and unselectedlabel to specify as text styles. But, you get the general gist, how to set it up so that both Material and Cupertino widgets can use such text styles. The only difference is that you will have to apply those text styles at the component theme level.
About Me, Fred Grott
I am a reformed Android, Java, Kotlin, and Front-End developer. I am a reformed ADHD’er and Code and Design Cowboy.
And I am the nut starting to write a Flutter Dev App series of books during COIVD. Not only that, but I am organically creating my Flutter Developer Book chapters via Medium article writing.
The more formalized almost ready for book code is at:
My writing approach is somewhat different. The only way to master Flutter And Dart together is to do uncomfortable work. That is, every week to stretch me in mastering new things daily and expanding my thinking models tool-set.
Learning about state management means that I have to build state management demos with benchmarks fully naked and then build them with all the boilerplate including Flutter Platform Widgets and full testing.
With the UI itself, it means building a full UI catalog of UI solutions for col screen layout cases seen in top apps. It then means generating graphical layered pieces of screens for mockup tools.
On the CS-wise level, it means becoming an expert at reactive, including the advanced libraries. This intersects with state management, as specific reactive approaches have a role to play in statement management.
If you join me in building the exact stuff I am talking about, you will be Flutter Expert after building all this stuff.