Themes, Material 3, Colors and Fonts
Why Use a Theme?
A theme stores your app's colors, fonts and widget styles in one place. Change it once and the whole app updates. It also makes dark mode almost free. Since Flutter 3.16, Material 3 is the default look.
One Color Creates a Palette
dart
MaterialApp(
title: 'My App',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.dark,
),
),
themeMode: ThemeMode.system, // follow the phone setting
home: const HomePage(),
)
ColorScheme.fromSeed makes a complete, accessible set of colors from one seed color: primary, secondary, surface, error and more.
Use Theme Colors in Widgets
dart
final scheme = Theme.of(context).colorScheme;
Container(
color: scheme.primaryContainer,
child: Text('Hello', style: TextStyle(color: scheme.onPrimaryContainer)),
)
Why not Colors.red? Fixed colors do not change in dark mode. Colors from the
colorScheme always have enough contrast on their matching background.
| ColorScheme role | Use |
|---|---|
primary / onPrimary | Main buttons and their text |
secondary / tertiary | Accents |
surface / onSurface | Cards, sheets and text on them |
error / onError | Errors |
Switch Between Light and Dark
dart
ThemeMode mode = ThemeMode.light;
// In a StatefulWidget:
Switch(
value: mode == ThemeMode.dark,
onChanged: (dark) => setState(() {
mode = dark ? ThemeMode.dark : ThemeMode.light;
}),
)
Text Theme and Component Themes
dart
ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
textTheme: const TextTheme(
headlineMedium: TextStyle(fontWeight: FontWeight.bold),
),
appBarTheme: const AppBarTheme(centerTitle: true),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
),
inputDecorationTheme: const InputDecorationTheme(border: OutlineInputBorder()),
)
Custom Fonts with google_fonts
bash
flutter pub add google_fonts
dart
import 'package:google_fonts/google_fonts.dart';
ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
textTheme: GoogleFonts.poppinsTextTheme(),
)
Custom Fonts from Files
yaml
flutter:
fonts:
- family: Pacifico
fonts:
- asset: assets/fonts/Pacifico-Regular.ttf
dart
Text('Hello', style: TextStyle(fontFamily: 'Pacifico'))
Tip: Define colors and styles in one
app_theme.dart file. Then your widgets never contain random hex colors.
Conclusion
A theme gives you consistency and dark mode with very little code. Start with ColorScheme.fromSeed and read colors from the theme.
Quick Summary: Use ThemeData with ColorScheme.fromSeed, set darkTheme and themeMode, and read colors from Theme.of(context).

