Flutter Text and Typography
Text is one of the most frequently used UI elements in Flutter. Flutter provides the Text widget for displaying strings and the TextStyle class for controlling typography such as font size, font weight, color, font family, alignment, spacing, decoration, line height, and shadows. The Text widget can also control wrapping, overflow, maximum lines, and text scaling. :contentReference[oaicite:0]{index=0}
1. What Is Text in Flutter?
The Text widget displays a string of text in a Flutter application. It is commonly used for headings, labels, descriptions, buttons, messages, titles, prices, usernames, and other textual information.
Basic Example
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(
home: Scaffold(
body: Center(
child: Text('Hello Flutter'),
),
),
);
}
}
2. Understanding the Text Widget
The basic syntax of the Text widget is:
Text(
'Your text here',
)
A Text widget can be customized using properties such as style, textAlign, maxLines, overflow, softWrap, and textScaler. :contentReference[oaicite:1]{index=1}
Example
Text(
'Welcome to Flutter Development',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
)
3. What Is Typography?
Typography refers to the visual presentation and arrangement of text. Good typography makes an application easier to read, more consistent, and visually organized.
In Flutter, typography can be controlled through TextStyle, TextTheme, ThemeData, and related text widgets.
Important Typography Elements
- Font family
- Font size
- Font weight
- Font style
- Text color
- Text alignment
- Line height
- Letter spacing
- Word spacing
- Text decoration
- Text shadows
- Text overflow
- Text wrapping
4. TextStyle
TextStyle is used to describe how text should be formatted and rendered. It provides properties for font size, weight, style, color, family, spacing, decoration, shadows, line height, and more. :contentReference[oaicite:2]{index=2}
Basic TextStyle Example
Text(
'Flutter Typography',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.deepPurple,
),
)
5. Font Size
The fontSize property controls the size of the text.
Text(
'Small Text',
style: TextStyle(
fontSize: 14,
),
);
Text(
'Large Text',
style: TextStyle(
fontSize: 32,
),
);
Example with Different Sizes
Column(
children: [
Text(
'Small Text',
style: TextStyle(fontSize: 14),
),
Text(
'Medium Text',
style: TextStyle(fontSize: 20),
),
Text(
'Large Text',
style: TextStyle(fontSize: 32),
),
],
)
6. Font Weight
fontWeight controls how thick or bold the characters appear.
Common Font Weights
| FontWeight | Description |
FontWeight.w100 | Very thin |
FontWeight.w200 | Extra light |
FontWeight.w300 | Light |
FontWeight.w400 | Normal |
FontWeight.w500 | Medium |
FontWeight.w600 | Semi-bold |
FontWeight.w700 | Bold |
FontWeight.w800 | Extra-bold |
FontWeight.w900 | Very bold |
Example
Column(
children: [
Text(
'Normal',
style: TextStyle(
fontWeight: FontWeight.normal,
),
),
Text(
'Medium',
style: TextStyle(
fontWeight: FontWeight.w500,
),
),
Text(
'Bold',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
],
)
7. Font Style
The fontStyle property can be used to make text normal or italic.
Text(
'Italic Text',
style: TextStyle(
fontStyle: FontStyle.italic,
),
)
Normal text can be created using:
TextStyle(
fontStyle: FontStyle.normal,
)
8. Text Color
The color property controls the color of the text.
Text(
'Blue Text',
style: TextStyle(
color: Colors.blue,
),
)
Using Color with Multiple Properties
Text(
'Important Message',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
color: Colors.red,
),
)
9. Text Alignment
The textAlign property controls the horizontal alignment of text within its available space.
Common TextAlign Values
TextAlign.left
TextAlign.right
TextAlign.center
TextAlign.justify
TextAlign.start
TextAlign.end
Example
Text(
'Welcome to Flutter',
textAlign: TextAlign.center,
)
Full-Width Centered Text
Container(
width: double.infinity,
child: Text(
'Centered Heading',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
)
10. Text Decoration
The decoration property can be used to add lines or other decorations to text.
Underline
Text(
'Underlined Text',
style: TextStyle(
decoration: TextDecoration.underline,
),
)
Line Through
Text(
'Deleted Price',
style: TextStyle(
decoration: TextDecoration.lineThrough,
),
)
Overline
Text(
'Overline Text',
style: TextStyle(
decoration: TextDecoration.overline,
),
)
11. Decoration Color and Style
Text(
'Special Text',
style: TextStyle(
decoration: TextDecoration.underline,
decorationColor: Colors.red,
decorationStyle: TextDecorationStyle.dashed,
),
)
Useful decoration styles include:
TextDecorationStyle.solid
TextDecorationStyle.double
TextDecorationStyle.dotted
TextDecorationStyle.dashed
TextDecorationStyle.wavy
12. Letter Spacing
letterSpacing controls the additional space between individual characters.
Text(
'FLUTTER',
style: TextStyle(
letterSpacing: 4,
fontWeight: FontWeight.bold,
),
)
Letter spacing can be useful for headings, labels, buttons, and uppercase text.
13. Word Spacing
wordSpacing controls the additional space between words.
Text(
'Flutter Text Typography',
style: TextStyle(
wordSpacing: 8,
),
)
14. Line Height
The height property of TextStyle controls line height as a multiple of the font size. It is useful for improving readability in paragraphs and multi-line content. :contentReference[oaicite:3]{index=3}
Text(
'Flutter makes it easy to build beautiful applications. '
'Good typography improves readability and user experience.',
style: TextStyle(
fontSize: 18,
height: 1.6,
),
)
15. Font Family
The fontFamily property specifies the preferred font family used to render text. Flutter also supports fallback font families when a requested glyph is not available. :contentReference[oaicite:4]{index=4}
Example
Text(
'Custom Font Example',
style: TextStyle(
fontFamily: 'Roboto',
fontSize: 24,
),
)
16. Using Custom Fonts
Custom fonts can be included in a Flutter project and declared in pubspec.yaml.
Example Project Structure
project/
├── assets/
│ └── fonts/
│ ├── MyFont-Regular.ttf
│ └── MyFont-Bold.ttf
├── lib/
│ └── main.dart
└── pubspec.yaml
pubspec.yaml
flutter:
fonts:
- family: MyFont
fonts:
- asset: assets/fonts/MyFont-Regular.ttf
- asset: assets/fonts/MyFont-Bold.ttf
weight: 700
Using the Custom Font
Text(
'Hello Flutter',
style: TextStyle(
fontFamily: 'MyFont',
fontSize: 28,
fontWeight: FontWeight.bold,
),
)
17. Font Family Fallback
Flutter allows developers to specify fallback font families. If a requested glyph is not available in the primary font, Flutter can try the fallback families in order. This is particularly useful for multilingual applications and special characters. :contentReference[oaicite:5]{index=5}
Text(
'Hello नमस्ते',
style: TextStyle(
fontFamily: 'MyFont',
fontFamilyFallback: [
'Noto Sans',
'Roboto',
],
),
)
18. Text Shadow
The shadows property can add one or more shadows to text.
Text(
'Flutter',
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.bold,
shadows: [
Shadow(
offset: Offset(3, 3),
blurRadius: 5,
color: Colors.grey,
),
],
),
)
19. Multiple Shadows
Text(
'Shadow Text',
style: TextStyle(
fontSize: 32,
shadows: [
Shadow(
offset: Offset(2, 2),
blurRadius: 4,
color: Colors.black45,
),
Shadow(
offset: Offset(-1, -1),
blurRadius: 2,
color: Colors.white,
),
],
),
)
20. Text Overflow
Text can sometimes be longer than the available space. Flutter provides the overflow property to control how visual overflow is handled. :contentReference[oaicite:6]{index=6}
Ellipsis
Text(
'This is a very long text that may not fit inside the available width.',
maxLines: 1,
overflow: TextOverflow.ellipsis,
)
This can produce an output similar to:
This is a very long text that may...
Fade
Text(
'This is a long text example.',
maxLines: 1,
overflow: TextOverflow.fade,
)
Clip
Text(
'This is a long text example.',
maxLines: 1,
overflow: TextOverflow.clip,
)
21. Maximum Lines
The maxLines property limits the number of lines that the text can occupy.
Text(
'Flutter is a UI toolkit for building applications. '
'This paragraph demonstrates maximum line control.',
maxLines: 2,
overflow: TextOverflow.ellipsis,
)
22. Soft Wrapping
The softWrap property controls whether text should wrap at soft line breaks.
Text(
'Flutter makes application development easier.',
softWrap: true,
)
For normal paragraphs, allowing text to wrap is generally appropriate.
23. Text in Containers
Text is frequently placed inside a Container to control its size, padding, background, and border.
Container(
padding: EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(12),
),
child: Text(
'Flutter Text',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
)
24. Text in Cards
Card(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Flutter Course',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8),
Text(
'Learn Flutter from basics to advanced concepts.',
style: TextStyle(
fontSize: 16,
color: Colors.grey,
),
),
],
),
),
)
25. TextTheme
For larger applications, defining text styles individually for every Text widget can lead to inconsistent typography. Flutter's Material system provides TextTheme for organizing commonly used text styles. A theme's text theme can be accessed through Theme.of(context).textTheme. :contentReference[oaicite:7]{index=7}
Example
Text(
'Dashboard',
style: Theme.of(context).textTheme.headlineMedium,
)
26. Common TextTheme Styles
The exact available styles depend on the Flutter Material API and theme configuration, but commonly used Material text roles include styles such as:
displayLarge
displayMedium
displaySmall
headlineLarge
headlineMedium
headlineSmall
titleLarge
titleMedium
titleSmall
bodyLarge
bodyMedium
bodySmall
labelLarge
labelMedium
labelSmall
Example
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Main Heading',
style: Theme.of(context).textTheme.headlineMedium,
),
Text(
'Section Title',
style: Theme.of(context).textTheme.titleLarge,
),
Text(
'Body content goes here.',
style: Theme.of(context).textTheme.bodyMedium,
),
],
)
27. Defining a Global Text Theme
A global text theme can be defined through ThemeData. This allows common typography choices to be shared across the application.
MaterialApp(
theme: ThemeData(
textTheme: const TextTheme(
headlineLarge: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
),
titleLarge: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w600,
),
bodyLarge: TextStyle(
fontSize: 18,
),
bodyMedium: TextStyle(
fontSize: 16,
),
),
),
home: const HomeScreen(),
)
28. Using Theme Styles in Widgets
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Scaffold(
appBar: AppBar(
title: const Text('Typography'),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Welcome',
style: textTheme.headlineMedium,
),
const SizedBox(height: 10),
Text(
'This application uses a shared text theme.',
style: textTheme.bodyLarge,
),
],
),
),
);
}
}
29. DefaultTextStyle
DefaultTextStyle provides a default text style to descendant Text widgets that do not specify their own explicit style. :contentReference[oaicite:8]{index=8}
DefaultTextStyle(
style: TextStyle(
fontSize: 18,
color: Colors.blue,
),
child: Column(
children: [
Text('First Text'),
Text('Second Text'),
Text('Third Text'),
],
),
)
This is useful when multiple text widgets inside the same section should share a common style.
30. TextStyle Inheritance
By default, a TextStyle can inherit unspecified values from the surrounding text style. For example, you can change only the font weight while allowing other inherited values to remain unchanged. :contentReference[oaicite:9]{index=9}
DefaultTextStyle(
style: TextStyle(
fontSize: 18,
color: Colors.blue,
),
child: Text(
'Bold Blue Text',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
)
31. RichText
When different parts of a single piece of text need different styles, RichText and TextSpan can be used. This is useful for highlighting words, creating mixed styles, or displaying formatted text.
RichText(
text: TextSpan(
text: 'Welcome to ',
style: TextStyle(
fontSize: 20,
color: Colors.black,
),
children: [
TextSpan(
text: 'Flutter',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
TextSpan(
text: ' Development',
style: TextStyle(
color: Colors.black,
),
),
],
),
)
32. TextSpan
TextSpan represents a span of text that can have its own style. Multiple TextSpan objects can be combined inside a RichText widget.
RichText(
text: TextSpan(
children: [
TextSpan(
text: 'Price: ',
style: TextStyle(
color: Colors.black,
),
),
TextSpan(
text: '₹999',
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.bold,
),
),
],
),
)
33. Text with Gesture Interaction
TextSpan can also be combined with a TapGestureRecognizer to create interactive portions of rich text.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
RichText(
text: TextSpan(
text: 'Read our ',
style: const TextStyle(
color: Colors.black,
),
children: [
TextSpan(
text: 'Terms and Conditions',
style: const TextStyle(
color: Colors.blue,
decoration: TextDecoration.underline,
),
recognizer: TapGestureRecognizer()
..onTap = () {
print('Terms clicked');
},
),
],
),
)
34. Selectable Text
When users need to copy text from the application, Flutter provides selectable text widgets such as SelectableText.
SelectableText(
'This text can be selected and copied.',
style: TextStyle(
fontSize: 18,
),
)
35. Text with Icons
Text and icons are frequently combined using Row.
Row(
children: [
Icon(
Icons.email,
color: Colors.blue,
),
SizedBox(width: 8),
Text(
'[email protected]',
style: TextStyle(
fontSize: 16,
),
),
],
)
36. Text in Buttons
ElevatedButton(
onPressed: () {},
child: Text(
'Login',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
)
For larger applications, button typography can also be managed through the application's theme and button styles.
37. Text Overflow in Row
A common layout problem occurs when long text is placed inside a Row. Expanded or Flexible can be used to give the text a constrained amount of space.
Row(
children: [
Icon(Icons.person),
SizedBox(width: 10),
Expanded(
child: Text(
'This is a very long username that may not fit.',
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
)
38. Responsive Typography
Typography should remain readable on different screen sizes. Avoid relying on unnecessarily large fixed text sizes. Use appropriate theme styles, constraints, and layouts.
Example
LayoutBuilder(
builder: (context, constraints) {
final size = constraints.maxWidth < 600 ? 24.0 : 32.0;
return Text(
'Responsive Heading',
style: TextStyle(
fontSize: size,
fontWeight: FontWeight.bold,
),
);
},
)
39. Text Scaling
Flutter provides text scaling support so that text can adapt to accessibility and platform settings. The modern Text API uses TextScaler; the older textScaleFactor property is deprecated in current Flutter documentation. :contentReference[oaicite:10]{index=10}
Text(
'Accessible Flutter Text',
textScaler: TextScaler.linear(1.2),
)
In production applications, avoid unnecessarily overriding user text-scaling preferences unless there is a specific design requirement.
40. Typography for a Login Screen
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Welcome Back',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8),
Text(
'Login to continue to your account.',
style: TextStyle(
fontSize: 16,
color: Colors.grey,
height: 1.5,
),
),
SizedBox(height: 25),
Text(
'Email',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
],
)
41. Typography for a Product Card
Card(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Flutter Laptop',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 6),
Text(
'High-performance laptop for development.',
style: TextStyle(
fontSize: 14,
color: Colors.grey,
height: 1.4,
),
),
SizedBox(height: 12),
Text(
'₹75,000',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
color: Colors.green,
),
),
],
),
),
)
42. Typography Hierarchy
A good typography hierarchy helps users understand which information is most important.
| Level | Example | Typical Purpose |
| Display | Large heading | Hero sections and major titles |
| Headline | Page heading | Main screen heading |
| Title | Section title | Card or section heading |
| Body | Paragraph | General information |
| Label | Button or form label | Actions and controls |
43. Good Typography Practices
- Use a consistent font family throughout the application.
- Use a clear hierarchy between headings, titles, body text, and labels.
- Avoid using too many different font families.
- Use appropriate font weights instead of making every heading bold.
- Use sufficient line height for paragraphs.
- Use text colors that provide adequate contrast.
- Handle long text using
maxLines and TextOverflow where appropriate.
- Use responsive layouts so text does not overflow on smaller screens.
- Prefer theme-based typography for consistency across screens.
- Respect accessibility and user text-scaling settings.
44. Common Mistakes in Flutter Typography
Mistake 1: Using Excessive Font Sizes
Text(
'Welcome',
style: TextStyle(
fontSize: 80,
),
)
Very large text can cause overflow and poor layout on smaller devices.
Mistake 2: Ignoring Text Overflow
Row(
children: [
Icon(Icons.info),
Text('A very long text that may overflow'),
],
)
Use Expanded or Flexible when the text needs to share limited horizontal space.
Mistake 3: Repeating the Same TextStyle Everywhere
Instead of manually repeating the same styles, use TextTheme and application-level themes.
Mistake 4: Poor Line Height
Very small line spacing can make paragraphs difficult to read. Use the height property when appropriate.
Mistake 5: Too Many Font Styles
Using many font families, colors, sizes, and decorations can make an application visually inconsistent.
45. Complete Typography Example
import 'package:flutter/material.dart';
void main() {
runApp(const TypographyApp());
}
class TypographyApp extends StatelessWidget {
const TypographyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
),
textTheme: const TextTheme(
headlineLarge: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
),
titleLarge: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w600,
),
bodyLarge: TextStyle(
fontSize: 18,
height: 1.5,
),
bodyMedium: TextStyle(
fontSize: 16,
height: 1.4,
),
),
),
home: const TypographyScreen(),
);
}
}
class TypographyScreen extends StatelessWidget {
const TypographyScreen({super.key});
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Scaffold(
appBar: AppBar(
title: const Text('Typography'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Flutter Typography',
style: textTheme.headlineLarge,
),
const SizedBox(height: 12),
Text(
'Create readable and consistent user interfaces using Flutter text widgets and themes.',
style: textTheme.bodyLarge,
),
const SizedBox(height: 25),
Text(
'Section Title',
style: textTheme.titleLarge,
),
const SizedBox(height: 10),
Text(
'Typography includes font family, font size, weight, spacing, alignment, decoration, and line height.',
style: textTheme.bodyMedium,
),
const SizedBox(height: 25),
Text(
'Special Text',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.blue,
letterSpacing: 1.5,
shadows: [
Shadow(
offset: Offset(1, 1),
blurRadius: 3,
color: Colors.grey,
),
],
),
),
],
),
),
);
}
}
46. Text and Typography Quick Reference
| Property | Purpose | Example |
fontSize | Controls text size | fontSize: 20 |
fontWeight | Controls text thickness | FontWeight.bold |
fontStyle | Controls normal/italic style | FontStyle.italic |
color | Controls text color | Colors.blue |
fontFamily | Sets preferred font family | fontFamily: 'Roboto' |
letterSpacing | Controls character spacing | letterSpacing: 2 |
wordSpacing | Controls word spacing | wordSpacing: 5 |
height | Controls line height | height: 1.5 |
decoration | Adds text decoration | TextDecoration.underline |
shadows | Adds text shadows | shadows: [...] |
textAlign | Aligns text | TextAlign.center |
maxLines | Limits number of lines | maxLines: 2 |
overflow | Controls overflowing text | TextOverflow.ellipsis |
softWrap | Controls soft wrapping | softWrap: true |
47. Interview Questions
- What is the purpose of the
Text widget in Flutter?
- What is
TextStyle?
- How do you change the font size of text?
- How do you make text bold?
- How do you make text italic?
- How do you change text color?
- What is the purpose of
textAlign?
- What is
TextOverflow.ellipsis?
- What is the purpose of
maxLines?
- What is the difference between
letterSpacing and wordSpacing?
- How do you add an underline to text?
- How do you add a shadow to text?
- What is
TextTheme?
- What is
DefaultTextStyle?
- What is the difference between
Text and RichText?
- What is
TextSpan used for?
- How can custom fonts be added to a Flutter application?
- Why is typography hierarchy important in UI design?
48. Practice Exercises
- Create a heading using a font size of 32 and bold weight.
- Create a paragraph with a custom line height.
- Create underlined, italic, and bold text examples.
- Create a text widget with custom letter spacing.
- Create a product card containing a product name, description, and price.
- Create a login screen with a typography hierarchy.
- Create a long text example using
maxLines and TextOverflow.ellipsis.
- Create a
RichText widget where different words have different colors.
- Create a custom
TextTheme and use it throughout an application.
- Add a custom font to a Flutter project and use it with
TextStyle.
49. Key Takeaways
- The
Text widget is used to display text in Flutter.
TextStyle controls the appearance of text.
- Font size, weight, color, family, style, spacing, and decoration are important typography properties.
TextOverflow helps handle text that does not fit within its available space.
maxLines limits how many lines text can occupy.
TextTheme provides a reusable typography system for Material applications.
DefaultTextStyle can provide a common style to descendant text widgets.
RichText and TextSpan are useful when different parts of a text need different styles.
- Custom fonts can be declared in
pubspec.yaml and used through fontFamily.
- Good typography improves readability, consistency, accessibility, and overall user experience.
50. Learning Resources
Summary: Flutter provides a powerful and flexible typography system through Text, TextStyle, TextTheme, DefaultTextStyle, RichText, and TextSpan. By combining these tools with appropriate font sizes, weights, colors, spacing, line heights, overflow handling, and responsive layouts, developers can create readable, consistent, and professional Flutter interfaces.