How to use Google Fonts in a Flutter application

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Loading custom fonts is often needed for websites, applications, and graphic design. Today we'll investigate how to load Google fonts inside a flutter application.
The end result will look like this:

If you want to work along with me, we start with the basic scaffolding app you can download from GitHub.
The first step is to load the package for google fonts.
To do so, add the Google fonts package in your pub spec.yml dependencies.
dependencies:
flutter:
sdk: flutter
#Load the Google fonts package
google_fonts: ^2.1.0
The next step is to import the font package into our dart file. Let's open up the lib/main.dart file and place the following import there.
import 'package:google_fonts/google_fonts.dart';
Now we can use any Google font we desire, but there are multiple options that we can use.
The most basic approach is to set the font on a specific Text widget. We already have one of those in our example, so let's pick a funky font and see it in action.
I'll be using the Pacifico font, because it will show you best how it works.
Now let's add this font as the style for our Text widget.
Text(
'Hello World π',
textDirection: TextDirection.ltr,
style: GoogleFonts.pacifico(fontSize: 48),
)
And that results in the following:

A pretty cool win already!
The same can be used to change the font of the app bar if you are using it.
appBar: AppBar(
title: Text(
'Testing Google Fonts',
style: GoogleFonts.pacifico(),
),
),
And it will look like this:

Another thing we could do is change the whole app theme font to be a Google font.
MaterialApp(
theme: ThemeData(
textTheme: GoogleFonts.pacificoTextTheme(),
),
)
This will change all the text elements in our main app into this google font as well!
So if we have our main text like this:
Text(
'Hello World π',
textDirection: TextDirection.ltr,
),
And that will result in:

Note: the AppBar is not changed here as the theme font won't change that by default!
Suppose you want to see how this works? Feel free to check it out on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter