Sub topic 3
Typography with TextTheme
This chapter assumes you know Dart null safety, MaterialApp and Scaffold, and how BuildContext works. It covers the theme features that Material 3 uses in Flutter.
Typography with TextTheme
TextTheme holds named text styles such as headlineMedium and bodyLarge. Widgets read these styles instead of hard-coding sizes. Change the style once in ThemeData, and every widget that uses it updates.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final base = ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF6750A4)),
);
return MaterialApp(
theme: base.copyWith(
textTheme: base.textTheme.copyWith(
headlineMedium: base.textTheme.headlineMedium?.copyWith(
fontWeight: FontWeight.w700,
),
),
filledButtonTheme: FilledButtonThemeData(
style: FilledButton.styleFrom(minimumSize: const Size(120, 48)),
),
),
home: Scaffold(
body: Builder(
builder: (context) => Center(
child: Text(
'Hello',
style: Theme.of(context).textTheme.headlineMedium,
),
),
),
),
);
}
}Customizing components with component themes
Component themes such as appBarTheme and filledButtonTheme set the default look of every widget of that type. The example above sets the button size once for the whole app.
Letting users switch between light, dark and system
Set theme for light mode, darkTheme for dark mode, and themeMode to choose which one applies. ThemeMode.system follows the device setting. Store the current mode in a ValueNotifier<ThemeMode>, and rebuild MaterialApp with a ValueListenableBuilder when it changes.
Saving the user's theme preference
Save the mode with shared_preferences by storing mode.name. On startup, read the value back with ThemeMode.values.asNameMap(), and fall back to ThemeMode.system if nothing valid is saved.
Adding your own tokens with ThemeExtension
Subclass ThemeExtension<T> to hold app-specific values, such as a success color. Implement copyWith and lerp so the values animate correctly when the theme changes. Add the extension to ThemeData.extensions, and read it with Theme.of(context).extension<BrandColors>().
Using dynamic color on supported platforms
The dynamic_color package provides DynamicColorBuilder, which returns the platform's color schemes when they exist. Use lightDynamic ?? ColorScheme.fromSeed(...) as the fallback, because some platforms do not provide dynamic colors.
Checking contrast and accessibility
Test every color pair in both light and dark themes. The WCAG AA minimums are listed below.
| Text type | Minimum contrast ratio |
|---|---|
| Body text | 4.5:1 |
| Large text (18pt or 14pt bold and larger) | 3:1 |
Exercise
You set darkTheme on MaterialApp, but the app still shows light colors when the device is in dark mode. What is the most likely cause, and how do you fix it?
Show answer
themeMode is probably set to ThemeMode.light, or it is missing so the default applies. Set themeMode: ThemeMode.system (or ThemeMode.dark) so Flutter chooses darkTheme when appropriate.