2022-05-03 10:45:10 +00:00
|
|
|
import 'package:dynamic_color/dynamic_color.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2022-12-31 02:33:04 +00:00
|
|
|
import 'package:material_color_utilities/palettes/core_palette.dart';
|
2022-05-03 10:45:10 +00:00
|
|
|
|
|
|
|
abstract class AppThemeFactory {
|
|
|
|
AppThemeFactory._();
|
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
static Future<ThemeData> create({
|
|
|
|
required final bool isDark,
|
|
|
|
required final Color fallbackColor,
|
|
|
|
}) =>
|
|
|
|
_createAppTheme(
|
|
|
|
isDark: isDark,
|
|
|
|
fallbackColor: fallbackColor,
|
|
|
|
);
|
2022-05-03 10:45:10 +00:00
|
|
|
|
|
|
|
static Future<ThemeData> _createAppTheme({
|
2022-06-05 19:36:32 +00:00
|
|
|
required final Color fallbackColor,
|
|
|
|
final bool isDark = false,
|
2022-05-03 10:45:10 +00:00
|
|
|
}) async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final Brightness brightness = isDark ? Brightness.dark : Brightness.light;
|
2022-05-03 10:45:10 +00:00
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
final ColorScheme? dynamicColorsScheme =
|
|
|
|
await _getDynamicColors(brightness);
|
2022-05-03 10:45:10 +00:00
|
|
|
|
2024-04-02 15:11:29 +00:00
|
|
|
final Color? accentColor = await _getAccentColor();
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final ColorScheme fallbackColorScheme = ColorScheme.fromSeed(
|
2024-04-02 15:11:29 +00:00
|
|
|
seedColor: accentColor ?? fallbackColor,
|
2022-05-03 10:45:10 +00:00
|
|
|
brightness: brightness,
|
|
|
|
);
|
|
|
|
|
2022-12-31 04:16:10 +00:00
|
|
|
final ColorScheme colorScheme = dynamicColorsScheme ?? fallbackColorScheme;
|
2022-05-03 10:45:10 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Typography appTypography = Typography.material2021();
|
2022-05-03 10:45:10 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final ThemeData materialThemeData = ThemeData(
|
2022-09-16 09:54:18 +00:00
|
|
|
visualDensity: VisualDensity.adaptivePlatformDensity,
|
2022-05-03 10:45:10 +00:00
|
|
|
colorScheme: colorScheme,
|
|
|
|
brightness: colorScheme.brightness,
|
|
|
|
typography: appTypography,
|
|
|
|
useMaterial3: true,
|
2024-05-15 15:47:00 +00:00
|
|
|
listTileTheme: ListTileThemeData(
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(12),
|
|
|
|
),
|
|
|
|
),
|
2022-05-03 10:45:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return materialThemeData;
|
|
|
|
}
|
|
|
|
|
2024-08-21 09:22:20 +00:00
|
|
|
static Future<ColorScheme?> _getDynamicColors(
|
2024-08-23 09:42:07 +00:00
|
|
|
final Brightness brightness,
|
|
|
|
) async {
|
2024-08-21 09:22:20 +00:00
|
|
|
List<Color> extractAdditionalColours(final ColorScheme scheme) => [
|
|
|
|
scheme.surface,
|
|
|
|
scheme.surfaceDim,
|
|
|
|
scheme.surfaceBright,
|
|
|
|
scheme.surfaceContainerLowest,
|
|
|
|
scheme.surfaceContainerLow,
|
|
|
|
scheme.surfaceContainer,
|
|
|
|
scheme.surfaceContainerHigh,
|
|
|
|
scheme.surfaceContainerHighest,
|
|
|
|
];
|
|
|
|
|
|
|
|
ColorScheme insertAdditionalColours(
|
2024-08-23 09:42:07 +00:00
|
|
|
final ColorScheme scheme,
|
|
|
|
final List<Color> additionalColours,
|
|
|
|
) =>
|
2024-08-21 09:22:20 +00:00
|
|
|
scheme.copyWith(
|
|
|
|
surface: additionalColours[0],
|
|
|
|
surfaceDim: additionalColours[1],
|
|
|
|
surfaceBright: additionalColours[2],
|
|
|
|
surfaceContainerLowest: additionalColours[3],
|
|
|
|
surfaceContainerLow: additionalColours[4],
|
|
|
|
surfaceContainer: additionalColours[5],
|
|
|
|
surfaceContainerHigh: additionalColours[6],
|
|
|
|
surfaceContainerHighest: additionalColours[7],
|
|
|
|
);
|
|
|
|
|
2022-05-03 10:45:10 +00:00
|
|
|
try {
|
2024-08-21 09:22:20 +00:00
|
|
|
final ColorScheme? colorScheme =
|
|
|
|
await DynamicColorPlugin.getCorePalette().then(
|
|
|
|
(final corePalette) =>
|
|
|
|
corePalette?.toColorScheme(brightness: brightness),
|
2022-06-05 22:40:34 +00:00
|
|
|
);
|
2024-08-21 09:22:20 +00:00
|
|
|
if (colorScheme == null) {
|
|
|
|
return Future.value(null);
|
|
|
|
}
|
|
|
|
// Workaround as dynamic_color doesn't add new color roles
|
|
|
|
final fromSeed = ColorScheme.fromSeed(
|
2024-08-23 09:42:07 +00:00
|
|
|
seedColor: colorScheme.primary,
|
|
|
|
brightness: brightness,
|
|
|
|
);
|
2024-08-21 09:22:20 +00:00
|
|
|
final additionalColours = extractAdditionalColours(fromSeed);
|
|
|
|
final updatedColorScheme =
|
|
|
|
insertAdditionalColours(colorScheme, additionalColours);
|
|
|
|
return Future.value(updatedColorScheme);
|
2022-05-03 10:45:10 +00:00
|
|
|
} on PlatformException {
|
|
|
|
return Future.value(null);
|
|
|
|
}
|
|
|
|
}
|
2022-12-31 02:33:04 +00:00
|
|
|
|
2024-04-02 15:11:29 +00:00
|
|
|
static Future<Color?> _getAccentColor() {
|
|
|
|
try {
|
|
|
|
return DynamicColorPlugin.getAccentColor();
|
|
|
|
} on PlatformException {
|
|
|
|
return Future.value(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-31 02:33:04 +00:00
|
|
|
static Future<CorePalette?> getCorePalette() async {
|
|
|
|
try {
|
|
|
|
return await DynamicColorPlugin.getCorePalette();
|
|
|
|
} on PlatformException {
|
|
|
|
return Future.value(null);
|
|
|
|
}
|
|
|
|
}
|
2022-05-03 10:45:10 +00:00
|
|
|
}
|