timer_settings.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import 'package:shared_preferences/shared_preferences.dart';
  2. class TimerSettings {
  3. static const String _soundKey = 'timer_sound';
  4. static const String _volumeKey = 'timer_volume';
  5. static const String _vibrateKey = 'timer_vibrate';
  6. static const String _loopKey = 'timer_loop';
  7. static const String _alignToHourKey = 'timer_align_to_hour';
  8. String sound;
  9. double volume;
  10. bool vibrate; // 是否震动
  11. bool loop; // 是否循环
  12. bool alignToHour; // 整点对齐
  13. TimerSettings({
  14. this.sound = 'Dripping',
  15. this.volume = 0.7,
  16. this.vibrate = true,
  17. this.loop = true,
  18. this.alignToHour = false,
  19. });
  20. // Save settings to SharedPreferences
  21. Future<void> saveSettings() async {
  22. final prefs = await SharedPreferences.getInstance();
  23. await prefs.setString(_soundKey, sound);
  24. await prefs.setDouble(_volumeKey, volume);
  25. await prefs.setBool(_vibrateKey, vibrate);
  26. await prefs.setBool(_loopKey, loop);
  27. await prefs.setBool(_alignToHourKey, alignToHour);
  28. }
  29. // Load settings from SharedPreferences
  30. static Future<TimerSettings> loadSettings() async {
  31. final prefs = await SharedPreferences.getInstance();
  32. return TimerSettings(
  33. sound: prefs.getString(_soundKey) ?? 'Dripping',
  34. volume: prefs.getDouble(_volumeKey) ?? 0.7,
  35. vibrate: prefs.getBool(_vibrateKey) ?? true,
  36. loop: prefs.getBool(_loopKey) ?? true,
  37. alignToHour: prefs.getBool(_alignToHourKey) ?? false,
  38. );
  39. }
  40. }