import 'package:flutter/material.dart'; import '../models/camera_setup.dart'; import '../services/app_settings.dart'; import '../theme/app_theme.dart'; import '../widgets/section.dart'; import '../widgets/text_prompt.dart'; /// Locations (strelišča), number of positions, and the camera IP of every /// position at every location. class CameraSettingsScreen extends StatelessWidget { const CameraSettingsScreen({super.key}); @override Widget build(BuildContext context) { return ListenableBuilder( listenable: AppSettings.instance, builder: (context, _) { final s = AppSettings.instance; final t = s.t; final setup = s.cameras; final surf = Surfaces.current; const firstPort = CameraSetup.firstPort; return Scaffold( backgroundColor: s.backgroundColor, appBar: AppBar( title: Text(t('Cameras'), style: const TextStyle(color: Colors.white)), backgroundColor: AppTheme.bar, iconTheme: const IconThemeData(color: Colors.white), scrolledUnderElevation: 0, ), body: ListView( // Keep the last row clear of the system gesture bar (edge-to-edge) padding: EdgeInsets.fromLTRB(16, 4, 16, 24 + MediaQuery.paddingOf(context).bottom), children: [ SectionHeader(t('Locations')), GroupCard(children: [ for (final location in setup.locations) SettingsTile( leading: Icon( location == setup.clubLocation ? Icons.verified_rounded : Icons.place_rounded, color: location == setup.clubLocation ? Colors.green : s.bulletColor, ), title: location, subtitle: location == setup.clubLocation ? t('Added with the club code') : null, value: t('{n} cameras').replaceAll('{n}', '${setup.camerasIn(location)}'), onTap: () => _editLocation(context, location), ), SettingsTile( leading: Icon(Icons.add_rounded, color: surf.textSecondary), title: t('Add location'), onTap: () => _addLocation(context), ), ]), SectionHeader(t('Positions')), GroupCard(children: [ SettingsTile( title: t('Number of positions'), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ IconButton( icon: const Icon(Icons.remove_rounded), color: surf.textPrimary, onPressed: setup.positions > 1 ? () => s.setPositionCount(setup.positions - 1) : null, ), SizedBox( width: 28, child: Text( '${setup.positions}', textAlign: TextAlign.center, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: surf.textPrimary), ), ), IconButton( icon: const Icon(Icons.add_rounded), color: surf.textPrimary, onPressed: setup.positions < CameraSetup.maxPositions ? () => s.setPositionCount(setup.positions + 1) : null, ), ], ), ), ]), for (final location in setup.locations) ...[ SectionHeader(location), GroupCard(children: [ SettingsTile( leading: Icon(Icons.bolt_rounded, color: s.bulletColor), title: t('Set all cameras from one IP'), subtitle: t('Ports {from}–{to}') .replaceAll('{from}', '$firstPort') .replaceAll('{to}', '${firstPort + setup.positions - 1}'), onTap: () => _editHost(context, location), ), for (int p = 1; p <= setup.positions; p++) SettingsTile( title: '${t('Camera')} $p', value: setup.ipFor(p, location).isEmpty ? '—' : setup.ipFor(p, location), onTap: () => _editCamera(context, p, location), ), ]), ], ], ), ); }, ); } Future _addLocation(BuildContext context) async { final s = AppSettings.instance; final name = await showTextPrompt(context, title: s.t('Add location'), hint: s.t('e.g. Home')); if (name == null || name.isEmpty) return; if (!await s.addLocation(name) && context.mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(s.t('This location already exists')))); } } Future _editLocation(BuildContext context, String location) async { final s = AppSettings.instance; final name = await showTextPrompt( context, title: s.t('Location'), initial: location, resetLabel: s.t('Delete'), onReset: () => s.removeLocation(location), ); if (name != null && name.isNotEmpty && name != location) await s.renameLocation(location, name); } Future _editHost(BuildContext context, String location) async { final s = AppSettings.instance; final first = s.cameras.ipFor(1, location); final host = await showTextPrompt( context, title: '${s.t('Set all cameras from one IP')} · $location', initial: first.split(':').first, hint: '192.168.0.10', keyboardType: TextInputType.url, ); if (host != null && host.isNotEmpty) s.setLocationHost(location, host); } Future _editCamera(BuildContext context, int position, String location) async { final s = AppSettings.instance; final ip = await showTextPrompt( context, title: '${s.t('Camera')} $position · $location', initial: s.cameras.ipFor(position, location), hint: '192.168.0.10:${CameraSetup.firstPort + position - 1}', keyboardType: TextInputType.url, resetLabel: s.t('Remove'), onReset: () => s.setCameraIp(position, location, ''), ); if (ip != null && ip.isNotEmpty) s.setCameraIp(position, location, ip); } }