text
stringlengths
1
372
),
<code_end>
error important
if a TextStyle object specifies a weight or style without a
corresponding font file, the engine uses a generic file for the font
and attempts to extrapolate outlines for the requested weight and style.
avoid relying on this capability. import the proper font file instead.
<topic_end>
<topic_start>
try the complete example
<topic_end>
<topic_start>
download fonts
download the raleway and RobotoMono font files from google fonts.
<topic_end>
<topic_start>
update the pubspec.yaml file
open the pubspec.yaml file at the root of your flutter project.
replace its contents with the following YAML.
<topic_end>
<topic_start>
use this main.dart file
open the main.dart file in the lib/ directory of your flutter project.
replace its contents with the following dart code.
<code_start>
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: 'custom fonts',
// set raleway as the default app font.
theme: ThemeData(fontFamily: 'raleway'),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
widget build(BuildContext context) {
return scaffold(
// the AppBar uses the app-default raleway font.
appBar: AppBar(title: const Text('Custom fonts')),
body: const center(
// this text widget uses the RobotoMono font.
child: text(
'roboto mono sample',
style: TextStyle(fontFamily: 'robotomono'),
),
),
);
}
}
<code_end>
the resulting flutter app should display the following screen.
<topic_end>
<topic_start>
export fonts from a package
rather than declaring a font as part of an app,
you can declare a font as part of a separate package.
this is a convenient way to share the same font across
several different projects,
or for coders publishing their packages to pub.dev.
this recipe uses the following steps:
info note
check out the google_fonts package for direct access
to almost 1000 open-sourced font families.
<topic_end>
<topic_start>
1. add a font to a package
to export a font from a package, you need to import the font files into the
lib folder of the package project. you can place font files directly in the
lib folder or in a subdirectory, such as lib/fonts.
in this example, assume you’ve got a flutter library called
awesome_package with fonts living in a lib/fonts folder.
<topic_end>
<topic_start>
2. add the package and fonts to the app
now you can use the fonts in the package by
updating the pubspec.yaml in the app’s root directory.
<topic_end>
<topic_start>
add the package to the app
to add the awesome_package package as a dependency,
run flutter pub add:
<topic_end>
<topic_start>
declare the font assets
now that you’ve imported the package, tell flutter where to
find the fonts from the awesome_package.
to declare package fonts, prefix the path to the font with
packages/awesome_package.
this tells flutter to look in the lib folder
of the package for the font.
<topic_end>
<topic_start>
3. use the font