Sub topic 1
What Material 3 means in Flutter
Material 3 (M3) is the current version of Google's Material Design system. In Flutter it affects three things: how colors are generated and named, how text styles are organized, and how the built-in widgets look and behave. This chapter explains what changed from Material 2 (M2) and what you control through ThemeData. It also covers how to opt out. Later chapters cover each piece in detail.
Prerequisites
This chapter assumes you already know:
- Basic Dart syntax and null safety.
- How to build widget trees with
StatelessWidgetandStatefulWidget. - What
MaterialAppandScaffolddo. - What a
BuildContextis, at least at the level of "it locates a widget in the tree".
You also need Flutter 3.16 or later. Examples use a recent stable release.
Material 3 is the default
Before Flutter 3.16, M3 was opt-in. You had to pass useMaterial3: true to ThemeData. Since 3.16, ThemeData.useMaterial3 defaults to true. A new app created with flutter create uses M3 without any configuration.
This default is the main source of surprises when upgrading old projects. If you never touched useMaterial3, your app switched to M3 when you upgraded Flutter, and some visuals changed.
You can still opt out:
ThemeData(useMaterial3: false)Flutter marks this flag as a temporary migration aid. It is expected to be removed in a future release, so treat opting out as a stopgap and not a long-term setting.
What changes when M3 is on
M3 changes three layers of your app.
1. Color system
M2 used a small set of colors: primary, secondary, and a few others. M3 uses a larger ColorScheme with named roles. Each role pairs a background color with a color for content on top of it:
primaryandonPrimaryprimaryContainerandonPrimaryContainersecondary,tertiary, and their container variantssurfaceandonSurfaceerrorandonError
Widgets pick their colors from these roles. They do not hard-code them. This is why changing one seed color can restyle a whole app. Chapter 4 covers the roles, and chapter 5 covers generating them from a seed.
2. Typography
M3 defines 15 text styles in five groups, each with large, medium, and small sizes: display, headline, title, body, and label. TextTheme exposes them as displayLarge through labelSmall. Under M2 the names were different (headline1, bodyText2, and so on), and those old names are gone from current Flutter. Chapter 8 covers typography.
3. Component look and behavior
Many widgets render differently under M3. Common differences:
| Area | Material 2 | Material 3 |
|---|---|---|
| Bottom navigation | `BottomNavigationBar` | `NavigationBar` |
| Side navigation | `Drawer` | `NavigationDrawer` is available |
| Filled button | Not available | `FilledButton` |
| Segmented choice | `ToggleButtons` | `SegmentedButton` |
| Corner shapes | Small radii | Larger, rounder radii |
| Elevation | Drop shadows | Shadows plus tonal color changes on surfaces |
| App bar | Primary-colored by default | Surface-colored, changes tone when content scrolls under it |
BottomNavigationBar still exists and still works under M3. It is simply no longer the design system's recommended pattern. The M3 replacement is NavigationBar, and it has a different API. Migrating means rewriting that widget, not flipping a flag.
See the difference yourself
The following app shows the same widgets under both settings. Each half is wrapped in its own Theme widget, so both themes appear on screen at once. You will meet Theme again in the next chapter.
import 'package:flutter/material.dart';
void main() => runApp(const CompareApp());
class CompareApp extends StatelessWidget {
const CompareApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'M2 vs M3',
home: ComparePage(),
);
}
}
class ComparePage extends StatelessWidget {
const ComparePage({super.key});
static const _seed = Color(0xFF6750A4);
ThemeData _themeFor({required bool material3}) {
return ThemeData(
useMaterial3: material3,
colorScheme: ColorScheme.fromSeed(seedColor: _seed),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Material 2 vs Material 3')),
body: Row(
children: [
Expanded(
child: Theme(
data: _themeFor(material3: false),
child: const _Sample(label: 'useMaterial3: false'),
),
),
Expanded(
child: Theme(
data: _themeFor(material3: true),
child: const _Sample(label: 'useMaterial3: true'),
),
),
],
),
);
}
}
class _Sample extends StatelessWidget {
const _Sample({required this.label});
final String label;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return ColoredBox(
color: theme.colorScheme.surface,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(label, style: theme.textTheme.titleMedium),
const SizedBox(height: 16),
ElevatedButton(onPressed: () {}, child: const Text('Elevated')),
const SizedBox(height: 8),
FilledButton(onPressed: () {}, child: const Text('Filled')),
const SizedBox(height: 8),
const Card(
child: Padding(
padding: EdgeInsets.all(16),
child: Text('A card'),
),
),
const SizedBox(height: 8),
Switch(value: true, onChanged: (_) {}),
],
),
),
);
}
}Run it and compare the buttons, the card corners, and the switch. The M3 side has rounder shapes and a different switch design. The FilledButton appears on both sides because the widget exists regardless of the flag. Only its styling follows the theme.
How the pieces fit together
ThemeData is the object that holds all theming values. MaterialApp puts one into the widget tree, and widgets read from it. Each part of M3 occupies a field on that object.
The useMaterial3 flag changes the defaults that widgets fall back on. The other fields let you override those defaults. The order of precedence is worth remembering:
- A style set directly on the widget instance, such as
style:on a button. - The matching component theme, such as
filledButtonTheme. - The widget's own M3 defaults, derived from
ColorSchemeandTextTheme.
If something does not look the way you expect, walk through these three levels in order.
Common mistakes when adopting M3
- Assuming
primarySwatchstill drives everything.primarySwatchis an M2-era shortcut. Under M3 you should build aColorScheme, usually withColorScheme.fromSeed. Mixing the two gives confusing results becausecolorSchemetakes precedence for most widgets. - Using removed
TextThemenames.headline6and similar names no longer exist. The compiler will tell you, but the fix is to map each to an M3 role, for exampletitleLarge. - Hard-coding
Colors.bluein widgets. Hard-coded colors ignore the theme and break in dark mode. Read fromTheme.of(context).colorSchemeinstead. - Expecting the app bar to look the same after upgrading. Its surface color and scroll-under tint are M3 defaults. Override them with
appBarThemeif your design needs the old look. - Treating
useMaterial3: falseas a fix. It hides differences temporarily. Plan the migration rather than relying on the flag.
What the rest of this tutorial builds
The remaining chapters follow the layers above. You will read the theme with Theme.of(context). You will build light and dark color schemes from a seed with ColorScheme.fromSeed. You will wire theme, darkTheme, and themeMode on MaterialApp, then customize typography and component themes. The final chapters add a runtime theme switcher, persistence with shared_preferences, custom tokens with ThemeExtension, platform dynamic color, and contrast checks.
Exercise
You upgrade an app from Flutter 3.10 to a current stable release without editing any theme code. The app never set useMaterial3. Its buttons and app bar now look different. Why, and what are two ways to respond?
Show answer
Since Flutter 3.16, ThemeData.useMaterial3 defaults to true, so the app switched from M2 to M3 styling when the Flutter version changed. One response is to set useMaterial3: false as a temporary measure, remembering that the flag is expected to be removed eventually. The better response is to migrate: build a ColorScheme (for example with ColorScheme.fromSeed), replace removed or outdated widgets and TextTheme names with their M3 equivalents, and use component themes such as appBarTheme to restore any look you want to keep.
Exercise
A button has style: ButtonStyle(backgroundColor: ...) set on the widget. The app also sets filledButtonTheme with a different background color. Which color does the button show, and why?
Show answer
The color set on the widget instance wins. Styles on the instance take precedence over the component theme, and the component theme takes precedence over the widget's M3 defaults derived from ColorScheme.