unable to add fonts in flutter - 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

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

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?

Full list of font families provided with Flutter?

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/

How to embed webfont using font URL in flutter application

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'))

Flutter icons do not show

I am trying to complete this Flutter Code Lab, but the icons do not appear in my application. But they appears in Android Studio code:
I have been included the lib in my file pubspec.yaml
name: startup_namer
description: A new Flutter application.
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.0
english_words: ^3.1.0
But result is this:
My main.dart file looks like this:
// Add the heart icons to the ListView.
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
.....
Widget _buildRow(WordPair pair) {
final bool alreadySaved = _saved.contains(pair);
return new ListTile(
title: new Text(
pair.asPascalCase,
style: _biggerFont,
),
trailing: new Icon(
alreadySaved ? Icons.favorite : Icons.favorite_border,
color: alreadySaved ? Colors.red : null,
size: 22.0,
),
onTap: () {
setState(() {
if (alreadySaved) {
_saved.remove(pair);
} else {
_saved.add(pair);
}
});
},
);
}
}
I thought that is because I use iOS emulator, but when I tried with Android emulator the result was the same.
(Copied from the comments)
You need to set uses-material-design: true in pubspec.yaml for the icons to be included in the app.
In my case, I have written uses-material-design: true in pubspec.yaml file. I am able to solve this problem by restarting the Android emulator. You can also try by uninstalling old build from your emulator and then installing the new one.
This is little weird behavior, but it works for me.
Hot reloading does not show changes in resources. Press the stop button square red color in the top action menu bar of android studio and run the app again.
It works.
Please try:
Stopping the app
Running flutter clean in your app directory
Deleting the app from your simulator / emulator / device
Rebuild & Deploy the app.
If you are using the FlutterIcons library, make sure that your font-family in your pubspec.yaml matches the one declared in the my_flutter_app_icons.dart file.
I assume we're talking about the flutter_icons package?
I had to kill the app entirely and reinstall it and they started working for me. Not sure why a hot reload didn't do the trick.
Just delete the App from emulator/physical device on which you are working and again build the app.
It works for me and will surely works for you.
Add the following to your pubspec.yaml:
flutter:
uses-material-design: true
Took me a while to figure out based on the answers above that uses-material-design is a property of flutter, not of the root.
Icon changing to Chinese character was resolved by adding the correct path to the fonts folder. Specify, the correct path to your fonts folder by providing proper indentations:
Flutter Clean
Remove Build From your testing device/Emulator
Run the app
You need to set uses-material-design: true in pubspec.yaml for the icons to be included in the app.
Be sure to have a uses-material-design: true entry in the flutter section of your pubspec. yaml file. It allows you to use the predefined set of Material icons. Many Material Design widgets need to be inside of a MaterialApp to display properly, in order to inherit theme data.
this was issue for me
Make sure in pubspec.yaml file all the images and fonts have correct path and if you have added new image recently try to remove that and then rebuild
The icons were loading normally on all devices except android for me. To fix it I simply:
Before
Icon(
Icons.chevron_left,
size: 40.0,
color: Colors.white,
),
After
Icon(
MdiIcons.chevronLeft,
size: 40.0,
color: Colors.white,
),
Steps
Add to pubspec yahml
dependencies:
material_design_icons_flutter: ^4.0.5955
Add this to the file you want the icon
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
Instead of Icons.NameHere use MdiIcon.NameHere
if all the above not worked then run
flutter pub run flutter_launcher_icons:main
The problem started in my project when I included my app within GetMaterialApp instead of MaterialApp Widget. All Material icons started to look like [X].