Implementing Flutter Theming and Adaptive Design
Contents
- Core Theming Concepts
- Material 3 Guidelines
- Component Theme Normalization
- Button Styling
- Workflows
- Examples
Core Theming Concepts
Flutter applies styling in a strict hierarchy: styles applied to the specific widget -> themes that override the immediate parent theme -> the main app theme.
- Define app-wide themes using the
themeproperty ofMaterialAppwith aThemeDatainstance. - Override themes for specific widget subtrees by wrapping them in a
Themewidget and usingTheme.of(context).copyWith(...). - Do not use deprecated
ThemeDataproperties:- Replace
accentColorwithcolorScheme.secondary. - Replace
accentTextThemewithtextTheme(usingcolorScheme.onSecondaryfor contrast). - Replace
AppBarTheme.colorwithAppBarTheme.backgroundColor.
- Replace
Material 3 Guidelines
Material 3 is the default theme as of Flutter 3.16.
- Colors: Generate color schemes using
ColorScheme.fromSeed(seedColor: Colors.blue). This ensures accessible contrast ratios. - Elevation: Material 3 uses
ColorScheme.surfaceTintto indicate elevation instead of just drop shadows. To revert to M2 shadow behavior, setsurfaceTint: Colors.transparentand define ashadowColor. - Typography: Material 3 updates font sizes, weights, and line heights. If text wrapping breaks legacy layouts, adjust
letterSpacingon the specificTextStyle. - Modern Components:
- Replace
BottomNavigationBarwithNavigationBar. - Replace
DrawerwithNavigationDrawer. - Replace
ToggleButtonswithSegmentedButton. - Use
FilledButtonfor a high-emphasis button without the elevation ofElevatedButton.
- Replace
Component Theme Normalization
When defining ThemeData, use the *ThemeData suffix:
cardTheme: UseCardThemeData(NotCardTheme)dialogTheme: UseDialogThemeData(NotDialogTheme)tabBarTheme: UseTabBarThemeData(NotTabBarTheme)appBarTheme: UseAppBarThemeData(NotAppBarTheme)bottomAppBarTheme: UseBottomAppBarThemeData(NotBottomAppBarTheme)inputDecorationTheme: UseInputDecorationThemeData(NotInputDecorationTheme)
Button Styling
Legacy button classes (FlatButton, RaisedButton, OutlineButton) are obsolete.
- Use
TextButton,ElevatedButton, andOutlinedButton. - Configure button appearance using a
ButtonStyleobject. - For simple overrides:
TextButton.styleFrom(foregroundColor: Colors.blue). - For state-dependent styling: use
WidgetStateProperty.resolveWith.
Workflows
Workflow: Material 3 ThemeData Setup
- Use
ColorScheme.fromSeed()for color generation. - Use
*ThemeDataclasses for component properties. - Replace legacy buttons with modern equivalents.
- Replace
BottomNavigationBarwithNavigationBar. - Replace
DrawerwithNavigationDrawer.
Examples
Example: Modern Material 3 ThemeData Setup
MaterialApp(
title: 'My App',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.deepPurple,
brightness: Brightness.light,
),
appBarTheme: const AppBarThemeData(
backgroundColor: Colors.deepPurple,
elevation: 0,
),
cardTheme: const CardThemeData(
elevation: 2,
),
textTheme: const TextTheme(
bodyMedium: TextStyle(letterSpacing: 0.2),
),
),
home: const MyHomePage(),
);
Example: State-Dependent ButtonStyle
TextButton(
style: ButtonStyle(
foregroundColor: WidgetStateProperty.all<Color>(Colors.blue),
overlayColor: WidgetStateProperty.resolveWith<Color?>(
(Set<WidgetState> states) {
if (states.contains(WidgetState.hovered)) {
return Colors.blue.withOpacity(0.04);
}
if (states.contains(WidgetState.focused) ||
states.contains(WidgetState.pressed)) {
return Colors.blue.withOpacity(0.12);
}
return null;
},
),
),
onPressed: () {},
child: const Text('Button'),
)
Source: openplaybooks-dev/converge — distributed by TomeVault.