This commit is contained in:
2025-10-16 17:42:46 +02:00
parent dc86955721
commit 2e3a69a23e
12 changed files with 1421 additions and 604 deletions
+179 -21
View File
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../utils/translations.dart';
class SettingsScreen extends StatefulWidget {
const SettingsScreen({super.key});
@@ -40,16 +41,26 @@ class _SettingsScreenState extends State<SettingsScreen> {
'Grey',
];
String _selectedLanguage = 'English';
final List<Map<String, String>> _languages = [
{'code': 'en', 'name': 'English', 'flag': '🇬🇧'},
{'code': 'sl', 'name': 'Slovenian', 'flag': '🇸🇮'},
];
@override
void initState() {
super.initState();
_loadCameraIPs();
_loadBulletColor();
_loadBulletColor();
_loadLanguage();
}
String _t(String key) {
return AppTranslations.translate(key, _selectedLanguage);
}
Color _getColorFromName(String colorName) {
switch (colorName.toLowerCase()) {
case 'blue':
@@ -108,6 +119,21 @@ Future<void> _saveBulletColor(String color) async {
setState(() => _selectedBulletColor = color);
}
Future<void> _loadLanguage() async {
final prefs = await SharedPreferences.getInstance();
String savedLanguage = prefs.getString('language') ?? 'English';
setState(() {
_selectedLanguage = savedLanguage;
});
}
Future<void> _saveLanguage(String language) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('language', language);
setState(() => _selectedLanguage = language);
}
Future<void> _loadCameraIPs() async {
final prefs = await SharedPreferences.getInstance();
@@ -164,7 +190,7 @@ Future<void> _saveBulletColor(String color) async {
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"Camera ${index + 1}",
"${_t('Camera')} ${index + 1}",
style: const TextStyle(
color: Colors.white,
fontSize: 20,
@@ -183,7 +209,7 @@ Future<void> _saveBulletColor(String color) async {
decoration: InputDecoration(
filled: true,
fillColor: Colors.grey[800],
hintText: "Enter Camera IP",
hintText: _t("Enter Camera IP"),
hintStyle: const TextStyle(color: Colors.white54),
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
enabledBorder: OutlineInputBorder(
@@ -207,7 +233,9 @@ Future<void> _saveBulletColor(String color) async {
String newIP = controller.text.trim();
if (newIP.isNotEmpty) {
await _saveCameraIP(index, newIP);
Navigator.pop(context);
if (context.mounted) {
Navigator.pop(context);
}
}
},
style: ElevatedButton.styleFrom(
@@ -218,7 +246,7 @@ Future<void> _saveBulletColor(String color) async {
borderRadius: BorderRadius.circular(12),
),
),
child: const Text("Save"),
child: Text(_t("Save")),
),
),
const SizedBox(height: 12),
@@ -229,7 +257,9 @@ Future<void> _saveBulletColor(String color) async {
child: ElevatedButton(
onPressed: () async {
await _clearCameraIP(index);
Navigator.pop(context);
if (context.mounted) {
Navigator.pop(context);
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[700],
@@ -239,7 +269,7 @@ Future<void> _saveBulletColor(String color) async {
borderRadius: BorderRadius.circular(12),
),
),
child: const Text("Reset to Default"),
child: Text(_t("Reset to Default")),
),
),
const SizedBox(height: 12),
@@ -257,7 +287,7 @@ Future<void> _saveBulletColor(String color) async {
borderRadius: BorderRadius.circular(12),
),
),
child: const Text("Cancel"),
child: Text(_t("Cancel")),
),
),
],
@@ -280,6 +310,8 @@ Future<void> _saveBulletColor(String color) async {
child: Column(
children: [
const SizedBox(height: 8),
_buildLanguageSelector(),
const SizedBox(height: 16),
_buildBulletColorSelector(),
const SizedBox(height: 16),
@@ -290,13 +322,61 @@ Future<void> _saveBulletColor(String color) async {
);
}
Widget _buildLanguageSelector() {
final selectedLang = _languages.firstWhere(
(lang) => lang['name'] == _selectedLanguage,
orElse: () => _languages[0],
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_t("Language"),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
const SizedBox(height: 8),
GestureDetector(
onTap: _showLanguagePopup,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
Text(
selectedLang['flag'] ?? '🌐',
style: const TextStyle(fontSize: 20),
),
const SizedBox(width: 12),
Expanded(
child: Text(
_t(selectedLang['name'] ?? 'English'),
style: const TextStyle(color: Colors.black87, fontSize: 14),
),
),
const Icon(Icons.chevron_right, color: Colors.black54),
],
),
),
),
],
);
}
Widget _buildBulletColorSelector() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Select Bullet Color",
style: TextStyle(
Text(
_t("Select Bullet Color"),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.black87,
@@ -325,7 +405,7 @@ Future<void> _saveBulletColor(String color) async {
),
Expanded(
child: Text(
_selectedBulletColor,
_t(_selectedBulletColor),
style: const TextStyle(color: Colors.black87, fontSize: 14),
),
),
@@ -338,6 +418,82 @@ Future<void> _saveBulletColor(String color) async {
);
}
void _showLanguagePopup() {
showDialog(
context: context,
builder: (context) {
return Dialog(
backgroundColor: Colors.grey[900],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
_t("Select Language"),
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 20),
..._languages.map((lang) {
final isSelected = lang['name'] == _selectedLanguage;
return Padding(
padding: const EdgeInsets.only(bottom: 12.0),
child: SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () async {
await _saveLanguage(lang['name']!);
if (context.mounted) {
Navigator.of(context).pop();
}
},
style: ElevatedButton.styleFrom(
backgroundColor: isSelected
? _getColorFromName(_selectedBulletColor)
: Colors.grey[800],
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
lang['flag']!,
style: const TextStyle(fontSize: 20),
),
const SizedBox(width: 12),
Text(
_t(lang['name']!),
style: const TextStyle(fontSize: 16),
),
],
),
),
),
);
}),
const SizedBox(height: 8),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(_t("Cancel"), style: const TextStyle(color: Colors.white70)),
),
],
),
),
);
},
);
}
void _showBulletColorPopup() {
showDialog(
context: context,
@@ -350,9 +506,9 @@ void _showBulletColorPopup() {
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
"Select Bullet Color",
style: TextStyle(
Text(
_t("Select Bullet Color"),
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
@@ -369,7 +525,9 @@ void _showBulletColorPopup() {
return GestureDetector(
onTap: () async {
await _saveBulletColor(colorName);
Navigator.of(context).pop();
if (context.mounted) {
Navigator.of(context).pop();
}
},
child: Container(
width: 48,
@@ -389,7 +547,7 @@ void _showBulletColorPopup() {
const SizedBox(height: 20),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text("Cancel", style: TextStyle(color: Colors.white70)),
child: Text(_t("Cancel"), style: const TextStyle(color: Colors.white70)),
),
],
),
@@ -403,9 +561,9 @@ void _showBulletColorPopup() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Camera Settings",
style: TextStyle(
Text(
_t("Camera Settings"),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.black87,
@@ -429,7 +587,7 @@ void _showBulletColorPopup() {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Camera ${index + 1}",
"${_t('Camera')} ${index + 1}",
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black87,