Sub topic 7
Text scaling and user font settings
Prerequisites
Assume you know basic Dart syntax, Flutter widgets, StatelessWidget, the build method, and pubspec.yaml. You should also have a Flutter project that runs on a device, emulator, or web browser.
Text scaling lets users enlarge or reduce text in the operating system or browser settings. Flutter applies the user's preferred scale to most Text widgets automatically. Your job is to avoid preventing that scaling and to make the resulting layout flexible.
Let Flutter scale normal text
Set a base size through TextStyle, but do not multiply it by a guessed scale factor. Flutter reads the current TextScaler from the nearest MediaQuery and applies it when it lays out the text.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ScalingDemo()));
}
class ScalingDemo extends StatelessWidget {
const ScalingDemo({super.key});
@override
Widget build(BuildContext context) {
final scaler = MediaQuery.textScalerOf(context);
final scaledBodySize = scaler.scale(16);
return Scaffold(
appBar: AppBar(title: const Text('Text scaling')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Flutter respects the user’s text size preference.',
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 16),
Text(
'A 16 logical-pixel base becomes '
'${scaledBodySize.toStringAsFixed(1)} pixels at the current setting.',
),
],
),
),
);
}
}MediaQuery.textScalerOf(context) reads the active scaling behavior. Calling scale(16) is useful when custom drawing or another measurement API needs the scaled value. Do not use that result as the fontSize of a normal Text widget, because the widget would apply the scaler again.
Flutter previously exposed textScaleFactor. In current Flutter code, prefer TextScaler and MediaQuery.textScalerOf. A TextScaler can represent non-linear scaling, so treating it as one universal double can produce incorrect results.
Design for larger text
Large text can wrap onto more lines. Give paragraphs enough vertical space and allow controls to grow. Avoid fixed-height containers around text.
Use maxLines and TextOverflow.ellipsis only when truncation is acceptable. Truncation can hide important information, especially in labels, error messages, and instructions. For a button with a long translated label, prefer a layout that can wrap or become taller.
Common errors
- Hard-coding a small font size and assuming every user can read it.
- Setting
textScaler: TextScaler.noScalingwithout a strong reason. This overrides the user's preference. - Scaling a font size manually and allowing
Textto scale it again. - Using
FittedBoxto shrink text until it fits. This may make text too small instead of allowing the layout to grow. - Giving a text-containing widget a fixed height.
- Testing only with the default text setting.
Exercise
Exercise
Why should you avoid multiplying a TextStyle fontSize by MediaQuery.textScalerOf(context).scale(1) before passing it to Text?
Show answer
Text already applies the inherited TextScaler. Manual scaling can apply the user's preference twice and make the text larger than intended.