Sub topic 8
Safe areas and text scaling
Assume you know basic Dart, widget trees, Scaffold, and ListView. This chapter focuses on two environmental changes that can break an otherwise flexible layout: system-obscured areas and user-selected text size.
Keep content out of system-obscured areas
A safe area is the part of a window that is not covered by system UI. On a phone, this can include a status bar, a camera cutout, or a home indicator. On a desktop window, the safe area is usually the whole window.
SafeArea is a widget that adds padding around its child when needed. It reads the current MediaQuery data and prevents content from occupying unsafe edges.
Place SafeArea inside the Scaffold body when the body should avoid system edges:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ReadingPage()));
}
class ReadingPage extends StatelessWidget {
const ReadingPage({super.key});
@override
Widget build(BuildContext context) {
final textScaler = MediaQuery.textScalerOf(context);
return Scaffold(
appBar: AppBar(title: const Text('Reading')),
body: SafeArea(
child: ListView(
padding: const EdgeInsets.all(16),
children: [
Text(
'A comfortable reading layout',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 12),
const Text(
'This paragraph remains inside the safe area and '
'responds to the user’s text-size preference.',
),
const SizedBox(height: 24),
Text(
'Current scale: ${textScaler.scale(1).toStringAsFixed(2)}',
),
],
),
),
);
}
}SafeArea does not make every child safe automatically. A widget that deliberately paints behind system UI may need a different design. For example, an edge-to-edge image can extend behind the status bar while its title is placed inside a padded SafeArea.
Respect the user’s text size
Flutter’s text widgets use the text scaling information in MediaQuery. When a user increases the system font size, a Text widget normally grows without special code.
Do not manually multiply every font size by a scale factor. That can scale text twice and make it unexpectedly large. Prefer theme text styles and flexible layouts. Test long labels, wrapping, and buttons with large accessibility text sizes.
| Need | Recommended approach |
|---|---|
| Keep content away from system edges | Wrap the content in `SafeArea` |
| Read the current text scale | Use `MediaQuery.textScalerOf(context)` |
| Display ordinary text | Use `Text` and theme styles |
| Fit text at every size | Allow wrapping and avoid fixed heights |
A fixed-height card may clip when text becomes larger. Use minHeight, flexible columns, or content-driven sizing instead. A Row can also overflow when two labels grow. Replace it with a Column, Wrap, or an Expanded child where appropriate.
Exercise
Exercise
Why should a screen usually use SafeArea and avoid manually multiplying Text font sizes?
Show answer
SafeArea accounts for system-obscured edges, while Flutter text widgets already honor MediaQuery text scaling. Manual multiplication can scale text twice.