How To Add Custom Font Family Using Flutter Android

admin_img Posted By Bajarangi soft , Posted On 06-10-2020

Custom font is a way to achieve it. Fonts can be very important in mobile applications. They make a text more readable and show more attention to various pieces of text. Fonts can make a format look bad or look great depending on how they are integrated into the application. Flutter comes with a few default fonts, however, we are also able to import custom font assets as we would like. After importing the fonts, we can directly call them in the dart code by passing a string to a property.

How To Add Custom Font Family Using Flutter Android

Add Custom Text Font Family 
Step 1: Open Google Fonts and search for a font family in the search bar (here “Pacifico”,"Robot",etc.).
Step 2: Select the “Pacifico” font file.
Step 3: To download, click the “Download Family” button.
Step 4: Open file manager and go to downloads. Extract and open the “Pacifico” zip file.
Step 5: Move the “Pacifico Regular” file into this directory. After moving, the font directory contains a file named “Pacifico-Regular.ttf”.
Step 6:
We cannot directly remove the time stamp from Custom Font Family but using the intl.dart package we can easily filter the date stamp from time stamp. So open your flutter project’s pubspec.yaml in code

dependencies:
  flutter:
    sdk: flutter
  google_fonts: ^0.3.8


flutter:
  fonts:
    - family: Pacifico
      fonts:
        - asset: fonts/Pacifico-Regular.ttf
    - family: Pacifico
      fonts:
        - asset: fonts/MontserratSubrayada-Regular.ttf
    - family: Pacifico
      fonts:
        - asset: fonts/FredokaOne-Regular.ttf

step 7:
After done saving the pubspec.yaml file, Open your flutter project root folder in Command Prompt or Terminal and execute flutter pub get command. 
flutter pub get

Step 8:
 Open your project’s main.dart file and import material.dart and    google_fonts: ^0.3.8. dart package.
import 'package:google_fonts/google_fonts.dart';

Complete Code FOr Custom Font Family In Flutter
Main.dart
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.indigo,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Custom Fonts'),
      ),
      body: Container(
        alignment: Alignment.topCenter,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Text(
              'Pacifico Regular',
              style: TextStyle(
                  fontFamily: 'Pacifico', fontSize: 32.0, color: Colors.pink),
            ),
            SizedBox(height: 20,),
            Text(
              'Pacifico Bold',
              style: TextStyle(
                  fontFamily: 'Pacifico',
                  fontWeight: FontWeight.bold,
                  fontSize: 32.0,
                  color: Colors.orange),
            ),
            SizedBox(height: 20,),
            Text(
              'MontserratSunbrayada',
              style: TextStyle(
                  fontFamily: 'MontserratSunbrayada',
                  fontWeight: FontWeight.bold,
                  fontSize: 22.0,
                  color: Colors.green),
            ),
            SizedBox(height: 20,),
            Text(
              'FredokaOne',
              style: TextStyle(
                  fontFamily: 'FradokaOne',
                  fontWeight: FontWeight.bold,
                  fontSize: 28.0,
                  color: Color(0xFFFFC107)),
            ),
            SizedBox(height: 20,),
            Text(
              'Hello world',
              style: GoogleFonts.pinyonScript(
                fontSize: 50,
                  color: Colors.lightBlue
              ),
            ),
            SizedBox(height: 20,),
            Text(
              'Anna meets Otto',
              style: TextStyle(
                fontFamily: 'Some handwriting font',
                fontSize: 30,
                  color: Colors.red
              ),
            ),
            SizedBox(height: 20,),
            Text(
              'Small Caps',
              style: TextStyle(
                fontSize: 30,
                  color: Colors.deepPurple
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Related Post