Fonts in Flutter Application

Fonts in Flutter Application

Fonts directly impact the user experience of the application. In Flutter, we can also use our font selection to improve overall application UI/UX.

Let's start with the minimal setup for showing an app screen (main.dart).

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Fonts',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({
    super.key,
  });

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Flutter Font Demo'),
      ),
      body: Container(
        padding: const EdgeInsets.all(16),
        child: const SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                _lorem,
                style: TextStyle(fontSize: 18),
              ),
            ],
          ),
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

const _lorem =
    "Lorem Ipsum is simply dummy text of the printing and typesetting industry. "
    "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, "
    "when an unknown printer took a galley of type and scrambled it to make a type specimen book. "
    "It has survived not only five centuries, but also the leap into electronic typesetting, "
    "remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset "
    "sheets containing Lorem Ipsum passages, and more recently with desktop publishing software "
    "like Aldus PageMaker including versions of Lorem Ipsum.";

The above code contains a home page with the text content with the default font Roboto used by flutter and font size 18 as below.

Flutter App

Step 1: Font Selection

Let's, choose one of the fonts from htpps://fonts.google.com for demonstration purposes. I am using Montserrat font for now.

Google fonts directory

Download and paste the fonts to the assets folder. Create the folder at root level of the project if it does not exist.

Font location

Step 2: Define Fonts

To include these fonts in our application we need to define fonts in pubspec.yaml file. It also contains the example usage to include the fonts in the comment.

# To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

Let's replace the commented section to match our font usage. I am using regular, italic, and bold font only for now. You can define other font variants too.

name: fonts_flutter
description: A new Flutter project.

# Prevent accidental publishing to pub.dev.
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: '>=3.0.6 <4.0.0'

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^2.0.0

flutter:
  uses-material-design: true

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

Step 3: Setting Fonts Globally

Let's use the font as a part of the application theme to use it globally in every text. In MyApp of main.dart, add fontFamily parameter to the theme like below.

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Fonts',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
        fontFamily: 'Montserrat'
      ),
      home: const HomePage(),
    );
  }
}

Let's see the font in action as shown below screen.

Montserrat Font

More Font Variants

Let's add italic and bold text styles by defining text styles in HomePage inside main.dart.

class HomePage extends StatefulWidget {
  const HomePage({
    super.key,
  });

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Flutter Font Demo'),
      ),
      body: Container(
        padding: const EdgeInsets.all(16),
        child: const SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(height: 20),
              Text(
                "Regular\n"
                    "$_quickBrownFox",
                style: TextStyle(fontSize: 18),
              ),
              SizedBox(height: 20),
              Text(
                "Italic\n"
                "$_quickBrownFox",
                style: TextStyle(fontSize: 18, fontStyle: FontStyle.italic),
              ),
              SizedBox(height: 20),
              Text(
                "Bold\n"
                "$_quickBrownFox",
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

const _quickBrownFox = "The quick brown fox jumps over the lazy dog";

The following result is obtained. To use more options like thin, medium, semi-bold, black, italic, bold italic, etc. can be configured from the pubspec.yaml.

Regular, Italic and Bold Montserrat

Multiple Font Family

We can also use different fonts in the same application overriding the default font set on the application theme. Let's download (Raleway) and define the font in pubspec.yaml.

name: fonts_flutter
description: A new Flutter project.

# Prevent accidental publishing to pub.dev.
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: '>=3.0.6 <4.0.0'

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^2.0.0

flutter:
  uses-material-design: true

  fonts:
    - family: Montserrat
      fonts:
        - asset: assets/fonts/Montserrat-Regular.ttf
        - asset: assets/fonts/Montserrat-Italic.ttf
          style: italic
        - asset: assets/fonts/Montserrat-Bold.ttf
          weight: 700
    - family: Raleway
      fonts:
        - asset: assets/fonts/Raleway-Regular.ttf
        - asset: assets/fonts/Raleway-Italic.ttf
          style: italic
        - asset: assets/fonts/Raleway-Bold.ttf
          weight: 700

Let's implement the font usage for both Montserrat and Raleway fonts.

class HomePage extends StatefulWidget {
  const HomePage({
    super.key,
  });

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Flutter Font Demo'),
      ),
      body: Container(
        padding: const EdgeInsets.all(16),
        child: const SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(height: 20),
              Text(
                "Monserrat Regular\n"
                "$_quickBrownFox",
                style: TextStyle(fontSize: 18),
              ),
              SizedBox(height: 20),
              Text(
                "Monserrat Italic\n"
                "$_quickBrownFox",
                style: TextStyle(fontSize: 18, fontStyle: FontStyle.italic),
              ),
              SizedBox(height: 20),
              Text(
                "Monserrat Bold\n"
                "$_quickBrownFox",
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              SizedBox(height: 20),
              Divider(),
              SizedBox(height: 20),
              Text(
                "Raleway Regular\n"
                    "$_quickBrownFox",
                style: TextStyle(fontSize: 18, fontFamily: 'Raleway'),
              ),
              SizedBox(height: 20),
              Text(
                "Raleway Italic\n"
                    "$_quickBrownFox",
                style: TextStyle(fontSize: 18, fontStyle: FontStyle.italic, fontFamily: 'Raleway'),
              ),
              SizedBox(height: 20),
              Text(
                "Raleway Bold\n"
                    "$_quickBrownFox",
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, fontFamily: 'Raleway'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

const _quickBrownFox = "The quick brown fox jumps over the lazy dog";

The result from the above code is here.

Montserrat and Railway Fonts


Bonus: Google Fonts

We need to choose external packages very carefully. It might impact our development process and application performance. If the above fonts implementation does not meet your requirements, you can take a look at the google fonts package which is similar to our implementation and requires other lines of code.

I hope this article was helpful to you. Thank you!