flutterfontsfont-family

how to use fontfamily properly in flutter


body: Center(
        child: SizedBox(
            width: 400,
            height: 400,
            child: Center(
                child: Row(
                  children: [
                    Image.asset('images/solider.png'),
                    const Text('Hello',style:TextStyle(fontFamily: 'IndieFlower'),)
                  ],
                )
            )
        ),
      ),

fontfamily is not affected

import 'package:flutter/material.dart';

I only include this


Solution

  • To use this font family you have to add the font ttf file as an asset. as if IndieFlower is a Google font. so, in this case, you can add a Google font package in pubspec.yaml instead of adding font ttf as an asset.

    add this package like :

    dependencies:
      google_fonts: ^5.1.0
    

    then where you want to use this font, import the library there like this:-

    import 'package:google_fonts/google_fonts.dart';
    

    then use googlefont.fontname instead of TextStyle like this:

    Text(
       'This is Google Fonts',
        style: GoogleFonts.indieFlower(),
      ),
    

    you will find all the text style properties inside of the () brackets.

    GoogleFonts.indieFlower(TextStyle property here)

    final code. will. be like this:

    import 'package:flutter/material.dart';
    import 'package:google_fonts/google_fonts.dart';
    
    class MyPageName extends StatelessWidget {
    const MyPageName({Key? key}) : super(key: key);
    
    @override
    Widget build(BuildContext context) {
      return Scaffold(
       body: Center(
        child: SizedBox(
          width: 400,
          height: 400,
          child: Center(
            child: Row(
              children: [
                Image.asset('images/solider.png'),
                Text(
                  'This is Google Fonts',
                  style: GoogleFonts.indieFlower(
                      fontSize: 14, color: Colors.black),
                    ),
                  ],
                 ),
               ),
             ),
           ),
         );
      }
    }
    

    Please let me know if you still getting any problem . if solved mark it as solved.

    Be careful: don't forget to add packages on pubspec.yaml file .

    google font package link: