Sub topic 3
Alignment, wrapping, and overflow
This chapter covers how a Text widget places its lines inside the space its parent gives it, and what happens when the text does not fit. It assumes you can write a StatelessWidget, know that Flutter layout passes constraints down from parent to child, and have used Row and Column. The previous chapters covered Text and TextStyle.
How Text decides its size
A Text widget lays out its characters inside the constraints from its parent. Two rules explain most of the surprises in this chapter:
- With a bounded maximum width, text wraps at that width and the widget grows taller as needed.
- With an unbounded maximum width, text stays on one line and the widget becomes as wide as the text.
A Column passes its children the full available width as the maximum, but it lets each child be narrower. A Row passes its children an unbounded width on the main axis. This difference causes most overflow errors.
Aligning text with textAlign
textAlign positions each line horizontally inside the widget's own width. The values are left, right, center, justify, start, and end. The default is start.
start and end follow the reading direction. In a left-to-right language start is the left edge. In Arabic or Hebrew it is the right edge. Prefer start and end over left and right unless you really want a fixed side.
textAlign often seems to do nothing. The cause is that a Text with loose constraints is only as wide as its longest line, so there is no spare room to align within. This is the most common mistake in this area. Here are three ways to fix it:
- Give the
Texta width, for example withSizedBox(width: double.infinity, child: ...). - Place it in a
CenterorAlignwidget, which moves the whole widget instead of the lines inside it. - Set
crossAxisAlignmenton the parentColumn.
Use TextAlign.justify with care. It stretches the spaces in every line except the last, and in narrow columns this produces wide gaps.
Wrapping, maxLines, and softWrap
By default softWrap is true, so text breaks at word boundaries when it reaches the maximum width. A single word longer than the width is broken at the character level instead.
maxLines caps the number of lines. The default is null, which means no limit. After the cap, the overflow setting decides what the reader sees.
If you set softWrap: false, text lays out as if the width were unlimited. Explicit newline characters (\n) still start new lines. This is useful for single-line labels that you truncate with overflow.
Choosing an overflow style
overflow takes a TextOverflow value. If you leave it unset, Text takes it from the nearest DefaultTextStyle, and the default there is clip.
| Value | Behavior | Typical use |
|---|---|---|
| `TextOverflow.clip` | Cuts the text at the box edge, possibly mid-letter | Default, tight layouts |
| `TextOverflow.fade` | Fades the last visible text to transparent | Soft edges on previews |
| `TextOverflow.ellipsis` | Ends the visible text with "…" | Titles, list rows |
| `TextOverflow.visible` | Draws the text outside its box | Rare, may overlap neighbors |
Here is a runnable app that compares the first three on the same string:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: OverflowDemo()));
class OverflowDemo extends StatelessWidget {
const OverflowDemo({super.key});
static const String sample =
'Flutter lays out text inside the constraints it receives, '
'wrapping at word boundaries and then applying overflow rules.';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Overflow styles')),
body: const Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(sample, maxLines: 2, overflow: TextOverflow.clip),
SizedBox(height: 16),
Text(sample, maxLines: 2, overflow: TextOverflow.fade),
SizedBox(height: 16),
Text(
sample,
maxLines: 2,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
),
],
),
),
);
}
}The Column uses CrossAxisAlignment.stretch, so every Text is as wide as the screen minus padding. That is why the textAlign: TextAlign.center on the last one has a visible effect.
Text inside a Row
A Row gives each child unlimited width on its main axis. A long Text in a Row therefore stays on one line and runs past the screen edge. In debug mode Flutter paints a yellow and black striped bar and logs a RenderFlex overflowed message. In release builds the content is silently clipped, so fix it before shipping.
Wrap the Text in Expanded or Flexible, which give it a bounded width:
import 'package:flutter/material.dart';
class ProfileRow extends StatelessWidget {
const ProfileRow({super.key, required this.name});
final String name;
@override
Widget build(BuildContext context) {
return Row(
children: [
const Icon(Icons.person),
const SizedBox(width: 8),
Expanded(
child: Text(
name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
const Icon(Icons.chevron_right),
],
);
}
}The two icons take their natural width. Expanded gives the text all the remaining space, and the ellipsis appears only when the name is too long.
Expanded forces its child to fill the available space. Flexible with its default FlexFit.loose lets the child be smaller. Use Flexible when a short label should hug its content but a long one should still shrink.
The same problem happens in a Column when the content is taller than the screen. There the fix is a scrollable parent such as SingleChildScrollView or ListView, not maxLines.
Width basis for multi-line text
When text wraps, the widget's width is normally the full width offered by the parent, even if the longest line is shorter. This leaves empty space on the right side of a shrink-wrapped bubble, for example in a chat message. Set textWidthBasis: TextWidthBasis.longestLine to make the widget as wide as its longest wrapped line. The default is TextWidthBasis.parent.
Common errors
- textAlign has no effect. The widget is exactly as wide as its text. Give it a width or wrap it in
Align. - Yellow-black stripes in a Row. An unconstrained
Textis too wide. AddExpandedorFlexible. - Ellipsis never appears.
maxLinesis unset and the text has room to grow, orsoftWrap: falseis paired with a parent that has unbounded width. Expandedoutside a Row or Column. Flutter throws a "Incorrect use of ParentDataWidget" error.Expandedmust be a direct child of aRow,Column, orFlex.- Text clipped at the bottom. A fixed-height
SizedBoxorContaineris too short, often after the user raises the system font size. Prefer constraints that grow, such asminHeight.
Exercise
A Row contains an Icon, a Text with a 90-character title, and an Icon. The title runs off the screen and a striped overflow bar appears. Which change fixes it while showing at most one line with a trailing "…"?
Show answer
Wrap the Text in Expanded (or Flexible) so it gets a bounded width, and set maxLines: 1 and overflow: TextOverflow.ellipsis on the Text.
Exercise
You set textAlign: TextAlign.right on a short Text inside a Column that uses the default crossAxisAlignment, but the text does not move to the right edge. Name two fixes.
Show answer
Make the Text as wide as the Column, for example with SizedBox(width: double.infinity, child: Text(...)) or by setting crossAxisAlignment: CrossAxisAlignment.stretch. Alternatively, wrap it in Align(alignment: Alignment.centerRight) so the whole widget moves.