theme.dart 1.9 KB

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