How to install and use google_fonts with Flutter 3.10.2 - including code sample!
A popular Flutter package with 4753 likes that support Flutter version 3.10.2 and above. Last version of the package was published on Jun 16, 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: ^5.0.0
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.light(useMaterial3: true),
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;
late Future googleFontsPending;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
void initState() {
super.initState();
googleFontsPending = GoogleFonts.pendingFonts([
GoogleFonts.poppins(),
GoogleFonts.montserrat(fontStyle: FontStyle.italic),
]);
}
@override
Widget build(BuildContext context) {
final pushButtonTextStyle = GoogleFonts.poppins(
textStyle: Theme.of(context).textTheme.headlineMedium,
);
final counterTextStyle = GoogleFonts.montserrat(
fontStyle: FontStyle.italic,
textStyle: Theme.of(context).textTheme.displayLarge,
);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: FutureBuilder(
future: googleFontsPending,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const SizedBox();
}
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'You have pushed the button this many times:',
style: pushButtonTextStyle,
),
Text('$_counter', style: counterTextStyle),
],
);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}