Skip to content

Commit 51ca3d4

Browse files
Add theme customization with color and background settings
- Introduced ThemeSettings model for comprehensive theme management - Created ThemeSettingsProvider for persistent theme configuration - Implemented ThemeSettingsScreen with color picker and preset themes - Added flutter_colorpicker dependency for advanced color selection - Enabled dynamic background image and color customization - Updated profile screen with theme settings navigation option
1 parent 27700e5 commit 51ca3d4

File tree

6 files changed

+508
-1
lines changed

6 files changed

+508
-1
lines changed

lib/data/models/theme_settings.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'dart:ui';
2+
3+
class ThemeSettings {
4+
final String backgroundImage;
5+
final Color primaryColor;
6+
final Color secondaryColor;
7+
final Color systemBubbleColor;
8+
final Color userBubbleColor;
9+
10+
const ThemeSettings({
11+
required this.backgroundImage,
12+
required this.primaryColor,
13+
required this.secondaryColor,
14+
required this.systemBubbleColor,
15+
required this.userBubbleColor,
16+
});
17+
18+
ThemeSettings copyWith({
19+
String? backgroundImage,
20+
Color? primaryColor,
21+
Color? secondaryColor,
22+
Color? systemBubbleColor,
23+
Color? userBubbleColor,
24+
}) {
25+
return ThemeSettings(
26+
backgroundImage: backgroundImage ?? this.backgroundImage,
27+
primaryColor: primaryColor ?? this.primaryColor,
28+
secondaryColor: secondaryColor ?? this.secondaryColor,
29+
systemBubbleColor: systemBubbleColor ?? this.systemBubbleColor,
30+
userBubbleColor: userBubbleColor ?? this.userBubbleColor,
31+
);
32+
}
33+
34+
factory ThemeSettings.fromJson(Map<String, dynamic> json) {
35+
return ThemeSettings(
36+
backgroundImage: json['backgroundImage'] as String,
37+
primaryColor: Color(json['primaryColor'] as int),
38+
secondaryColor: Color(json['secondaryColor'] as int),
39+
systemBubbleColor: Color(json['systemBubbleColor'] as int),
40+
userBubbleColor: Color(json['userBubbleColor'] as int),
41+
);
42+
}
43+
44+
Map<String, dynamic> toJson() => {
45+
'backgroundImage': backgroundImage,
46+
'primaryColor': primaryColor.value,
47+
'secondaryColor': secondaryColor.value,
48+
'systemBubbleColor': systemBubbleColor.value,
49+
'userBubbleColor': userBubbleColor.value,
50+
};
51+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import 'dart:convert';
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_chatgpt_text_and_image_processing/configs/theme/app_theme.dart';
5+
import 'package:flutter_riverpod/flutter_riverpod.dart';
6+
import 'package:shared_preferences/shared_preferences.dart';
7+
// import 'package:shared_preferences.dart';
8+
import '../models/theme_settings.dart';
9+
10+
final themeSettingsProvider = StateNotifierProvider<ThemeSettingsNotifier, ThemeSettings>((ref) {
11+
return ThemeSettingsNotifier();
12+
});
13+
14+
class ThemeSettingsNotifier extends StateNotifier<ThemeSettings> {
15+
static const String _storageKey = 'themeSettings';
16+
17+
ThemeSettingsNotifier() : super(_defaultSettings) {
18+
_initializeSettings();
19+
}
20+
21+
static const _defaultSettings = ThemeSettings(
22+
backgroundImage: 'assets/themeImages/aiBackground.jpeg',
23+
primaryColor: AppTheme.primaryColor,
24+
secondaryColor: Colors.teal,
25+
systemBubbleColor: Color(0xFF444654),
26+
userBubbleColor: Color(0xFF343541),
27+
);
28+
29+
Future<void> _initializeSettings() async {
30+
try {
31+
final prefs = await SharedPreferences.getInstance();
32+
final settingsJson = prefs.getString(_storageKey);
33+
34+
if (settingsJson != null) {
35+
final settings = ThemeSettings.fromJson(json.decode(settingsJson));
36+
state = settings;
37+
} else {
38+
// Save default settings if none exist
39+
await updateSettings(_defaultSettings);
40+
}
41+
} catch (e) {
42+
// Fallback to default settings on error
43+
state = _defaultSettings;
44+
}
45+
}
46+
47+
Future<void> updateSettings(ThemeSettings settings) async {
48+
try {
49+
final prefs = await SharedPreferences.getInstance();
50+
await prefs.setString(_storageKey, json.encode(settings.toJson()));
51+
state = settings;
52+
} catch (e) {
53+
// Handle error (could show an error message)
54+
print('Error saving theme settings: $e');
55+
}
56+
}
57+
58+
// Helper method to update individual properties
59+
Future<void> updateThemeColors({
60+
Color? primaryColor,
61+
Color? secondaryColor,
62+
Color? systemBubbleColor,
63+
Color? userBubbleColor,
64+
}) async {
65+
final newSettings = state.copyWith(
66+
primaryColor: primaryColor,
67+
secondaryColor: secondaryColor,
68+
systemBubbleColor: systemBubbleColor,
69+
userBubbleColor: userBubbleColor,
70+
);
71+
await updateSettings(newSettings);
72+
}
73+
74+
Future<void> updateBackgroundImage(String imagePath) async {
75+
final newSettings = state.copyWith(backgroundImage: imagePath);
76+
await updateSettings(newSettings);
77+
}
78+
}

lib/data/view/profile/profile_screen.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import '../../../configs/theme/app_theme.dart';
88
import '../../../configs/routes/routes_name.dart';
99
import '../../services/shared_prefs_service.dart';
1010
import '../../providers/chat_provider.dart';
11+
import '../../../data/view/settings/theme_settings_screen.dart';
1112

1213
class ProfileScreen extends ConsumerStatefulWidget {
1314
const ProfileScreen({super.key});
@@ -248,6 +249,16 @@ class _ProfileScreenState extends ConsumerState<ProfileScreen> {
248249
);
249250
},
250251
),
252+
_buildSettingsTile(
253+
icon: Icons.color_lens,
254+
title: 'Theme Settings',
255+
onTap: () {
256+
Navigator.push(
257+
context,
258+
MaterialPageRoute(builder: (context) => const ThemeSettingsScreen()),
259+
);
260+
},
261+
),
251262
_buildSettingsTile(
252263
icon: Icons.logout,
253264
title: 'Logout',

0 commit comments

Comments
 (0)