Build: Custom Fonts Demo
This screen shows text using a custom font loaded into the app.
Target UI
------------------------- | Custom Fonts Demo | |-----------------------| | | | Hello in Custom Font | | | |-----------------------|
------------------------- | Custom Fonts Demo | |-----------------------| | | | Hello in Custom Font | | | |-----------------------|
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 Demo', home: const CustomFontScreen(), ); } } class CustomFontScreen extends StatelessWidget { const CustomFontScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Custom Fonts Demo'), ), body: const Center( // TODO: Add Text widget here using custom font ), ); } }
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 Demo', theme: ThemeData( fontFamily: 'Lobster', ), home: const CustomFontScreen(), ); } } class CustomFontScreen extends StatelessWidget { const CustomFontScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Custom Fonts Demo'), ), body: const Center( child: Text( 'Hello in Custom Font', style: TextStyle(fontSize: 28), ), ), ); } } /* Additional steps outside this code: 1. Add font files (e.g., Lobster-Regular.ttf) into assets/fonts/ 2. Update pubspec.yaml: flutter: fonts: - family: Lobster fonts: - asset: assets/fonts/Lobster-Regular.ttf This setup makes the font family 'Lobster' available to use in the app. */
We added a custom font named 'Lobster' to the Flutter app by placing the font file in the assets folder and configuring pubspec.yaml. Then, we set the app's theme fontFamily to 'Lobster' so all text uses it by default. The main text on the screen uses this font and is centered with a larger font size for visibility.
This approach shows how to load and use custom fonts in Flutter, making your app's text look unique and styled.
------------------------- | Custom Fonts Demo | |-----------------------| | | | Hello in Custom Font | | | |-----------------------|
fonts:
- family: CoolFont
fonts:
- asset: assets/fonts/CoolFont-Regular.ttf
- asset: assets/fonts/CoolFont-Bold.ttf
weight: 700
What will this code display?
Text('Hi', style: TextStyle(fontFamily: 'CoolFont', fontWeight: FontWeight.bold))pubspec.yaml:
fonts:
- family: FancyFont
fonts:
- asset: assets/fonts/FancyFont.ttf
Dart code:
Text('Hello', style: TextStyle(fontFamily: 'FancyFontt'))Pass the test and this pattern moves unknown