Sub topic 9
Layouts that survive large text
Users can raise the system font size in Android and iOS settings. Flutter reads that setting and scales every Text it draws. A layout that looks right at the default size can then overflow, clip text, or push buttons off screen. This chapter covers how scaling works and which layout habits keep screens usable. It also shows how to test them. It assumes you know Row, Column, Expanded and MediaQuery, and that you can run widget tests.
How Flutter applies the text scale
The platform reports a text scale to Flutter. Flutter exposes it as a TextScaler on MediaQuery. Every Text and TextField multiplies its font size through that scaler before it lays out. Only text grows. Icons, padding, images and fixed-size boxes stay the same, so a text-heavy row can run out of room while the boxes around it do not change.
Scale ranges differ by platform. Android settings commonly allow about 100% to 200%. iOS offers larger "Accessibility Sizes" that can reach roughly 300% of the body size. Plan for at least 200%, and test at 300% for text-heavy screens.
Read the scaler with MediaQuery.textScalerOf(context). Call scaler.scale(fontSize) to get the size a font will actually have. Older code uses textScaleFactor, a single double. It is deprecated, and TextScaler replaced it because newer Android versions scale large text nonlinearly. Treat the scale as a function, not a constant multiplier.
Why fixed sizes break
Three patterns cause most failures.
| Pattern | What happens at large text | Fix |
|---|---|---|
| `SizedBox(height: 48)` or `Container(height: 48)` around text | Text is clipped or overflows | `minHeight` constraint, or no height |
| `Row` with a long `Text` and no flex wrapper | Yellow and black overflow stripes | Wrap the `Text` in `Expanded` or `Flexible` |
| Content in a `Column` taller than the screen | Overflow at the bottom | Put it in a scrollable |
Here is the first pattern, which fails.
Container(
height: 48, // text at 300% needs far more than 48 logical pixels
alignment: Alignment.centerLeft,
child: const Text('Continue to payment'),
)The next version states a minimum instead of a fixed size. The box can grow with its content.
Container(
constraints: const BoxConstraints(minHeight: 48),
alignment: Alignment.centerLeft,
child: const Text('Continue to payment'),
)You can also scale the minimum itself, as in minHeight: scaler.scale(48). This keeps touch targets proportional to the text. Use it for controls where the target should grow with the label. The minHeight of 48 already covers the tap target requirement from the earlier chapter. Do not scale it by default.
Flexible rows and wrapping groups
A Row gives its children unbounded width unless some of them are flexible. Wrap the text in Expanded, and it takes the remaining width and wraps onto more lines.
A group of buttons or chips is different. At large scale it should flow onto a second line, not shrink. Wrap does that: when children no longer fit, it starts a new run.
import 'package:flutter/material.dart';
class ProfileCard extends StatelessWidget {
const ProfileCard({super.key});
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const CircleAvatar(child: Icon(Icons.person)),
const SizedBox(width: 12),
Expanded(
child: Text(
'Alexandra Montgomery-Whitfield',
style: Theme.of(context).textTheme.titleMedium,
),
),
],
),
const SizedBox(height: 12),
Wrap(
spacing: 8,
runSpacing: 8,
children: [
FilledButton(onPressed: () {}, child: const Text('Follow')),
OutlinedButton(onPressed: () {}, child: const Text('Message')),
OutlinedButton(
onPressed: () {},
child: const Text('Share profile'),
),
],
),
],
),
);
}
}This card has three protections. Expanded lets the name wrap. Wrap lets the buttons flow onto more lines. SingleChildScrollView lets the whole card scroll if it grows taller than the screen. Make scrolling the last line of defense for any screen with a lot of content, since no layout can promise to fit at every scale.
Truncation is a last resort
Setting maxLines: 1 with overflow: TextOverflow.ellipsis hides information. A low-vision user who enlarged the text is the person most hurt by cutting it off. Use truncation only where the full text is available elsewhere, such as a long file name in a list whose detail page shows it in full. For titles, labels, prices and error messages, let the text wrap.
Choosing a fix
The diagram summarizes the decision you make when a widget overflows.
Adapting the layout to the scale
Sometimes wrapping is not enough. A horizontal row of three icon-and-label tabs works at 100% but reads badly at 300%. In that case, change the structure. Read the scaler and switch from a Row to a Column above a threshold.
import 'package:flutter/material.dart';
class ActionBar extends StatelessWidget {
const ActionBar({super.key});
@override
Widget build(BuildContext context) {
final scaler = MediaQuery.textScalerOf(context);
// Size a 14-point label would have after scaling.
final isLarge = scaler.scale(14) > 24;
final items = [
TextButton(onPressed: () {}, child: const Text('Reply')),
TextButton(onPressed: () {}, child: const Text('Forward')),
TextButton(onPressed: () {}, child: const Text('Archive')),
];
return isLarge
? Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: items)
: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: items);
}
}Comparing scaler.scale(14) against a size works with nonlinear scaling. Comparing a raw factor does not.
Do not turn scaling off
You may be tempted to ignore the user's setting by wrapping the app in a MediaQuery with TextScaler.noScaling. This removes the accommodation for the people who need it. If a specific widget really breaks, such as a fixed-size badge counter, clamp only that widget.
MediaQuery.withClampedTextScaling(
minScaleFactor: 1.0,
maxScaleFactor: 1.5,
child: const Text('99+'),
)Keep clamps narrow and rare. Apply them to small decorative labels, never to body text or controls.
Testing at large scale
Add a widget test that renders your screens at several scales on a narrow surface. Flutter reports overflow as an exception during layout, and tester.takeException() returns it.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
// Assumes ProfileCard from this chapter is imported.
void main() {
testWidgets('ProfileCard does not overflow at large text', (tester) async {
tester.view.physicalSize = const Size(360, 640);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
for (final scale in [1.0, 2.0, 3.0]) {
await tester.pumpWidget(
MaterialApp(
builder: (context, child) => MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaler: TextScaler.linear(scale),
),
child: child!,
),
home: const Scaffold(body: ProfileCard()),
),
);
expect(tester.takeException(), isNull, reason: 'overflow at $scale');
}
});
}Automated tests catch overflow, but not awkward layouts. Also set the real system font size on a device or emulator and look at each screen. On Android, use Settings, Display, Font size. On iOS, use Settings, Accessibility, Display & Text Size, Larger Text. Check screens that contain forms, dialogs and bottom sheets. Dialogs and sheets fail often because they have a fixed maximum height.
Edge cases
- Text inside
AppBarandBottomNavigationBar. Their heights are tied to the toolbar size. Long titles can be cut off, so give titlesmaxLinesof 2 or use a flexibleSliverAppBar. TextFieldlabels. A floating label scales with the text. UseInputDecorationwith enough content padding and avoid a fixed-height parent.- Images beside text. Keep image sizes fixed, since they should not scale. Make sure the text next to them can wrap.
- Custom painting.
CustomPainterandTextPainterdo not scale automatically. PasstextScaler: MediaQuery.textScalerOf(context)to theTextPainter.
Exercise
A settings row is Row(children: [Icon(Icons.wifi), Text('Connect to a wireless network automatically'), Switch(...)]). At 300% text, a striped overflow appears. What change fixes it, and why is maxLines: 1 with an ellipsis a poor fix?
Show answer
Wrap the Text in Expanded (or Flexible). The Row then gives the text only the width left after the icon and switch, so the text wraps onto more lines instead of running past the edge. Truncating with maxLines: 1 and an ellipsis hides part of the label, which hurts the users who enlarged the text to read it.
Exercise
Why is Container(constraints: BoxConstraints(minHeight: 48)) safer than Container(height: 48) around a label?
Show answer
A fixed height cannot grow, so large text is clipped or overflows. A minimum height keeps the 48-pixel tap target but lets the box grow to fit the scaled text.