How to use Siyuan Song font style.
First of all, let’s understand what resource material management is. Simply put, it is to facilitate us to manage various resource files, such as pictures, fonts, audio, etc. In Flutter, we can use files to manage our resources.
Okay, let’s get back to the topic. Now let’s take a look at how to implement the Siyuan Song font style. Siyuan Songyi is an open source font. It is an optimization and improvement of traditional Songyi fonts, so using it in our applications can make our fonts more beautiful.
Introduce font files
The first step is that we need to introduce Siyuan Song font into the file. We can implement it through the following code:
fonts: - family: SourceHanSerif fonts: - asset: fonts/
Here, we define a font group called SourceHanSerif and then introduce the font file. Please note that the file path here is relative to the file path.
Specify font style
In the second step, we need to use this font in the application. We can specify the name of the font group in TextStyle, like this:
TextStyle( fontFamily: 'SourceHanSerif', fontWeight: , fontSize: 20,)
Here, we specify the name of the font family as SourceHanSerif, and then specify the font thickness, size and other attributes.
OK, now let's take a look at the complete code:
fonts: - family: SourceHanSerif fonts: - asset: fonts/ Text('Hello, Flutter!', style: TextStyle( fontFamily: 'SourceHanSerif', fontWeight: , fontSize: 20,),)
Huh? This font doesn't seem to change? Don't worry, we need one more step.
Load font files
In the third step, we need to load our fonts first when the application is started. We can implement it through the following code:
void main() async { // Load fonts await loadFonts(); runApp(MyApp()); } Future<void> loadFonts() async { await ([ // Load Siyuan Song font ('fonts/').then( (data) { final font = FontLoader('SourceHanSerif'); ((data)); return (); }, ), ]); }
Here, we used rootBundle to load our font file. We then use FontLoader to load the font and add it to the font cache. Please note that we need to use the Future returned by FontLoader as a parameter so that the application can wait for the font to load before starting.
OK, now let's run the application again to see if Siyuan Songti is effective.
void main() async { // Load fonts await loadFonts(); runApp(MyApp()); } Future<void> loadFonts() async { await ([ // Load Siyuan Song font ('fonts/').then( (data) { final font = FontLoader('SourceHanSerif'); ((data)); return (); }, ), ]); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: , ), home: Scaffold( appBar: AppBar( title: Text('Siyuan Songyi'), ), body: Center( child: Text( 'Hello, Flutter!', style: TextStyle( fontFamily: 'SourceHanSerif', fontWeight: , fontSize: 20, ), ), ), ), ); } }
The font has changed! Now our application uses Siyuan Song font. Next, you can use this font as you like in the app.
To sum up, there are three steps to implement Siyuan Song font style:Introduce font files、Specify font style、Load font files. Here we need to pay special attention to the setting of font file paths and the use of FontLoader.
Okay, I think this article should have made you easy to master how to use Siyuan Songtv fonts in Flutter application. For more information about Flutter Siyuan Songtv UI, please follow my other related articles!