new UI + new targets.
This commit is contained in:
+177
-355
@@ -1,34 +1,34 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_mjpeg/flutter_mjpeg.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../utils/translations.dart';
|
||||
import '../services/app_settings.dart';
|
||||
import '../services/wifi_binding.dart';
|
||||
import '../theme/app_theme.dart';
|
||||
import '../widgets/ui.dart';
|
||||
import 'stream_screen.dart';
|
||||
|
||||
/// Opazuj: every chosen camera of the current location live, two per row.
|
||||
class SpectatorScreen extends StatefulWidget {
|
||||
final String selectedLanguage;
|
||||
|
||||
const SpectatorScreen({
|
||||
super.key,
|
||||
required this.selectedLanguage,
|
||||
});
|
||||
const SpectatorScreen({super.key, required this.selectedLanguage});
|
||||
|
||||
@override
|
||||
State<SpectatorScreen> createState() => _SpectatorScreenState();
|
||||
}
|
||||
|
||||
class _SpectatorScreenState extends State<SpectatorScreen> {
|
||||
final List<bool> _selectedCameras = List.generate(6, (_) => true); // All selected by default
|
||||
List<bool> _enabledCameras = List.generate(6, (_) => false); // Which cameras have IPs configured
|
||||
List<Map<String, dynamic>> _cameras = [];
|
||||
Color _bulletColor = Colors.blue; // default/fallback bullet color
|
||||
final Set<int> _hidden = {}; // positions switched off in "Izberi kamere"
|
||||
late final Future<void> _wifiReady;
|
||||
bool _ready = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_wifiReady = WifiBinding.acquire();
|
||||
_loadCameras();
|
||||
_loadBulletColor();
|
||||
// Streams must start after traffic is pinned to Wi-Fi
|
||||
_wifiReady.then((_) {
|
||||
if (mounted) setState(() => _ready = true);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -37,376 +37,198 @@ class _SpectatorScreenState extends State<SpectatorScreen> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String _t(String key) {
|
||||
return AppTranslations.translate(key, widget.selectedLanguage);
|
||||
String _t(String key) => AppSettings.instance.t(key);
|
||||
|
||||
String? get _location {
|
||||
final cams = AppSettings.instance.cameras;
|
||||
if (cams.locations.isEmpty) return null;
|
||||
return cams.locations.contains(cams.activeLocation) ? cams.activeLocation : cams.locations.first;
|
||||
}
|
||||
|
||||
Future<void> _loadCameras() async {
|
||||
await _wifiReady; // streams must start after traffic is pinned to Wi-Fi
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
List<Map<String, dynamic>> cameras = [];
|
||||
List<bool> enabledCameras = List.generate(6, (_) => false);
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
String? ip = prefs.getString('cameraIP_${i + 1}');
|
||||
if (ip != null && ip.isNotEmpty && ip != 'Not Set') {
|
||||
cameras.add({
|
||||
'index': i + 1,
|
||||
'ip': ip,
|
||||
});
|
||||
enabledCameras[i] = true; // Mark camera as enabled
|
||||
}
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_cameras = cameras;
|
||||
_enabledCameras = enabledCameras;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadBulletColor() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final selected = prefs.getString('bulletColor') ?? 'Blue';
|
||||
setState(() {
|
||||
_bulletColor = _getColorFromName(selected);
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
// Positions with a camera at the location, with their address
|
||||
List<(int, String)> get _cameras {
|
||||
final s = AppSettings.instance;
|
||||
final location = _location;
|
||||
if (location == null) return const [];
|
||||
return [
|
||||
for (int p = 1; p <= s.cameras.positions; p++)
|
||||
if (s.cameras.ipFor(p, location).isNotEmpty) (p, s.cameras.ipFor(p, location)),
|
||||
];
|
||||
}
|
||||
|
||||
void _showCameraSelection() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => StatefulBuilder(
|
||||
builder: (context, setModalState) => Dialog(
|
||||
backgroundColor: Colors.grey[900],
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.fromLTRB(24, 20, 24, 20),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
_t('Select Cameras'),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
// Row 1: Cameras 1-3
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: List.generate(3, (index) {
|
||||
final cameraNum = index + 1;
|
||||
final isSelected = _selectedCameras[index];
|
||||
final isEnabled = _enabledCameras[index];
|
||||
final c = Palette.current;
|
||||
final s = AppSettings.instance;
|
||||
showAppSheet(
|
||||
context,
|
||||
title: _t('Select Cameras'),
|
||||
subtitle: _location,
|
||||
builder: (sheet) => StatefulBuilder(
|
||||
builder: (context, setSheet) => Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
for (int p = 1; p <= s.cameras.positions; p++)
|
||||
Builder(builder: (_) {
|
||||
final ip = _location == null ? '' : s.cameras.ipFor(p, _location!);
|
||||
final enabled = ip.isNotEmpty;
|
||||
final on = enabled && !_hidden.contains(p);
|
||||
void toggle() {
|
||||
if (!enabled) return;
|
||||
setState(() => on ? _hidden.add(p) : _hidden.remove(p));
|
||||
setSheet(() {});
|
||||
}
|
||||
|
||||
return GestureDetector(
|
||||
onTap: isEnabled
|
||||
? () {
|
||||
// Update both modal and parent state simultaneously
|
||||
_selectedCameras[index] = !_selectedCameras[index];
|
||||
setModalState(() {});
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
: null,
|
||||
child: Container(
|
||||
width: 50,
|
||||
height: 50,
|
||||
decoration: BoxDecoration(
|
||||
color: !isEnabled
|
||||
? Colors.grey[700]
|
||||
: (isSelected ? _bulletColor : Colors.grey[800]),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: !isEnabled
|
||||
? Colors.grey[600]!
|
||||
: (isSelected ? _bulletColor : Colors.grey[700]!),
|
||||
width: 2,
|
||||
),
|
||||
return InkWell(
|
||||
onTap: toggle,
|
||||
child: Container(
|
||||
height: 52,
|
||||
decoration: BoxDecoration(border: p > 1 ? Border(top: BorderSide(color: c.line2)) : null),
|
||||
child: Row(
|
||||
children: [
|
||||
SizedBox.square(
|
||||
dimension: 24,
|
||||
child: Checkbox(value: on, onChanged: enabled ? (_) => toggle() : null),
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'$cameraNum',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 14),
|
||||
Expanded(
|
||||
child: Text('${_t('Camera')} $p',
|
||||
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500, color: enabled ? c.ink : c.faint)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
// Row 2: Cameras 4-6
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: List.generate(3, (index) {
|
||||
final cameraNum = index + 4;
|
||||
final arrayIndex = index + 3;
|
||||
final isSelected = _selectedCameras[arrayIndex];
|
||||
final isEnabled = _enabledCameras[arrayIndex];
|
||||
|
||||
return GestureDetector(
|
||||
onTap: isEnabled
|
||||
? () {
|
||||
// Update both modal and parent state simultaneously
|
||||
_selectedCameras[arrayIndex] = !_selectedCameras[arrayIndex];
|
||||
setModalState(() {});
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
: null,
|
||||
child: Container(
|
||||
width: 50,
|
||||
height: 50,
|
||||
decoration: BoxDecoration(
|
||||
color: !isEnabled
|
||||
? Colors.grey[700]
|
||||
: (isSelected ? _bulletColor : Colors.grey[800]),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: !isEnabled
|
||||
? Colors.grey[600]!
|
||||
: (isSelected ? _bulletColor : Colors.grey[700]!),
|
||||
width: 2,
|
||||
),
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'$cameraNum',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Container(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
top: BorderSide(color: Colors.grey[700]!, width: 1),
|
||||
Text(enabled ? ip.split(':').first : _t('Not set up'),
|
||||
style: TextStyle(
|
||||
fontSize: 12, fontFamily: enabled ? AppTheme.mono : null, color: enabled ? c.muted : c.faint)),
|
||||
],
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const SizedBox(height: 16),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 40,
|
||||
child: ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: _bulletColor,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: Text(
|
||||
_t('Done'),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
const SizedBox(height: 16),
|
||||
PrimaryButton(label: _t('Done'), onTap: () => Navigator.pop(sheet)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _cameraCount(int n) {
|
||||
if (AppSettings.instance.language == 'Slovenian') {
|
||||
final mod = n % 100;
|
||||
return '$n ${mod == 1 ? 'kamera' : mod == 2 ? 'kameri' : (mod == 3 || mod == 4) ? 'kamere' : 'kamer'}';
|
||||
}
|
||||
return _t('{n} cameras').replaceAll('{n}', '$n');
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final visibleCameras = <Map<String, dynamic>>[];
|
||||
for (int i = 0; i < _cameras.length; i++) {
|
||||
if (_selectedCameras[_cameras[i]['index'] - 1]) {
|
||||
visibleCameras.add(_cameras[i]);
|
||||
}
|
||||
}
|
||||
final c = Palette.current;
|
||||
final visible = [for (final cam in _cameras) if (!_hidden.contains(cam.$1)) cam];
|
||||
final location = _location;
|
||||
|
||||
// Portrait list view
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
_t('Spectate'),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.w600,
|
||||
body: Column(
|
||||
children: [
|
||||
BackHeader(
|
||||
title: _t('Spectate'),
|
||||
subtitle: [_cameraCount(visible.length), if (location != null) location].join(' · '),
|
||||
actions: [
|
||||
Material(
|
||||
color: c.tile,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: InkWell(
|
||||
onTap: _showCameraSelection,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.grid_view_rounded, size: 17, color: c.ink),
|
||||
const SizedBox(width: 6),
|
||||
Text(_t('Choose'), style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: c.ink)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
backgroundColor: Colors.grey[900],
|
||||
elevation: 0,
|
||||
scrolledUnderElevation: 0,
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back, color: Colors.white),
|
||||
onPressed: () => Navigator.pop(context),
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.settings, color: Colors.white),
|
||||
onPressed: _showCameraSelection,
|
||||
Expanded(
|
||||
child: !_ready
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: LayoutBuilder(builder: (context, box) {
|
||||
final w = (box.maxWidth - 2 * AppTheme.edge - 12) / 2;
|
||||
return SingleChildScrollView(
|
||||
padding: EdgeInsets.fromLTRB(AppTheme.edge, 4, AppTheme.edge, 24 + MediaQuery.paddingOf(context).bottom),
|
||||
child: Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 12,
|
||||
children: [
|
||||
for (final (p, ip) in visible) SizedBox(width: w, child: _cameraCard(c, p, ip, location!)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: visibleCameras.isEmpty
|
||||
? Center(
|
||||
child: Text(
|
||||
_t('No cameras selected'),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
)
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.fromLTRB(4, 4, 4, 16),
|
||||
itemCount: visibleCameras.length,
|
||||
itemBuilder: (context, index) {
|
||||
final camera = visibleCameras[index];
|
||||
final cameraIndex = camera['index'] as int;
|
||||
final cameraIp = camera['ip'] as String;
|
||||
);
|
||||
}
|
||||
|
||||
final streamUrl = cameraIp.startsWith(RegExp(r"https?://"))
|
||||
? cameraIp
|
||||
: "http://$cameraIp";
|
||||
|
||||
// 4:3 aspect ratio = width / height (640x480 stream)
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: Colors.grey[800]!, width: 1),
|
||||
),
|
||||
child: AspectRatio(
|
||||
aspectRatio: 4 / 3,
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Stack(
|
||||
children: [
|
||||
Mjpeg(
|
||||
stream: streamUrl,
|
||||
isLive: true,
|
||||
fit: BoxFit.cover,
|
||||
error: (context, error, stackTrace) => Container(
|
||||
color: Colors.grey[700],
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.videocam_off,
|
||||
color: Colors.white,
|
||||
size: 32,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
"${_t('Camera')} $cameraIndex",
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
// Camera number circle - top right (with bullet color)
|
||||
Positioned(
|
||||
top: 8,
|
||||
right: 8,
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: _bulletColor,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'$cameraIndex',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
Widget _cameraCard(Palette c, int position, String ip, String location) {
|
||||
final url = ip.startsWith(RegExp(r'https?://')) ? ip : 'http://$ip';
|
||||
void open() => Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => CameraStreamScreen(cameraIndex: position, ip: ip, location: location)),
|
||||
);
|
||||
return AppCard(
|
||||
padding: EdgeInsets.zero,
|
||||
onTap: open,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
AspectRatio(
|
||||
aspectRatio: 0.78,
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
ColoredBox(
|
||||
color: Colors.black,
|
||||
child: Mjpeg(
|
||||
stream: url,
|
||||
isLive: true,
|
||||
fit: BoxFit.cover,
|
||||
error: (context, error, stackTrace) => ColoredBox(
|
||||
color: c.tile,
|
||||
child: Icon(Icons.videocam_off_outlined, color: c.muted, size: 30),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
Positioned(
|
||||
top: 10,
|
||||
left: 10,
|
||||
child: StatusChip(label: _t('Live'), dot: true, color: c.red, background: Colors.black.withValues(alpha: 0.55)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 10, 10, 12),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('${_t('Camera')} $position', style: TextStyle(fontSize: 14.5, fontWeight: FontWeight.w700, color: c.ink)),
|
||||
Text(location, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 11.5, color: c.muted)),
|
||||
],
|
||||
),
|
||||
),
|
||||
Icon(Icons.open_in_new_rounded, size: 17, color: c.muted),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user