In Flutter we can use TextStyle to provide a desired fontFamily property for the text.
While some fontFamily names are obvious and do work (like 'Arial', 'Courier', 'Times' and so on), where's the full list of available options?
The docs (https://api.flutter.dev/flutter/painting/TextStyle/fontFamily.html) don't help.
I do not think such a list is provided by the Flutter team. The fonts you mention are probably system default fonts. Flutter handles the use of custom fonts with a 'Custom Font Fallback'. Listed below is a tool snippet of how this goes to work:
Snippet
In the following example, any glyphs not present in the font Raleway will be attempted to be resolved with Noto Sans CJK SC, and then with Noto Color Emoji:
const TextStyle(
fontFamily: 'RaleWay',
fontFamilyFallback: <String>[
'Noto Sans CJK SC',
'Noto Color Emoji',
],
)
If all custom fallback font families are exhausted and no match was found
or no custom fallback was provided, the platform font fallback will be used.
Since you do not provide any FallBack fonts, Flutter automatically uses the the fonts default to your system of choice.
I hope this you understand what happens now! Please look at the documentation on how to add custom fonts:
Flutter font documentation
You don´t need to worry about the default fonts.
You can download a font that you like from here https://fonts.google.com/
Then add it to your assets folder, and update your pubspec.yaml
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
fonts:
- family: yourFont
fonts:
- asset: assets/fonts/yourFont.ttf
- family: otherFont
fonts:
- asset: assets/fonts/otherFont.ttf
After that, you can use it like this:
Text('Some text',
style: TextStyle(
color: Colors.blue,
fontFamily: 'yourFont',
fontSize: 22.0,
),
),
I recently encountered the same problem with fonts and Flutters incomplete documentation of font list.
A much easier solution for me was to import Google's font library:
//Import the font package
import 'package:google_fonts/google_fonts.dart';
//Use the Font package instead of TextStyle with your custom tweeks
Text('Sign Up',
style: GoogleFonts.poppins(
color: const Color.fromARGB(255, 59, 59, 61),
fontSize: 64,
fontWeight: FontWeight.w900,
),
),
And the list of fonts offered by this library is HUGE.
check them out with complete examples from here:
https://fonts.google.com/
Related
I am trying to use custom fonts in my flutter app, I did what i always do, and even trying to see many tutorials to see if i am missing something
1- added the ttf files to 'assets/fonts' (trying to upload an image but getting server error here)
2- added these to pubspec.yaml
uses-material-design: true
fonts:
- family: karashi
fonts:
- asset: assets/fonts/Karashi.ttf
- family: Hafs
fonts:
- asset: assets/fonts/Hafs.otf
- family: Qarwan
fonts:
- asset: assets/fonts/Qarwan.ttf
- family: suranames
fonts:
- asset: assets/fonts/suranames.ttf
3- used TextStyle to apply the font family
appBar: AppBar(
title: Text(
'استقلال ',
style: TextStyle(
fontFamily: 'Qarwan', color: Colors.red, fontSize: 40),
),
the result is that the font does not change and stays on the default font, only if I used 'Hafs' font or 'suranames' font these two work while the others don't.
I tried changing only the file name of 'Hafs' and put another file, but still wont work
I restarted the app, uninstalled then installed, restarted computer but nothing works
what could be the issue and how can I fix it?
thanks
I am using font-family called "FFShamelFamily". I added two sub-font families as shown in the picture below. When I run my app, it uses the first sub-fontFamily: "SansOneBook" only. I try to use fontWeight to make use of the second sub-fontFamily:"SansOneBold" but it doesn't show. I mean that I want to use both of the sub-FontFamily, the bold one for titles and the other one for any text in the app. How can I do it?
When using a custom font you need to specify which FontFamily to use on a TextStyle. Your “sub” font FFShamelFamily-SansOneBold is correctly set up in your pubspec.yaml as a weight variation of the same Font Family.
To use it in a textStyle you simply have to use the weight you set it to with the correct family name:
Text(
“Hello, world”,
style: TextStyle(
fontFamily: “FFShamelFamily”,
fontWeight: FontWeight.w900,
),
)
Please check the official flutter documentation for using custom fonts to learn more.
This is the code inside the .YML file
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- Images/
fonts:
- family: Pacifico
fonts:
- asset: Fonts/Pacifico-Regular.ttf
and this is the part that I added before losing connection and crashes.
Text(
'Abdulrahman Hejazi',
style: TextStyle(
fontSize: 25,
color: Colors.white,
fontWeight: FontWeight.bold,
fontFamily: 'Pacifico'
),
),
I noticed something, that If I build (run) the project from the start it crashes, but if I hot reload it, it works just fine.
you need to rename asset folder Fonts to lower case fonts because capital letters are not allowed in folder names
It may be either of the following:
Your font file name has space on it. If yes remove it.
Your font file name may be misspelled.
Your font file may be corrupted.Try adding new one.
PS. does it load the font when hot reload or it doesn't?
I'm using GoogleFonts api in my flutter app but right now I am setting the fontStyle for every text manually to GoogleFonts.roberto but I want to set that as default in ThemeData in main.dart. But the fontFamily: GoogleFonts.roberto throws an error saying expected an string value so how can I achive that?
You can use it like this to make or modify an entire text theme to use the "Roboto" font as mentioned in their official document:
MaterialApp(
theme:ThemeData(
textTheme: GoogleFonts.robotoTextTheme(
Theme.of(context).textTheme,
),
);
I am trying to add custom fonts in my flutter app. I have added fonts in pubspec.yaml
flutter:
fonts:
- family: Quicksand
fonts:
- asset: assets/fonts/Quicksand-Regular.ttf
Aslo I have added this in my Text widget
style: TextStyle(
color: Colors.white,
fontSize: 80.0,
fontFamily: 'Quicksand',
),
I am not getting any error, but my font is not getting applied.
Make sure you Re-Build the Project Again - Hot-Restart Won't work & do run flutter packages get
when you add the font to youre project. first, you must restart your app after that its work normally .its important not using Hot Reload in this case! for the first time