ThemeProvider.dart 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import 'package:flutter/material.dart';
  2. import '../common/LocalData.dart';
  3. class ThemeProvider extends ChangeNotifier {
  4. int? currentIndex;
  5. MaterialColor? currentMaterialColor;
  6. Brightness? currentBrightness;
  7. late List<Color> otherColors;
  8. ThemeProvider() {
  9. currentIndex = 0;
  10. currentMaterialColor = Colors.amber;
  11. currentBrightness = Brightness.light;
  12. otherColors = [];
  13. }
  14. void init() {
  15. int? index = LocalData.getInstance()!.getInt("theme");
  16. if (index == null) {
  17. index = 0;
  18. LocalData.getInstance()!.setInt("theme", index);
  19. }
  20. changeTheme(index);
  21. debugPrint(
  22. "init ThemeProvider to: $currentMaterialColor, $currentBrightness");
  23. }
  24. void changeTheme(int index) {
  25. if (index == themeColors.length) {
  26. currentMaterialColor = Colors.teal;
  27. currentBrightness = Brightness.dark;
  28. } else {
  29. currentMaterialColor = themeColors[index];
  30. currentBrightness = Brightness.light;
  31. }
  32. currentIndex = index;
  33. LocalData.getInstance()!.setInt("theme", index);
  34. otherColors = List<Color>.of(themeColors);
  35. otherColors.remove(currentMaterialColor);
  36. notifyListeners();
  37. }
  38. }
  39. List<MaterialColor> themeColors = [
  40. Colors.amber,
  41. Colors.pink,
  42. Colors.green,
  43. Colors.purple,
  44. Colors.blue,
  45. Colors.cyan,
  46. Colors.brown,
  47. ];