Back to results for “App Theming & Dark Mode”
Flutter › App Theming & Dark Mode

How do custom ThemeExtensions in Flutter let you add your own additional custom design tokens, beyond what the built in ThemeData already provides, to your app's central theme?

Intermediate

Answer

A ThemeExtension lets you define your own completely custom set of design values, such as a specific set of brand spacing measurements, custom semantic colors like a success or warning color not included in the standard ColorScheme, or custom typography variants specific to your particular app, and registering this custom extension within your ThemeData means these custom values become accessible from anywhere in your widget tree using the same familiar Theme.of pattern used for built in theme values, keeping all of your app's design decisions centralized in one consistent place.
class AppColors extends ThemeExtension<AppColors> {
  final Color success;
  const AppColors({required this.success});

  @override
  AppColors copyWith({Color? success}) => AppColors(success: success ?? this.success);

  @override
  AppColors lerp(AppColors? other, double t) => this;
}
Real-world example A finance app defines a custom AppColors theme extension containing a specific success green color for positive balance changes, accessing this custom value consistently throughout the app using the same Theme.of pattern used for all of Flutter's own built in theme colors.

Common follow-ups: How do you actually retrieve a custom theme extension's values from within a widget?;What is the lerp method on a ThemeExtension used for?