selfprivacy.org.app/lib/theming/factory/app_theme_factory.dart

66 lines
1.9 KiB
Dart
Raw Normal View History

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';
import 'package:material_color_utilities/palettes/core_palette.dart';
2022-05-03 10:45:10 +00:00
abstract class AppThemeFactory {
AppThemeFactory._();
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
final ColorScheme? dynamicColorsScheme =
await _getDynamicColors(brightness);
2022-05-03 10:45:10 +00:00
2022-06-05 19:36:32 +00:00
final ColorScheme fallbackColorScheme = ColorScheme.fromSeed(
seedColor: fallbackColor,
2022-05-03 10:45:10 +00:00
brightness: brightness,
);
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,
2022-05-16 22:41:00 +00:00
scaffoldBackgroundColor: colorScheme.background,
2022-05-03 10:45:10 +00:00
);
return materialThemeData;
}
2022-06-05 19:36:32 +00:00
static Future<ColorScheme?> _getDynamicColors(final Brightness brightness) {
2022-05-03 10:45:10 +00:00
try {
return DynamicColorPlugin.getCorePalette().then(
(final corePallet) => corePallet?.toColorScheme(brightness: brightness),
);
2022-05-03 10:45:10 +00:00
} on PlatformException {
return Future.value(null);
}
}
static Future<CorePalette?> getCorePalette() async {
try {
return await DynamicColorPlugin.getCorePalette();
} on PlatformException {
return Future.value(null);
}
}
2022-05-03 10:45:10 +00:00
}