How to install and use google_fonts with Flutter 3.7.7 - including code sample!
A popular Flutter package with 4501 likes that support Flutter version 3.7.7 and above. Last version of the package was published on Mar 24, 2023.Installing google_fonts
You can either add the package directly using the flutter command line.
In either case, make sure to refresh your packages by running the following command:
$ flutter pub add google_fonts
dependencies:
google_fonts: ^4.0.3
In either case, make sure to refresh your packages by running the following command:
$ flutter pub get
Using google_fonts
Once you have installed the package, you can use it in your Flutter application by importing it.
import 'package:google_fonts/google_fonts.dart';
How to use google_fonts with a code example/sample
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'You have pushed the button this many times:',
style: GoogleFonts.alike(
textStyle: Theme.of(context).textTheme.displayMedium,
),
),
Text(
'$_counter',
style: GoogleFonts.lato(fontStyle: FontStyle.italic),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}