38 lines
1016 B
Dart
38 lines
1016 B
Dart
/// [ChangeNotifier] that owns [ThemeMode] and persists it locally.
|
|
library;
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../services/storage_service.dart';
|
|
import '../utils/helpers.dart';
|
|
|
|
/// Provides [themeMode] to [MaterialApp] and writes changes to storage.
|
|
class ThemeController extends ChangeNotifier {
|
|
ThemeController(this._storage) {
|
|
_themeMode = themeModeFromName(_storage.themeModeName);
|
|
}
|
|
|
|
final StorageService _storage;
|
|
late ThemeMode _themeMode;
|
|
|
|
ThemeMode get themeMode => _themeMode;
|
|
|
|
bool get isDark => _themeMode == ThemeMode.dark;
|
|
|
|
Future<void> setThemeMode(ThemeMode mode) async {
|
|
if (mode == _themeMode) return;
|
|
_themeMode = mode;
|
|
notifyListeners();
|
|
await _storage.setThemeModeName(nameFromThemeMode(mode));
|
|
}
|
|
|
|
Future<void> cycleThemeMode() {
|
|
final next = switch (_themeMode) {
|
|
ThemeMode.system => ThemeMode.light,
|
|
ThemeMode.light => ThemeMode.dark,
|
|
ThemeMode.dark => ThemeMode.system,
|
|
};
|
|
return setThemeMode(next);
|
|
}
|
|
}
|