Files
SDKmuta/lib/screens/settings_screen.dart
T
2025-12-23 16:30:36 +01:00

1024 lines
33 KiB
Dart

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../utils/translations.dart';
import '../utils/target_colors.dart';
class SettingsScreen extends StatefulWidget {
const SettingsScreen({super.key});
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
Color _backgroundColor = Colors.grey[900]!;
List<String> _cameraIPs = List.generate(6, (index) => 'Not Set');
final List<String> _defaultIPs = [
'192.168.0.134:9081',
'192.168.0.134:9082',
'192.168.0.134:9083',
'192.168.0.134:9084',
'192.168.0.134:9085',
'192.168.0.134:9086',
];
String _selectedBulletColor = 'Blue';
final List<String> _bulletColors = [
'Blue',
'Green',
'Yellow',
'Purple',
'Cyan',
'Teal',
'Indigo',
'Pink',
'Lime',
'Mint',
'Orange',
'Violet',
'Amber',
'Brown',
'Grey',
];
String _selectedLanguage = 'English';
final List<Map<String, String>> _languages = [
{'code': 'en', 'name': 'English', 'flag': '🇬🇧'},
{'code': 'sl', 'name': 'Slovenian', 'flag': '🇸🇮'},
];
String _selectedTarget = 'assets/target.png';
final List<String> _availableTargets = [
'assets/target.png',
'assets/targets/Target1.jpg',
'assets/targets/Target2.jpg',
'assets/targets/Target3.jpg',
'assets/targets/Target4.jpg',
'assets/targets/Target5.jpg',
'assets/targets/Target6.jpg',
'assets/targets/Target7.jpg',
'assets/targets/Target8.jpg',
'assets/targets/Target9.jpg',
'assets/targets/Target10.jpg',
'assets/targets/Target11.jpg',
'assets/targets/Target12.jpg',
'assets/targets/Target13.jpg',
'assets/targets/Target14.jpg',
'assets/targets/Target15.jpg',
'assets/targets/Target16.jpg',
'assets/targets/Target17.jpg',
'assets/targets/Target18.jpg',
'assets/targets/Target19.jpg',
'assets/targets/Target20.jpg',
'assets/targets/Target21.jpg',
'assets/targets/Target22.jpg',
'assets/targets/Target23.jpg',
'assets/targets/Target24.jpg',
];
@override
void initState() {
super.initState();
_loadCameraIPs();
_loadBulletColor();
_loadLanguage();
_loadSelectedTarget();
}
String _t(String key) {
return AppTranslations.translate(key, _selectedLanguage);
}
Color _getColorFromName(String colorName) {
switch (colorName.toLowerCase()) {
case 'blue':
return Colors.blue;
case 'green':
return Colors.green;
case 'yellow':
return Colors.yellow;
case 'purple':
return Colors.purple;
case 'cyan':
return Colors.cyan;
case 'teal':
return Colors.teal;
case 'indigo':
return Colors.indigo;
case 'pink':
return Colors.pink;
case 'lime':
return Colors.lime;
case 'mint':
return Colors.greenAccent;
case 'orange':
return Colors.orange;
case 'violet':
return Colors.deepPurpleAccent;
case 'amber':
return Colors.amber;
case 'brown':
return Colors.brown;
case 'grey':
return Colors.grey;
default:
return Colors.blue; // fallback
}
}
Future<void> _loadBulletColor() async {
final prefs = await SharedPreferences.getInstance();
String savedColor = prefs.getString('bulletColor') ?? 'Blue';
// Fallback in case old color is no longer valid
if (!_bulletColors.contains(savedColor)) {
savedColor = 'Blue';
await prefs.setString('bulletColor', savedColor);
}
setState(() {
_selectedBulletColor = savedColor;
});
}
Future<void> _saveBulletColor(String color) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('bulletColor', color);
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> _loadSelectedTarget() async {
final prefs = await SharedPreferences.getInstance();
String savedTarget = prefs.getString('selectedTarget') ?? 'assets/target.png';
setState(() {
_selectedTarget = savedTarget;
_backgroundColor = TargetColors.getBackgroundColor(savedTarget);
});
}
Future<void> _saveSelectedTarget(String target) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('selectedTarget', target);
setState(() {
_selectedTarget = target;
_backgroundColor = TargetColors.getBackgroundColor(target);
});
}
String _getTargetDisplayName(String targetPath) {
if (targetPath == 'assets/target.png') {
return _t('Default Target');
}
final targetName = targetPath
.replaceAll('assets/targets/', '')
.replaceAll('.jpg', '');
return _t(targetName);
}
Future<void> _loadCameraIPs() async {
final prefs = await SharedPreferences.getInstance();
List<String> tempIPs = [];
for (int i = 0; i < 6; i++) {
String key = 'cameraIP_${i + 1}';
String? savedIP = prefs.getString(key);
// If not saved, use default and write it to prefs
if (savedIP == null) {
await prefs.setString(key, _defaultIPs[i]);
tempIPs.add(_defaultIPs[i]);
} else {
tempIPs.add(savedIP);
}
}
setState(() {
_cameraIPs = tempIPs;
});
}
Future<void> _saveCameraIP(int index, String ip) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('cameraIP_${index + 1}', ip);
_loadCameraIPs();
}
Future<void> _clearCameraIP(int index) async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove('cameraIP_${index + 1}');
_loadCameraIPs();
}
void _editCameraIP(int index) {
TextEditingController controller = TextEditingController(
text: _cameraIPs[index],
);
showDialog(
context: context,
builder: (context) {
return RepaintBoundary(
child: Dialog(
backgroundColor: Colors.grey[900],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Padding(
padding: const EdgeInsets.all(24.0),
child: StatefulBuilder(
builder: (context, setState) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"${_t('Camera')} ${index + 1}",
style: const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: TextField(
controller: controller,
style: const TextStyle(color: Colors.white),
textAlign: TextAlign.center,
decoration: InputDecoration(
filled: true,
fillColor: Colors.grey[800],
hintText: _t("Enter Camera IP"),
hintStyle: const TextStyle(color: Colors.white54),
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide.none,
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(color: _getColorFromName(_selectedBulletColor)),
),
),
),
),
const SizedBox(height: 24),
// Save Button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () async {
String newIP = controller.text.trim();
if (newIP.isNotEmpty) {
await _saveCameraIP(index, newIP);
if (context.mounted) {
Navigator.pop(context);
}
}
},
style: ElevatedButton.styleFrom(
backgroundColor: _getColorFromName(_selectedBulletColor),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text(_t("Save")),
),
),
const SizedBox(height: 12),
// Reset Button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () async {
await _clearCameraIP(index);
if (context.mounted) {
Navigator.pop(context);
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[700],
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text(_t("Reset to Default")),
),
),
const SizedBox(height: 12),
// Cancel Button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () => Navigator.pop(context),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[800],
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text(_t("Cancel")),
),
),
],
);
},
),
),
),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: _backgroundColor,
body: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: Column(
children: [
const SizedBox(height: 8),
_buildLanguageSelector(),
const SizedBox(height: 16),
_buildBulletColorSelector(),
const SizedBox(height: 16),
_buildTargetSelector(),
const SizedBox(height: 16),
_buildCameraList(),
],
),
),
);
}
Widget _buildLanguageSelector() {
final selectedLang = _languages.firstWhere(
(lang) => lang['name'] == _selectedLanguage,
orElse: () => _languages[0],
);
return Container(
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 0),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.grey.withValues(alpha: 0.3),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Header
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: Colors.grey[900],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Row(
children: [
Container(
width: 4,
height: 16,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(2),
),
),
const SizedBox(width: 12),
Text(
_t("Language"),
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
),
// Content
GestureDetector(
onTap: _showLanguagePopup,
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(12),
bottomRight: Radius.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 Container(
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 0),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.grey.withValues(alpha: 0.3),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Header
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: Colors.grey[900],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Row(
children: [
Container(
width: 4,
height: 16,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(2),
),
),
const SizedBox(width: 12),
Text(
_t("Select Bullet Color"),
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
),
// Content
GestureDetector(
onTap: _showBulletColorPopup,
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(12),
bottomRight: Radius.circular(12),
),
),
child: Row(
children: [
Container(
width: 16,
height: 16,
margin: const EdgeInsets.only(right: 12),
decoration: BoxDecoration(
color: _getColorFromName(_selectedBulletColor),
shape: BoxShape.circle,
border: Border.all(color: Colors.black26),
),
),
Expanded(
child: Text(
_t(_selectedBulletColor),
style: const TextStyle(color: Colors.black87, fontSize: 14),
),
),
const Icon(Icons.chevron_right, color: Colors.black54),
],
),
),
),
],
),
);
}
Widget _buildTargetSelector() {
return Container(
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 0),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.grey.withValues(alpha: 0.3),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Header
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: Colors.grey[900],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Row(
children: [
Container(
width: 4,
height: 16,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(2),
),
),
const SizedBox(width: 12),
Text(
_t("Select Target"),
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
),
// Content
GestureDetector(
onTap: _showTargetPopup,
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(12),
bottomRight: Radius.circular(12),
),
),
child: Row(
children: [
Expanded(
child: Text(
_getTargetDisplayName(_selectedTarget),
style: const TextStyle(color: Colors.black87, fontSize: 14),
),
),
const Icon(Icons.chevron_right, color: Colors.black54),
],
),
),
),
],
),
);
}
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,
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 Bullet Color"),
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 20),
Wrap(
spacing: 12,
runSpacing: 12,
children: _bulletColors.map((colorName) {
final color = _getColorFromName(colorName);
final isSelected = colorName == _selectedBulletColor;
return GestureDetector(
onTap: () async {
await _saveBulletColor(colorName);
if (context.mounted) {
Navigator.of(context).pop();
}
},
child: Container(
width: 48,
height: 48,
decoration: BoxDecoration(
color: color,
shape: BoxShape.circle,
border: Border.all(
color: isSelected ? Colors.white : Colors.white24,
width: isSelected ? 3 : 1,
),
),
),
);
}).toList(),
),
const SizedBox(height: 20),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(_t("Cancel"), style: const TextStyle(color: Colors.white70)),
),
],
),
),
);
},
);
}
void _showTargetPopup() {
String previewTarget = _selectedTarget;
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, innerSetState) {
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 Target"),
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 20),
// Image Preview
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
border: Border.all(color: Colors.white24),
borderRadius: BorderRadius.circular(8),
),
child: Image.asset(
previewTarget,
fit: BoxFit.contain,
errorBuilder: (context, error, stackTrace) {
return Container(
color: Colors.grey[800],
child: const Center(
child: Icon(Icons.image_not_supported, color: Colors.white54),
),
);
},
),
),
const SizedBox(height: 20),
// Target List
SizedBox(
width: double.maxFinite,
height: 300,
child: ListView(
children: _availableTargets.map((targetPath) {
final isSelected = targetPath == previewTarget;
final displayName = _getTargetDisplayName(targetPath);
return Padding(
padding: const EdgeInsets.only(bottom: 12.0),
child: SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
innerSetState(() => previewTarget = targetPath);
},
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: Text(displayName),
),
),
);
}).toList(),
),
),
const SizedBox(height: 12),
// Action Buttons
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: () async {
await _saveSelectedTarget(previewTarget);
if (context.mounted) {
Navigator.of(context).pop();
}
},
style: ElevatedButton.styleFrom(
backgroundColor: _getColorFromName(_selectedBulletColor),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text(_t("Apply")),
),
),
const SizedBox(width: 12),
Expanded(
child: TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(_t("Cancel"), style: const TextStyle(color: Colors.white70)),
),
),
],
),
],
),
),
);
},
);
},
);
}
Widget _buildCameraList() {
return Container(
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 0),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.grey.withValues(alpha: 0.3),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Header
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: Colors.grey[900],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Row(
children: [
Container(
width: 4,
height: 16,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(2),
),
),
const SizedBox(width: 12),
Text(
_t("Camera Settings"),
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
),
// Content - all cameras
...List.generate(6, (index) {
return GestureDetector(
onTap: () => _editCameraIP(index),
child: Column(
children: [
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: index == 5
? const BorderRadius.only(
bottomLeft: Radius.circular(12),
bottomRight: Radius.circular(12),
)
: null,
border: index < 5
? Border(
bottom: BorderSide(
color: Colors.grey[400]!,
width: 1,
),
)
: null,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"${_t('Camera')} ${index + 1}",
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
Text(
_cameraIPs[index],
style: const TextStyle(color: Colors.black54),
),
],
),
const Icon(Icons.chevron_right, color: Colors.black54),
],
),
),
],
),
);
}),
],
),
);
}
}