Adding fonts to the Flutter application is quite simple.
Configuration
Create a fonts folder in the project and push the font file that you want to use.
Let's add the fonts to the project's configuration file.
# pubspec.yaml
...
# The following section is specific to Flutter.
flutter:
...
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
fonts:
- family: Sans
fonts:
- asset: fonts/NotoSans-Regular.ttf
weight: 400
Creating Widget for Fonts
Create a file for the font widget.
import 'package:flutter/material.dart';
class FontWidgetApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _FontWidgetApp();
}
}
class _FontWidgetApp extends State<FontWidgetApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Font Widget')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
Text('Hello Flutter',
style: TextStyle(
fontFamily: 'Sans', fontSize: 30, color: Colors.blue
),
),
],
),
),
);
}
}
Fonts are used in the style of Text() widget.
Rendering Texts
Let's use this widget.
// lib/main.dart
import 'package:flutter/material.dart';
import 'fontWidget.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: 'Calculator',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FontWidgetApp()
);
}
}
'Dart (Flutter)' 카테고리의 다른 글
[Flutter] Navigation (0) | 2022.03.30 |
---|---|
[Flutter] How to Use Tab Bar Widget (0) | 2022.03.27 |
[Flutter] Adding Image to Flutter Application (0) | 2022.03.26 |
[Flutter] Life Cycle (0) | 2022.03.23 |
[Dart] Data Communication with Stream (0) | 2022.03.20 |
댓글