4

Let's say i have added following lines to the pubspec.yaml file fonts:

fonts:
- family: GreatVibes
  fonts:
    - asset: fonts/GreatVibes-Regular.ttf
    - asset: fonts/GreatVibes-Bold.ttf

I am using it in my app with the following lines of code.

new Text('My New Font',
        style: new TextStyle(
          color: Colors.white,
          fontFamily: 'GreatVibes',
          fontSize: 16.0,
        )),

My question is that ,among the two .ttf files provided earlier, how does flutter decides which file to use?

And Let's say if flutter decides to use GreatVibes-Bold.ttf, what can i do to make it use GreatVibes-regular.ttf

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440

3 Answers3

4

If I understand these font things correctly - it has to be like this:

fonts:
- family: GreatVibes
  fonts:
    - asset: fonts/GreatVibes-Regular.ttf
    - asset: fonts/GreatVibes-Bold.ttf
      weight: 700
    - asset: fonts/GreatVibes-Italic.ttf
      style: italic

And then

new Text('My New Font',
    style: new TextStyle(
      color: Colors.white,
      fontFamily: 'GreatVibes',
      fontWeight: FontWeight.w700,
      fontSize: 16.0,
    )),
Andrii Turkovskyi
  • 27,554
  • 16
  • 95
  • 105
  • Sorry, it is just changing the weight of the font, what if i had placed italic style font in there, i want to give multiple font choices to the app user but if i have to specify 'family' for each of those fonts, it will become really hard to manage, that's the sole purpose of me asking this question. – hemant bansal Nov 09 '18 at 13:42
  • I've edited answer - add option for italic. I don't know how you can put many different fonts in one family and work with them like as list – Andrii Turkovskyi Nov 09 '18 at 13:51
  • yes, the fact is that we can use any name for family, it does not need to be same as the font's name, so if it has to choose b/w let say roboto and open sans, which of them will it choose – hemant bansal Nov 09 '18 at 14:13
0

you may use it directly into widget style,or using Theme object which i prefer to control overall app from one location, here an example for main.dart:

return MaterialApp(
  title: 'Flutter Demo',
  theme: ThemeData(
      primarySwatch: Colors.purple,
      accentColor: Colors.deepOrange,
      **fontFamily: 'Lato'**),

 ....
  },
);
bara batta
  • 1,002
  • 8
  • 8
-1

Checkout these links :

How to use a custom font style in flutter?

https://flutter.io/docs/cookbook/design/fonts

Sandeep Insan
  • 348
  • 1
  • 7