Theming and Material 3
Colours and fonts should not be hardcoded inside widgets. The central place in Flutter is `ThemeData`: you pass it to MaterialApp(theme: ...) and read it anywhere down the tree with Theme.of(context).
Material 3 (Flutter's modern default) simplifies this: a whole coordinated colour scheme is generated from a single seedColor:
ColorScheme.fromSeed(seedColor: ...)— once for light, once for dark- you supply
themeanddarkTheme, andthemeModedecides which is active ThemeMode.systemfollows the device setting
| Approach | Result |
|---|---|
| Hardcoding `Colors.blue` in a widget | Unreadable in dark mode; a brand change means editing every file |
| `Theme.of(context).colorScheme.primary` | Correct in both modes, changed from one place |
| Writing `TextStyle(fontSize: 16)` | Typography drifts between screens |
| `Theme.of(context).textTheme.bodyLarge` | Consistent typography that respects accessibility settings |
Interview tip. The right answer to "how do you support dark mode?" is not "I check a flag and use black". A strong answer: I define theme and darkTheme separately, follow the device with themeMode: ThemeMode.system, and keep every colour out of widget code — it all comes from colorScheme. One step further: mention that ColorScheme.fromSeed derives contrast-appropriate pairs for you.
📚 Sources and documentation
- Using themesofficialdocs.flutter.dev
- ThemeData classofficialapi.flutter.dev
- Theme classofficialapi.flutter.dev
- Material Designofficialdocs.flutter.dev