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

89 lines
2.7 KiB
Dart
Raw Normal View History

2022-05-03 13:45:10 +03:00
import 'dart:io';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:system_theme/system_theme.dart';
import 'package:gtk_theme_fl/gtk_theme_fl.dart';
abstract class AppThemeFactory {
AppThemeFactory._();
static Future<ThemeData> create({
required final bool isDark,
required final Color fallbackColor,
}) =>
_createAppTheme(
isDark: isDark,
fallbackColor: fallbackColor,
);
2022-05-03 13:45:10 +03:00
static Future<ThemeData> _createAppTheme({
2022-06-05 22:36:32 +03:00
required final Color fallbackColor,
final bool isDark = false,
2022-05-03 13:45:10 +03:00
}) async {
ColorScheme? gtkColorsScheme;
2022-06-05 22:36:32 +03:00
final Brightness brightness = isDark ? Brightness.dark : Brightness.light;
2022-05-03 13:45:10 +03:00
final ColorScheme? dynamicColorsScheme =
await _getDynamicColors(brightness);
2022-05-03 13:45:10 +03:00
if (Platform.isLinux) {
2022-06-05 22:36:32 +03:00
final GtkThemeData themeData = await GtkThemeData.initialize();
final bool isGtkDark =
2022-05-17 02:42:46 +03:00
Color(themeData.theme_bg_color).computeLuminance() < 0.5;
2022-06-05 22:36:32 +03:00
final bool isInverseNeeded = isGtkDark != isDark;
2022-05-03 13:45:10 +03:00
gtkColorsScheme = ColorScheme.fromSeed(
seedColor: Color(themeData.theme_selected_bg_color),
2022-05-17 01:41:00 +03:00
brightness: brightness,
2022-05-17 02:42:46 +03:00
background: isInverseNeeded ? null : Color(themeData.theme_bg_color),
surface: isInverseNeeded ? null : Color(themeData.theme_base_color),
2022-05-03 13:45:10 +03:00
);
}
2022-06-05 22:36:32 +03:00
final SystemAccentColor accentColor = SystemAccentColor(fallbackColor);
try {
await accentColor.load();
} on MissingPluginException catch (e) {
2022-05-24 21:55:39 +03:00
print('_createAppTheme: ${e.message}');
}
2022-05-03 13:45:10 +03:00
2022-06-05 22:36:32 +03:00
final ColorScheme fallbackColorScheme = ColorScheme.fromSeed(
2022-05-03 13:45:10 +03:00
seedColor: accentColor.accent,
brightness: brightness,
);
2022-06-05 22:36:32 +03:00
final ColorScheme colorScheme =
2022-05-17 01:41:00 +03:00
dynamicColorsScheme ?? gtkColorsScheme ?? fallbackColorScheme;
2022-05-03 13:45:10 +03:00
2022-06-05 22:36:32 +03:00
final Typography appTypography = Typography.material2021();
2022-05-03 13:45:10 +03:00
2022-06-05 22:36:32 +03:00
final ThemeData materialThemeData = ThemeData(
2022-09-16 12:54:18 +03:00
visualDensity: VisualDensity.adaptivePlatformDensity,
2022-05-03 13:45:10 +03:00
colorScheme: colorScheme,
brightness: colorScheme.brightness,
typography: appTypography,
useMaterial3: true,
2022-05-17 01:41:00 +03:00
scaffoldBackgroundColor: colorScheme.background,
2022-05-03 13:45:10 +03:00
appBarTheme: AppBarTheme(
elevation: 0,
backgroundColor: colorScheme.primary,
foregroundColor: colorScheme.onPrimary,
),
);
return materialThemeData;
}
2022-06-05 22:36:32 +03:00
static Future<ColorScheme?> _getDynamicColors(final Brightness brightness) {
2022-05-03 13:45:10 +03:00
try {
return DynamicColorPlugin.getCorePalette().then(
(final corePallet) => corePallet?.toColorScheme(brightness: brightness),
);
2022-05-03 13:45:10 +03:00
} on PlatformException {
return Future.value(null);
}
}
}