text stringlengths 1 474 |
|---|
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 |
Use a TextStyle to change the appearance of text. |
To use package fonts, declare which font you’d like to use and |
which package the font belongs to. |
<code_start>child: Text( |
'Using the Raleway font from the awesome_package', |
style: TextStyle( |
fontFamily: 'Raleway', |
), |
),<code_end> |
<topic_end> |
<topic_start> |
Complete example |
<topic_end> |
<topic_start> |
Fonts |
The Raleway and RobotoMono fonts were downloaded from |
Google Fonts.<topic_end> |
<topic_start> |
pubspec.yaml |
<topic_end> |
<topic_start> |
main.dart |
<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 const MaterialApp( |
title: 'Package Fonts', |
home: MyHomePage(), |
); |
} |
} |
class MyHomePage extends StatelessWidget { |
const MyHomePage({super.key}); |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
// The AppBar uses the app-default font. |
appBar: AppBar(title: const Text('Package Fonts')), |
body: const Center( |
// This Text widget uses the Raleway font. |
child: Text( |
'Using the Raleway font from the awesome_package', |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.