Sub topic 7
Text scaling and user font settings
This chapter assumes you can write a StatelessWidget, you know the basics of Text and TextStyle, and you have used Theme.of(context).textTheme. It covers what happens when a user changes the font size in their device settings, and how to keep your layouts working when they do.
Why text scaling exists
Both Android and iOS let users enlarge or shrink text system-wide. People with low vision rely on this. Others just prefer larger type. Flutter reads that setting and applies it to every Text widget. Your fontSize: 16 is therefore a starting point, not the final size on screen.
The setting reaches your app through MediaQuery. The value is a TextScaler, an object that turns a font size you chose into the size Flutter actually draws.
import 'package:flutter/material.dart';
class ScaleInfo extends StatelessWidget {
const ScaleInfo({super.key});
@override
Widget build(BuildContext context) {
final scaler = MediaQuery.textScalerOf(context);
final scaled = scaler.scale(16);
return Text('A 16 px font is drawn at $scaled px');
}
}On a device with default settings, scaled is 16. With a 150% setting, it is 24. Some platforms, such as recent Android versions, use a non-linear curve. Large sizes grow less than small ones, so do not assume scaled == 16 * factor. Call scale instead of multiplying.
> Older tutorials use textScaleFactor, a plain double. It is deprecated in favor of TextScaler. Use MediaQuery.textScalerOf(context) in new code.
Scaling is on by default
Every Text widget scales with the user's setting unless you turn that off. The rule is simple: if the user asks for bigger text, the text grows, and the widgets around it must make room.
The last step is where bugs appear. A button with a fixed height of 40 and a label that grows to 60 will clip or overflow. Test your screens at large scale before shipping.
Controlling scaling on one widget
Text has a textScaler parameter. It replaces the inherited scaler for that widget only.
import 'package:flutter/material.dart';
class ScalingExamples extends StatelessWidget {
const ScalingExamples({super.key});
@override
Widget build(BuildContext context) {
return const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Follows the user's setting (default behavior).
Text('Follows the system setting'),
// Ignores the setting entirely.
Text(
'Never scales',
textScaler: TextScaler.noScaling,
),
// Always drawn at 1.5 times, regardless of the setting.
Text(
'Fixed at 1.5x',
textScaler: TextScaler.linear(1.5),
),
],
);
}
}Disabling scaling is sometimes justified. A tiny badge showing "3" on an icon, or a label inside a fixed-size graphic, may not have room to grow. Do it rarely. Text that carries meaning should scale, because turning scaling off removes the accommodation the user asked for.
Limiting scaling for a whole subtree
Sometimes you want scaling, but with a ceiling. At 300% a dense screen may become unusable. Wrap the subtree in a MediaQuery that clamps the scaler.
import 'package:flutter/material.dart';
class ClampedScale extends StatelessWidget {
const ClampedScale({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
final data = MediaQuery.of(context);
return MediaQuery(
data: data.copyWith(
textScaler: data.textScaler.clamp(
minScaleFactor: 1.0,
maxScaleFactor: 1.6,
),
),
child: child,
);
}
}TextScaler.clamp keeps the user's choice inside a range. A user at 120% still gets 120%. A user at 250% gets 160%. The ceiling must be generous. Platform accessibility guidance generally expects text to remain usable up to about 200%, so a cap below that works against users who need it.
To apply a limit to the whole app, set it in MaterialApp.builder.
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(
builder: (context, child) {
final data = MediaQuery.of(context);
return MediaQuery(
data: data.copyWith(
textScaler: data.textScaler.clamp(maxScaleFactor: 2.0),
),
child: child!,
);
},
home: const Scaffold(
body: Center(child: Text('Hello, scaling')),
),
);
}
}Building layouts that survive large text
Most scaling bugs come from layouts that assume text has a fixed size. These habits prevent them.
- Avoid fixed heights around text. Use padding and let the widget size itself, or use
constraints: BoxConstraints(minHeight: 48)so the box can grow. - Let text wrap. A
Rowwith long text needsExpandedorFlexiblearound theText, or it overflows horizontally. - Do not rely on
maxLines: 1for important content. An ellipsis hides what the user enlarged the text to read. - Make screens scrollable. A
Columnthat fit at 100% overflows at 200%. Wrap it inSingleChildScrollView, or use aListView. - Scale companions with the text. An icon next to a label looks tiny when the label doubles. Size it from the scaler.
import 'package:flutter/material.dart';
class ScaledRow extends StatelessWidget {
const ScaledRow({super.key});
@override
Widget build(BuildContext context) {
final iconSize = MediaQuery.textScalerOf(context).scale(24);
return Row(
children: [
Icon(Icons.info_outline, size: iconSize),
const SizedBox(width: 8),
const Expanded(
child: Text('Your changes are saved automatically.'),
),
],
);
}
}Here Expanded lets the sentence wrap onto extra lines, and the icon grows at the same rate as the text.
Testing different scales
Test two ways. On a device, change the system font size and run the app. In an automated widget test, inject a scaler through MediaQuery.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('label fits at 2x text scale', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: MediaQuery(
data: const MediaQueryData(textScaler: TextScaler.linear(2.0)),
child: const Scaffold(
body: Center(child: Text('Checkout')),
),
),
),
);
expect(find.text('Checkout'), findsOneWidget);
expect(tester.takeException(), isNull);
});
}If the layout overflows, Flutter records an exception, and takeException returns it. The test then fails and points to the problem.
Note that MaterialApp builds its own MediaQuery from the view, so a MediaQuery placed above it can be replaced. Putting it inside home, as above, keeps your value in effect for the subtree.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Overflow stripe only at large text | Fixed height or unwrapped `Row` | Use `minHeight`, `Expanded`, or scrolling |
| Text ignores the system setting | `textScaler: TextScaler.noScaling` set | Remove it, or clamp instead |
| Compile warning about `textScaleFactor` | Deprecated API | Use `textScaler` and `MediaQuery.textScalerOf` |
| Clamp has no effect | `MediaQuery` placed below the `Text` | Wrap an ancestor of the `Text` |
| Icon looks small next to big text | Icon size is fixed | Compute the size with `scaler.scale` |
Exercise
A settings screen uses a Column with a fixed-height Container (height: 48) holding a Text label. At a 200% system font size, the label is cut off. Name two changes that fix this without disabling text scaling.
Show answer
Replace the fixed height with a minimum height, for example constraints: BoxConstraints(minHeight: 48), so the container grows with the text. Also let the text wrap by removing maxLines: 1 or wrapping the Text in Expanded or Flexible when it sits in a Row, and wrap the Column in a SingleChildScrollView so the taller content can scroll.
Exercise
What does data.textScaler.clamp(maxScaleFactor: 1.5) do for a user whose system setting is 130%, and for a user whose setting is 200%?
Show answer
The user at 130% still gets 130%, because it is inside the allowed range. The user at 200% is limited to 150%, because the clamp caps the result at the maximum.