Sub topic 3
Alignment, Wrapping, and Overflow in Flutter Text
Assume you know basic Dart syntax, StatelessWidget, the build method, and how Flutter constraints affect widget size. This chapter focuses on how a Text widget uses its available width.
How text gets laid out
A Text widget measures its string inside the constraints provided by its parent. If the available width is smaller than the text, Flutter can wrap the text onto multiple lines. If wrapping is disabled or limited, Flutter must handle the remaining text through overflow behavior.
Alignment, wrapping, and line limits
textAlign controls how lines are positioned within the width assigned to the Text widget. It does not automatically center the widget itself. For example, TextAlign.center centers the text inside the widget's available width.
softWrap controls whether text may wrap at soft break opportunities, such as spaces. It defaults to true. A long word or URL might still extend beyond the available width because it has no suitable break point.
maxLines limits the number of displayed lines. When the text reaches that limit, overflow determines what the user sees.
| Property | Purpose | Common values |
|---|---|---|
| `textAlign` | Positions text inside its width | `left`, `center`, `right`, `justify` |
| `softWrap` | Allows wrapping at break opportunities | `true`, `false` |
| `maxLines` | Limits the number of lines | `1`, `2`, or another integer |
| `overflow` | Handles text beyond the line limit | `clip`, `ellipsis`, `fade` |
This example creates a two-line description. Extra text is replaced with an ellipsis:
import 'package:flutter/material.dart';
class ArticleSummary extends StatelessWidget {
const ArticleSummary({super.key});
@override
Widget build(BuildContext context) {
return const SizedBox(
width: 280,
child: Text(
'Flutter uses constraints to decide how much space a widget may occupy. '
'Text then wraps its content inside that space.',
textAlign: TextAlign.start,
softWrap: true,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
);
}
}A single-line label often uses maxLines: 1 and TextOverflow.ellipsis. This is useful for card titles, usernames, or navigation labels. Use it only when shortening the visible text is acceptable. Important information should remain available through another interaction or a larger layout.
Common layout errors
A Text widget cannot wrap usefully without a finite width. Inside a Row, text may receive unbounded horizontal space and produce a layout error. Wrap it with Expanded or Flexible so it receives the remaining width.
Row(
children: [
const Icon(Icons.info_outline),
const SizedBox(width: 8),
Expanded(
child: Text(
'This message can wrap because Expanded supplies a bounded width.',
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
],
)Expanded forces the child to use the remaining space. Flexible allows the child to use less space when possible. Both can solve text overflow in a Row.
Exercise
Why does placing a long Text widget directly in a Row often cause a layout problem, and what widget can fix it?
Show answer
The Row may give the text unbounded horizontal space. Wrap the Text with Expanded or Flexible so it receives a usable width.