full app done
This commit is contained in:
@@ -4,12 +4,11 @@ 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);
|
||||
_themeMode = _fromName(_storage.themeModeName);
|
||||
}
|
||||
|
||||
final StorageService _storage;
|
||||
@@ -17,21 +16,37 @@ class ThemeController extends ChangeNotifier {
|
||||
|
||||
ThemeMode get themeMode => _themeMode;
|
||||
|
||||
bool get isDark => _themeMode == ThemeMode.dark;
|
||||
bool get isDark =>
|
||||
_themeMode == ThemeMode.dark ||
|
||||
(_themeMode == ThemeMode.system &&
|
||||
WidgetsBinding.instance.platformDispatcher.platformBrightness ==
|
||||
Brightness.dark);
|
||||
|
||||
Future<void> setThemeMode(ThemeMode mode) async {
|
||||
if (mode == _themeMode) return;
|
||||
_themeMode = mode;
|
||||
notifyListeners();
|
||||
await _storage.setThemeModeName(nameFromThemeMode(mode));
|
||||
await _storage.setThemeModeName(_toName(mode));
|
||||
}
|
||||
|
||||
Future<void> cycleThemeMode() {
|
||||
final next = switch (_themeMode) {
|
||||
ThemeMode.system => ThemeMode.light,
|
||||
ThemeMode.light => ThemeMode.dark,
|
||||
ThemeMode.dark => ThemeMode.system,
|
||||
};
|
||||
return setThemeMode(next);
|
||||
/// Toggles between light and dark (moon / sun control).
|
||||
Future<void> toggleLightDark() {
|
||||
return setThemeMode(isDark ? ThemeMode.light : ThemeMode.dark);
|
||||
}
|
||||
}
|
||||
|
||||
ThemeMode _fromName(String name) {
|
||||
return switch (name) {
|
||||
'dark' => ThemeMode.dark,
|
||||
'system' => ThemeMode.system,
|
||||
_ => ThemeMode.light,
|
||||
};
|
||||
}
|
||||
|
||||
String _toName(ThemeMode mode) {
|
||||
return switch (mode) {
|
||||
ThemeMode.light => 'light',
|
||||
ThemeMode.dark => 'dark',
|
||||
ThemeMode.system => 'system',
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user