Skip to content

Instantly share code, notes, and snippets.

@olivoil
Forked from SashaKryzh/app_colors_extension.dart
Created November 14, 2023 02:50
Show Gist options
  • Save olivoil/aab6fee44bf8228cf4f75248f3a06c3f to your computer and use it in GitHub Desktop.
Save olivoil/aab6fee44bf8228cf4f75248f3a06c3f to your computer and use it in GitHub Desktop.
Flutter ThemeExtension for custom app colors.
import 'package:flutter/material.dart';
/// `ThemeExtension` template for custom colors.
///
/// For example purposes, it has all required fields from the default Material `ColorScheme`.
/// But you can add, rename and delete any fields your need.
///
/// ### Motivation
///
/// At the beginning, you may not know if your colors will fit into the Material `ColorScheme`,
/// but you still decided to start using `ColorScheme`, and only then realize that you need additional fields.
/// You will create `ThemeExtension` for only the additional fields, and as the result, you will have your colors
/// scattered between the `ColorScheme` and `ThemeExtension` with a few extra colors.
///
/// With this template, you can collect all fields in one place,
/// and don't worry about future changes to the Material or your design.
///
/// Or you can just quickly copy-paste this file and rename fields to your needs.
class AppColorsExtension extends ThemeExtension<AppColorsExtension> {
AppColorsExtension({
required this.primary,
required this.onPrimary,
required this.secondary,
required this.onSecondary,
required this.error,
required this.onError,
required this.background,
required this.onBackground,
required this.surface,
required this.onSurface,
});
final Color primary;
final Color onPrimary;
final Color secondary;
final Color onSecondary;
final Color error;
final Color onError;
final Color background;
final Color onBackground;
final Color surface;
final Color onSurface;
@override
ThemeExtension<AppColorsExtension> copyWith({
Color? primary,
Color? onPrimary,
Color? secondary,
Color? onSecondary,
Color? error,
Color? onError,
Color? background,
Color? onBackground,
Color? surface,
Color? onSurface,
}) {
return AppColorsExtension(
primary: primary ?? this.primary,
onPrimary: onPrimary ?? this.onPrimary,
secondary: secondary ?? this.secondary,
onSecondary: onSecondary ?? this.onSecondary,
error: error ?? this.error,
onError: onError ?? this.onError,
background: background ?? this.background,
onBackground: onBackground ?? this.onBackground,
surface: surface ?? this.surface,
onSurface: onSurface ?? this.onSurface,
);
}
@override
ThemeExtension<AppColorsExtension> lerp(
covariant ThemeExtension<AppColorsExtension>? other,
double t,
) {
if (other is! AppColorsExtension) {
return this;
}
return AppColorsExtension(
primary: Color.lerp(primary, other.primary, t)!,
onPrimary: Color.lerp(onPrimary, other.onPrimary, t)!,
secondary: Color.lerp(secondary, other.secondary, t)!,
onSecondary: Color.lerp(onSecondary, other.onSecondary, t)!,
error: Color.lerp(error, other.error, t)!,
onError: Color.lerp(onError, other.onError, t)!,
background: Color.lerp(background, other.background, t)!,
onBackground: Color.lerp(onBackground, other.onBackground, t)!,
surface: Color.lerp(surface, other.surface, t)!,
onSurface: Color.lerp(onSurface, other.onSurface, t)!,
);
}
}
/// Optional. If you also want to assign colors in the `ColorScheme`.
extension ColorSchemeBuilder on AppColorsExtension {
ColorScheme toColorScheme(Brightness brightness) {
return ColorScheme(
brightness: brightness,
primary: primary,
onPrimary: onPrimary,
secondary: secondary,
onSecondary: onSecondary,
error: error,
onError: onError,
background: background,
onBackground: onBackground,
surface: surface,
onSurface: onSurface,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment