94 lines
3.0 KiB
Dart
94 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
// 1. 앱에서 사용할 색상표 정의
|
|
final Map<String, MaterialColor> appColors = {
|
|
'Blue': Colors.blue,
|
|
'Green': Colors.green,
|
|
'Red': Colors.red,
|
|
'Purple': Colors.purple,
|
|
'Orange': Colors.orange,
|
|
'Teal': Colors.teal,
|
|
};
|
|
|
|
class ThemeNotifier with ChangeNotifier {
|
|
final String _themeKey = 'selected_theme';
|
|
final String _darkModeKey = 'is_dark_mode'; // 다크 모드 저장 키
|
|
|
|
MaterialColor _currentColor = Colors.blue; // 기본값
|
|
bool _isDarkMode = false; // 다크 모드 상태 변수
|
|
|
|
// --- Getters ---
|
|
|
|
// 라이트 모드용 테마
|
|
ThemeData get currentTheme => ThemeData(
|
|
// 🔽 [수정] M3의 권장 방식인 ColorScheme.fromSeed 사용
|
|
colorScheme: ColorScheme.fromSeed(
|
|
seedColor: _currentColor, // 👈 _currentColor를 '씨앗색'으로 사용
|
|
brightness: Brightness.light,
|
|
),
|
|
useMaterial3: true,
|
|
brightness: Brightness.light,
|
|
// 🔺 'primarySwatch' 속성 제거
|
|
);
|
|
|
|
// 다크 모드용 테마
|
|
ThemeData get currentDarkTheme => ThemeData(
|
|
// 🔽 [수정] 다크 모드에도 동일하게 적용
|
|
colorScheme: ColorScheme.fromSeed(
|
|
seedColor: _currentColor, // 👈 _currentColor를 '씨앗색'으로 사용
|
|
brightness: Brightness.dark,
|
|
),
|
|
useMaterial3: true,
|
|
brightness: Brightness.dark,
|
|
// 🔺 'primarySwatch' 속성 제거
|
|
);
|
|
|
|
// MaterialApp에 전달할 현재 테마 모드
|
|
ThemeMode get currentThemeMode => _isDarkMode ? ThemeMode.dark : ThemeMode.light;
|
|
|
|
// SettingsScreen에서 사용할 현재 상태
|
|
bool get isDarkMode => _isDarkMode;
|
|
MaterialColor get currentColor => _currentColor;
|
|
|
|
// --- Methods ---
|
|
|
|
ThemeNotifier() {
|
|
_loadTheme(); // 앱 시작 시 저장된 설정 불러오기
|
|
}
|
|
|
|
// 저장된 테마와 '다크 모드' 설정을 함께 불러오기
|
|
void _loadTheme() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
// 색상 로드
|
|
final themeName = prefs.getString(_themeKey) ?? 'Blue';
|
|
_currentColor = appColors[themeName] ?? Colors.blue;
|
|
|
|
// 다크 모드 로드
|
|
_isDarkMode = prefs.getBool(_darkModeKey) ?? false;
|
|
|
|
notifyListeners(); // 설정 로드 후 UI 갱신
|
|
}
|
|
|
|
// 새 테마 색상 설정
|
|
void setTheme(String themeName) async {
|
|
final newColor = appColors[themeName];
|
|
if (newColor == null) return;
|
|
|
|
_currentColor = newColor;
|
|
notifyListeners(); // 테마 변경을 앱 전체에 알림
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
prefs.setString(_themeKey, themeName); // 선택한 테마 이름 저장
|
|
}
|
|
|
|
// 다크 모드 토글
|
|
void toggleTheme(bool isDark) async {
|
|
_isDarkMode = isDark;
|
|
notifyListeners(); // 모드 변경을 앱 전체에 알림
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
prefs.setBool(_darkModeKey, isDark); // 다크 모드 상태 저장
|
|
}
|
|
} |