I have a package named Handwriter. It writes text in a custom font. I have the .ttf saved in lib/third_party/. In its pubspec.yaml, I add the font:
flutter:
uses-material-design: true
fonts:
- family: FancyHandwriting
fonts:
- asset: lib/third_party/FancyHandwriting-Regular.ttf
My app imports this package. In its pubspec.yaml, I add Handwriter as a dependency:
dependencies:
flutter:
sdk: flutter
handwriter:
path: ../handwriter
model:
path: ../model
However, the font does not show up at all when I use it in my app. Why?
final defaultStyle = TextStyle(
fontFamily: 'FancyHandwriting',
fontSize: 130);
Without having to re-add the fonts in the package consumer app, you can use:
final defaultStyle = TextStyle(
fontFamily: 'FancyHandwriting',
package: 'handwriter',
fontSize: 130,
);
Which is also equivalent to doing:
final defaultStyle = TextStyle(
fontFamily: 'packages/handwriter/FancyHandwriting',
fontSize: 130,
);
To use your Flutter Icon font from a custom package, you can follow these steps:
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)
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 = ...
....
}
According to this: https://flutter.dev/docs/cookbook/design/package-fonts you have to declare the package's font in your app's pubspec.yaml:
Declare the font assets
Now that you’ve imported the package, tell
Flutter where to find the fonts from the awesome_package.
To declare package fonts, prefix the path to the font with
packages/awesome_package. This tells Flutter to look in the lib folder
of the package for the font.
I.e.:
dependencies:
flutter:
sdk: flutter
handwriter:
path: ../handwriter
model:
path: ../model
flutter:
uses-material-design: true
fonts:
- family: FancyHandwriting
fonts:
- asset: packages/handwriter/third_party/FancyHandwriting-Regular.ttf
Note you're declaring the asset as being from the package - you don't have to make a copy of it in your app's lib.
The reasoning behind this is that not all fonts from every package may be used, so this trims down your final app's size.
Don't forget that you must also import/copy the fonts into your font assets directory. In your case make sure you place them in lib/third_party/. (See step 1 - https://flutter.dev/docs/cookbook/design/fonts).
Related
When creating a custom package that contains font as a .ttf file. I try to download the font file also in the example app that is contained in the package.
The font is downloaded successfully on iOS and macOS apps but not on Linux or Web.
The web console is giving me an error
Failed to load font My-Custom-Icons at assets/../lib/assets/fonts/My-Custom-Icons.ttf
...
my_package:
# When depending on this package from a real application you should use:
# custom_package: ^x.y.z
# See https://dart.dev/tools/pub/dependencies#version-constraints
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../
...
flutter:
uses-material-design: true
fonts:
- family: My-Custom-Icons
fonts:
- asset: ../lib/assets/fonts/My-Custom-Icons.ttf
I would expect that font would be downloaded the same way on all different platforms. What I'm missing here?
Hi just replace this and try.
- asset: assets/fonts/My-Custom-Icons.ttf
Ah, it seems that you can use the packages path when importing the font even inside the example. So the following did the work.
...
my_package:
# When depending on this package from a real application you should use:
# custom_package: ^x.y.z
# See https://dart.dev/tools/pub/dependencies#version-constraints
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../
...
flutter:
uses-material-design: true
fonts:
- family: My-Custom-Icons
fonts:
- asset: packages/my_package/assets/fonts/My-Custom-Icons.ttf
Page URL: [https://flutter.dev/docs/cookbook/design/package-fonts.html][1]
Page source: [https://github.com/flutter/website/tree/master/src/docs/cookbook/design/package-fonts.md][1]
Description of issue:
I've been trying to replicate the project Export fonts from a package from the Flutter.dev cookbook page. But I'm getting lost in the instructions. The instructions indicate that :
To export a font from a package, you need to import the font files into the lib folder of the package project.
The instructions also say that (I quote)
assume you’ve got a Flutter library called awesome_package with fonts living in a lib/fonts folder.
And the awesome_package package/library folder structure is defined as follow
awesome_package/
lib/
awesome_package.dart
fonts/
Raleway-Regular.ttf
Raleway-Italic.ttf
My question is : How do I create the awesome_package. Do I necessarily need to publish the package on the pub.dev ?
Please help me out
How do I create the awesome_package.
See more about Flutter packages and plugins
Do I necessarily need to publish the package on the pub.dev ?
No, you can use the package locally.
If you just want to use differents fonts, don't create a package for this, use this package google_fonts
Or you can use your own font
Docs
flutter:
fonts:
- family: font_name
fonts:
- asset: fonts/font_name1.ttf
- asset: fonts/font_name1.ttf
i was tring to change font in my flutter app. i downloaded my font in a folder called fonts and extracted it from google fonts then add it in pubspec.yaml file then i wrote that code in my main.dart
my main.dart
Text(
'My lovely app',
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
fontFamily: 'Pacifico',
),
),
my pubspec.yaml file
assets:
- assets/profile.jpg
what should i do ?
It seems that you didn't add any font from what you linked us from your pubspec.yaml
Following the official documentation on how to use a custom font, you need to add the font file (.ttf or .otf) in your asset folder then import it through your pubspec.yaml as such:
fonts:
- family: Raleway
fonts:
- asset: fonts/Raleway-Regular.ttf
- asset: fonts/Raleway-Italic.ttf
style: italic
Don't forget to restart the app with flutter run to see changes
I am working in a Flutter project and I want to use the following Adobe fonts, how can I do that?
https://fonts.adobe.com/fonts/myriad
https://fonts.adobe.com/fonts/myriad-arabic
You need to download this fonts and place it to assets/fonts folder at the root of your project. Next, add fonts to your pubspec.yaml file. For example:
flutter:
fonts:
- family: FontFamily
fonts:
- asset: fonts/FontFamily-Regular.ttf
style: regular
You can find more information in Official Documentation.
First, I added the google_fonts package to your pubspec dependencies.
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
google_fonts: ^0.2.0
Then
import 'package:google_fonts/google_fonts.dart';
and apply to a Text widget
Text(
'This is Google Fonts',
style: GoogleFonts.lato(fontSize: 40),
),
Text(
'This is Google Fonts',
style: GoogleFonts.adventPro(fontSize: 40),
),
Please check internet connection- your emulator does not have internet connectivity. google fonts need internet connection on device/emulator.
After adding the dependency pubspec.yaml file
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
google_fonts: ^0.2.0
run the command in terminal\cmd console as :
> flutter packages get
this will fetch the dependency into your workspace.
Google Fonts are fetched from the internet in the runtime. So you observe this behaviour
To overcome this, download the font from google font and make it available in the asset folder by following the steps below.
Visit the https://fonts.google.com/ and download the lato font.
At the root directory, create a directory called google_fonts.
Copy-Paste lato.ttf file into the google_fonts folder.
Open the pubspec.yaml file, Under the assets: section add the -google_fonts/
And change the code from
// Online Mode
Text(
'This is hammersmithOne from Google Font'
style: GoogleFonts.lato(),
),
to
// Offline Mode
Text(
'This is hammersmithOne from Google Font',
style: TextStyle(fontFamily: 'lato') // This is loaded from assets
),
For futher reference refer this article.
Refer these stackoverflow answers aswell:
https://stackoverflow.com/a/74949875/13431819
https://stackoverflow.com/a/74950261/13431819
I fixed mine by deleting the emulator and installing a new one. My older emulator was not connecting to the internet for some reason and reinstalling it fixed it for me.
Unless you store the fonts in your assets, you need to define explicitly that runtime fetching is enabled for Google Fonts:
GoogleFonts.config.allowRuntimeFetching = true;