Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter custom fonts not loading

Tags:

flutter

SOLVED: I'm still not sure what the problem was, but I fixed it by simply creating a new Flutter project, getting the fonts set up in the new project, and then just pasting my .dart files into the new project. Even though everything was exactly the same (as far as I can tell) it worked fine. Go figure.


I'm trying to use custom fonts in my app without success.

This is the fonts section of my pubspec.yaml file:

fonts:
  - family: Muli
    fonts:
      - asset:  fonts/Muli-Light.ttf

  - family: LibreBaskerville
    fonts:
      - asset:  fonts/LibreBaskerville-Regular.ttf

And in my app:

Widget _sectionTitle(String text) {
  return Container( 
  padding: EdgeInsets.fromLTRB(25.0, 0.0, 25.0, 5.0),
  child: Text(text,          
  style: new TextStyle(fontFamily: 'Muli', fontSize: 50.0),
  ));
  }
Widget _sectionText(String text){
    return Container (
    padding: EdgeInsets.fromLTRB(25.0, 0.0, 25.0, 15.0),
    child: Text(text, style: TextStyle(fontFamily: 'LibreBaskerville', fontSize: 20.0)),
    );
 }

Among the things I've tried (multiple times):

  • Deleting the installed file from the emulator
  • running from Android Studio vs running from VSCode
  • moving the .ttf files to different folders
  • downloading other fonts
  • using an online YAML validator
  • flutter clean

I'm assuming there's something amiss with the pubspec.yaml file, but I can't see what it might be.

Any suggestions would be much appreciated. Thx.

EDIT: Just tried creating a new default Flutter app with the same Pubspec.yaml font info. I added the custom font to the MaterialApp widget and it works ... so maybe the problem is not the yaml.

like image 446
LeeHunter Avatar asked Oct 26 '18 00:10

LeeHunter


People also ask

Does Flutter support variable fonts?

Flutter Variable Font Support The Dart UI font feature class was for the font feature flags and any int values, but variable fonts requires double values. This is why currently, if you attempt to use variable fonts, specific styling does not appear applied.


1 Answers

remove the app from the test device then do flutter clean, re-run

flutter clean
like image 72
Mohanad Obaid Avatar answered Nov 15 '22 10:11

Mohanad Obaid