theme.dart 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'package:flowy_infra/colorscheme/colorscheme.dart';
  2. import 'package:flowy_infra/colorscheme/default_colorscheme.dart';
  3. import 'plugins/service/plugin_service.dart';
  4. class BuiltInTheme {
  5. static const String defaultTheme = 'Default';
  6. static const String dandelion = 'Dandelion';
  7. static const String lavender = 'Lavender';
  8. }
  9. class AppTheme {
  10. // metadata member
  11. final bool builtIn;
  12. final String themeName;
  13. final FlowyColorScheme lightTheme;
  14. final FlowyColorScheme darkTheme;
  15. // static final Map<String, dynamic> _cachedJsonData = {};
  16. const AppTheme({
  17. required this.builtIn,
  18. required this.themeName,
  19. required this.lightTheme,
  20. required this.darkTheme,
  21. });
  22. static const AppTheme fallback = AppTheme(
  23. builtIn: true,
  24. themeName: BuiltInTheme.defaultTheme,
  25. lightTheme: DefaultColorScheme.light(),
  26. darkTheme: DefaultColorScheme.dark(),
  27. );
  28. static Future<Iterable<AppTheme>> _plugins(FlowyPluginService service) async {
  29. final plugins = await service.plugins;
  30. return plugins.map((plugin) => plugin.theme).whereType<AppTheme>();
  31. }
  32. static Iterable<AppTheme> get builtins => themeMap.entries
  33. .map(
  34. (entry) => AppTheme(
  35. builtIn: true,
  36. themeName: entry.key,
  37. lightTheme: entry.value[0],
  38. darkTheme: entry.value[1],
  39. ),
  40. )
  41. .toList();
  42. static Future<Iterable<AppTheme>> themes(FlowyPluginService service) async =>
  43. [
  44. ...builtins,
  45. ...(await _plugins(service)),
  46. ];
  47. static Future<AppTheme> fromName(
  48. String themeName, {
  49. FlowyPluginService? pluginService,
  50. }) async {
  51. pluginService ??= FlowyPluginService.instance;
  52. for (final theme in await themes(pluginService)) {
  53. if (theme.themeName == themeName) {
  54. return theme;
  55. }
  56. }
  57. throw ArgumentError('The theme $themeName does not exist.');
  58. }
  59. }