Sub topic 1
Showing text with the Text widget
Prerequisites
This chapter assumes basic Dart syntax, named parameters, Flutter widgets, StatelessWidget, the build method, and a working Flutter project with a pubspec.yaml file. It also assumes you know how to run the project on an emulator, device, or web browser.
The working model
Text displays a string in the widget tree. Flutter lays out that string using the available width, the active text direction, and a TextStyle. If you do not provide a style, Text inherits the default style from the nearest DefaultTextStyle, usually supplied by MaterialApp.
A Text widget does not automatically make every string fit on one line. Long text wraps when its parent gives it a limited width. Use maxLines and overflow when the design requires a fixed number of lines.
import 'package:flutter/material.dart';
void main() {
runApp(const TextExampleApp());
}
class TextExampleApp extends StatelessWidget {
const TextExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Text example')),
body: const Padding(
padding: EdgeInsets.all(24),
child: Text(
'Flutter builds user interfaces from widgets. '
'This sentence can wrap when the available width is small.',
style: TextStyle(
fontSize: 18,
height: 1.4,
color: Colors.black87,
),
),
),
),
);
}
}Styling and layout
TextStyle contains visual properties such as font size, weight, color, letter spacing, and line height. The height property is a multiplier of the font size. For example, a font size of 18 with height: 1.4 gives the line a height of about 25.2 logical pixels.
Text layout properties belong to Text, not TextStyle.
| Property | Purpose |
|---|---|
| `textAlign` | Positions lines within the widget's width |
| `maxLines` | Limits the number of displayed lines |
| `overflow` | Defines what happens to hidden text |
| `softWrap` | Allows or prevents wrapping at word boundaries |
The following widget keeps a heading to one line and shows an ellipsis when the parent provides too little width:
Text(
'A heading that may be longer than the available space',
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
fontWeight: FontWeight.bold,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
)Theme.of(context).textTheme returns the application's theme styles. Starting with a theme style and using copyWith keeps the app's typography consistent while changing only the properties this widget needs.
Constraints and common errors
Text can only wrap or truncate when its parent supplies a finite width. A row may give children less width than expected, so a long label can overflow with yellow and black stripes. Wrap the text in Expanded or Flexible when it must share a row with another child.
Avoid fixing overflow by choosing a very small font. That makes the interface harder to read and ignores user text-size settings. Test with larger system font sizes and with long, translated strings.
Exercise
Why might maxLines: 1 fail to show an ellipsis in a row?
Show answer
The text may not have a finite width. Give it a bounded space with Expanded, Flexible, or another width-constraining parent.