Files

69 lines
1.9 KiB
Dart
Raw Permalink Normal View History

// Appearance preferences: theme mode and seed colour. Device-local — the
// phone draws its own Material theme, so none of this is sent to the server.
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class AppearancePrefs extends ChangeNotifier {
AppearancePrefs._();
static final AppearancePrefs instance = AppearancePrefs._();
static const _modeKey = 'appearance.themeMode.v1';
static const _seedKey = 'appearance.seedColor.v1';
/// The first-party client's colour; a fresh install starts here.
static const defaultSeed = 0xFF6C5CE7;
ThemeMode _themeMode = ThemeMode.system;
int _seed = defaultSeed;
bool _loaded = false;
ThemeMode get themeMode => _themeMode;
Color get seedColor => Color(_seed);
Future<void> ensureLoaded() async {
if (_loaded) return;
_loaded = true;
try {
final prefs = await SharedPreferences.getInstance();
_themeMode = switch (prefs.getString(_modeKey)) {
'light' => ThemeMode.light,
'dark' => ThemeMode.dark,
_ => ThemeMode.system,
};
_seed = prefs.getInt(_seedKey) ?? defaultSeed;
notifyListeners();
} catch (_) {}
}
Future<void> setThemeMode(ThemeMode mode) async {
if (mode == _themeMode) return;
_themeMode = mode;
notifyListeners();
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_modeKey, mode.name);
} catch (_) {}
}
Future<void> setSeedColor(int value) async {
if (value == _seed) return;
_seed = value;
notifyListeners();
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_seedKey, value);
} catch (_) {}
}
}
/// The colours the appearance section offers; the first is the default.
const appearanceSeeds = <int>[
0xFF6C5CE7,
0xFF2F80ED,
0xFF00A884,
0xFF0FA3B1,
0xFFF2994A,
0xFFEB5757,
0xFFE84393,
0xFF6B7280,
];