Sub topic 9
Customizing components with component themes
Assume you know basic Dart syntax, can build widget trees with StatelessWidget and StatefulWidget, and have used MaterialApp, Scaffold, and BuildContext. This chapter focuses on setting defaults for Material components instead of styling each widget separately.
A component theme is a group of default values for one widget type. For example, AppBarTheme configures app bars, while FilledButtonThemeData configures filled buttons. These values live in ThemeData, so the whole app can share one visual system.
Why component themes matter
Without component themes, an app often contains repeated styling:
- Each
AppBarsets its own title alignment. - Each
FilledButtonrepeats its shape and minimum size. - Different screens slowly develop different button styles.
A component theme centralizes those defaults. A widget can still override the default when one instance needs a different appearance.
Defining component themes
Set component themes inside ThemeData. This example uses Material 3 and defines defaults for app bars and filled buttons.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final theme = ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
),
appBarTheme: const AppBarTheme(
centerTitle: true,
elevation: 0,
),
filledButtonTheme: FilledButtonThemeData(
style: FilledButton.styleFrom(
minimumSize: const Size(120, 48),
padding: const EdgeInsets.symmetric(horizontal: 24),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
);
return MaterialApp(
theme: theme,
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Orders')),
body: Center(
child: FilledButton(
onPressed: () {},
child: const Text('Place order'),
),
),
);
}
}The FilledButton receives its size, padding, and shape automatically. The button does not need a local style argument.
Defaults and local overrides
Flutter resolves styles from broad defaults to specific settings:
ThemeDatasupplies the global theme.- A component theme supplies defaults for one component type.
- A widget's local properties override those defaults.
For example, this button keeps the component theme's shape but changes its minimum size:
FilledButton(
style: FilledButton.styleFrom(
minimumSize: const Size(200, 56),
),
onPressed: () {},
child: const Text('Continue'),
)Use local overrides for genuine exceptions. If many widgets need the same change, update the component theme instead.
Component themes and light or dark themes
When an app has both theme and darkTheme, define matching component themes in both ThemeData objects. Keep dimensions and shapes consistent, but choose colors through each theme's ColorScheme.
A component theme controls defaults, not every visual detail. State changes such as pressed, disabled, and hovered may require state-aware button styles. Test those states on each platform your app supports.
Exercise
A team repeats the same FilledButton shape on eight screens. Should each button keep its local style?
Show answer
No. Move the shared shape into FilledButtonThemeData. Keep a local style only for buttons that are intentional exceptions.