How to embed webfont using font URL in flutter application - flutter

How to use font-URL to embed webfonts in flutter application ?
As per the requirement I can't use custom font as below :
flutter:
fonts:
- family: Raleway
fonts:
- asset: fonts/Raleway-Regular.ttf
Can anyone please let me know how to use fonts directly from URL ?

Because of flutter for web still in technical preview maybe in the future it won't work. But for now (in my case Flutter 1.10.14 dev) we can reference font from url as in a normal web page.
Add to the head section of index.html file from web folder:
<link href="https://fonts.googleapis.com/css?family=Raleway&display=swap" rel="stylesheet">
and reference font from code like:
Text( 'Example', style: TextStyle(fontFamily: 'Raleway'))

Related

Flutter not applying custom fonts

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

fonts on pubspec.yaml, doesn't display my font correctly

i recently change pubspec.yaml file but flutter can't display my font
fonts:
- family: Amatic
fonts:
- asset: assets/fonts/Amatic-Bold.ttf
- asset: assets/fonts/Amatic-Regular.ttf
inside your GetMaterialApp at the main file inside theme you should add
fontFamily: 'The name of your font in the assets'
.yaml files are case sensitive. So, you have to give us your pubspec.yaml file as it is.
If you are not formatting the content as it should be, you will no get what you expect. ‎ ‎ ‎

Using a custom icon font in a Flutter package displays Question marks instead of custom icons

I have an application composed of a number of different Dart and Flutter packages.
This has proven useful in that I've been able to move things like fonts, colours and Widgets into an independent style guide package which we can then use across all of our other applications.
Using custom fonts in a package works slightly differently in Flutter as we need to add the assets directory as a child of lib rather than a sibling:
…and then register these assets in pubspec.yaml like so:
assets:
- packages/receipt_widget/assets/fonts/my_font.ttf
// other asset declarations
This, for custom fonts and icons (png, jpg etc) works completely fine. My issue is that the same approach does not work for custom icon fonts.
Steps:
Generate custom icon font using IcoMoon or Flutter Icon
Place the generated font into the assets/fonts directory as demonstrated above
Place the generated dart file into the lib folder
Reference the custom icon in code and place into an Icon widget like so:
Icon(MyFontIcon.ic_heart);
For safety I then run flutter clean and uninstall the app from the device/ emulator before deploying a clean build.
I'm then left with a question mark rather than the referenced icon.
NB. the very same icons work correctly when used in a Flutter application directly rather than in a package.
To use your Flutter Icon font from a custom package, you can follow these steps:
(I repeated some of your steps already, for others who will find this issue)
create a flutter icon font
You can use flutter icon for that https://www.fluttericon.com
Put the file into your package
Make sure to copy the .ttf file into of the /lib folder.
NOT just assets, as you would do in your root project.
Example path:
/packages/my_awesome_fontpackage/lib/assets/MyIconFont.ttf
(see https://zubairehman.medium.com/how-to-use-custom-fonts-images-in-flutter-package-c2d9d4bfd47a )
Add the font to your project
Now open your package's pubspec.yaml file and add the font as an asset with package path:
flutter:
fonts:
- family: MyIconFont
fonts:
- asset: packages/my_awesome_fontpackage/assets/MyIconFont.ttf
(you might have to restart your app and bundling completely for the font to be loaded correctly and maybe run flutter clean just to be sure)
Now add package declaration to font dart file
Go into the my_icon_font.dart file and change the constant _kFontPkg there to your package name.
class MyIconFont {
MyIconFont._();
static const _kFontFam = 'MyIconFont';
static const String? _kFontPkg = 'my_awesome_fontpackage';
static const IconData bell = ...
....
}

Lost Connection to device as soon as I used custom fonts

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?

unable to add fonts in flutter

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