Centralize colors and typography in ThemeData, generate a Material 3 palette with ColorScheme.fromSeed, and offer a dark mode effortlessly.
Open this lesson in KodokonStopping styling widget by widget is a professional milestone: ThemeData centralizes colors, typography and shapes for the entire app. With Material 3 (enabled by default since Flutter 3.16), everything starts from the ColorScheme: ColorScheme.fromSeed derives a complete, harmonious and accessible palette from a single seed color.
import 'package:flutter/material.dart';
void main() {
runApp(const ShopApp());
}
class ShopApp extends StatelessWidget {
const ShopApp({super.key});
@override
Widget build(BuildContext context) {
const seed = Colors.teal;
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: seed,
),
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: seed,
brightness: Brightness.dark,
),
),
themeMode: ThemeMode.system,
home: Scaffold(
appBar: AppBar(title: const Text('Shop')),
body: const Center(child: Text('Welcome')),
),
);
}
}Inside your widgets, don't reinvent anything: read the ambient theme with Theme.of(context). The colorScheme exposes semantic roles (primary, onPrimary, primaryContainer, surface, and so on) and textTheme exposes named styles (titleMedium, bodyLarge, and so on). A widget written this way adapts automatically to the light theme as well as the dark one.
import 'package:flutter/material.dart';
class PriceTag extends StatelessWidget {
const PriceTag({super.key, required this.price});
final double price;
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
final text = Theme.of(context).textTheme;
return Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 6,
),
decoration: BoxDecoration(
color: colors.primaryContainer,
borderRadius: BorderRadius.circular(8),
),
child: Text(
'$price €',
style: text.titleMedium?.copyWith(
color: colors.onPrimaryContainer,
),
),
);
}
}To go further, configure the component themes: each family of widgets (app bars, buttons, input fields) accepts a dedicated theme in ThemeData. You define your buttons' padding or your titles' centering once, and every instance inherits it without a single local style.
import 'package:flutter/material.dart';
final ThemeData appTheme = ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
),
appBarTheme: const AppBarTheme(
centerTitle: true,
elevation: 0,
),
filledButtonTheme: FilledButtonThemeData(
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 12,
),
),
),
);