app mobile allarmi prima
This commit is contained in:
573
lib/screens/allarme_detail_screen.dart
Normal file
573
lib/screens/allarme_detail_screen.dart
Normal file
@@ -0,0 +1,573 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'dart:convert';
|
||||
import '../models/allarme.dart';
|
||||
import '../models/sito.dart';
|
||||
import '../services/api_service.dart';
|
||||
import '../utils/constants.dart';
|
||||
|
||||
class AllarmeDetailScreen extends StatefulWidget {
|
||||
final Allarme allarme;
|
||||
|
||||
const AllarmeDetailScreen({super.key, required this.allarme});
|
||||
|
||||
@override
|
||||
State<AllarmeDetailScreen> createState() => _AllarmeDetailScreenState();
|
||||
}
|
||||
|
||||
class _AllarmeDetailScreenState extends State<AllarmeDetailScreen> {
|
||||
final _apiService = ApiService();
|
||||
late Allarme _allarme;
|
||||
bool _isUpdating = false;
|
||||
bool _isLoadingSito = true;
|
||||
Sito? _sito;
|
||||
GoogleMapController? _mapController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_allarme = widget.allarme;
|
||||
_loadSito();
|
||||
}
|
||||
|
||||
Future<void> _loadSito() async {
|
||||
try {
|
||||
final sito = await _apiService.getSito(_allarme.sitoId);
|
||||
setState(() {
|
||||
_sito = sito;
|
||||
_isLoadingSito = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() => _isLoadingSito = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_mapController?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Color _getSeverityColor() {
|
||||
switch (_allarme.severita) {
|
||||
case 'critical':
|
||||
return AppColors.critical;
|
||||
case 'warning':
|
||||
return AppColors.warning;
|
||||
case 'info':
|
||||
return AppColors.info;
|
||||
default:
|
||||
return AppColors.textSecondary;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _updateStato(String nuovoStato) async {
|
||||
setState(() => _isUpdating = true);
|
||||
|
||||
try {
|
||||
final updatedAllarme = await _apiService.updateAllarme(
|
||||
_allarme.id,
|
||||
stato: nuovoStato,
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_allarme = updatedAllarme;
|
||||
_isUpdating = false;
|
||||
});
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Stato aggiornato a: ${_allarme.statoReadable}'),
|
||||
backgroundColor: AppColors.success,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
setState(() => _isUpdating = false);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Errore aggiornamento: $e'),
|
||||
backgroundColor: AppColors.critical,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _showStatoDialog() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Cambia Stato'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildStatoOption('nuovo', 'Nuovo', Icons.fiber_new),
|
||||
_buildStatoOption('in_gestione', 'In Gestione', Icons.engineering),
|
||||
_buildStatoOption('risolto', 'Risolto', Icons.check_circle),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatoOption(String stato, String label, IconData icon) {
|
||||
final isCurrentStato = _allarme.stato == stato;
|
||||
|
||||
return ListTile(
|
||||
leading: Icon(
|
||||
icon,
|
||||
color: isCurrentStato ? AppColors.primary : AppColors.textSecondary,
|
||||
),
|
||||
title: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontWeight: isCurrentStato ? FontWeight.bold : FontWeight.normal,
|
||||
color: isCurrentStato ? AppColors.primary : null,
|
||||
),
|
||||
),
|
||||
trailing: isCurrentStato
|
||||
? const Icon(Icons.check, color: AppColors.primary)
|
||||
: null,
|
||||
onTap: isCurrentStato
|
||||
? null
|
||||
: () {
|
||||
Navigator.pop(context);
|
||||
_updateStato(stato);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dateFormat = DateFormat('dd/MM/yyyy HH:mm');
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Dettaglio Allarme'),
|
||||
actions: [
|
||||
if (!_isUpdating)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.edit),
|
||||
onPressed: _showStatoDialog,
|
||||
tooltip: 'Cambia stato',
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _isUpdating
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Header con severità
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [
|
||||
_getSeverityColor(),
|
||||
_getSeverityColor().withOpacity(0.7),
|
||||
],
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
bottom: false,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 6,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Text(
|
||||
_allarme.severitaReadable,
|
||||
style: TextStyle(
|
||||
color: _getSeverityColor(),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 6,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withOpacity(0.9),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
_getStatoIcon(),
|
||||
size: 16,
|
||||
color: _getStatoColor(),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
_allarme.statoReadable,
|
||||
style: TextStyle(
|
||||
color: _getStatoColor(),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
_allarme.titolo,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
AlarmTypeIcons.getIcon(_allarme.tipo),
|
||||
color: Colors.white.withOpacity(0.9),
|
||||
size: 18,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
_allarme.tipoReadable,
|
||||
style: TextStyle(
|
||||
color: Colors.white.withOpacity(0.9),
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Informazioni principali
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Descrizione
|
||||
if (_allarme.descrizione != null) ...[
|
||||
const Text(
|
||||
'Descrizione',
|
||||
style: AppTextStyles.h3,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
_allarme.descrizione!,
|
||||
style: AppTextStyles.bodyLarge,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
|
||||
// Valori rilevati
|
||||
if (_allarme.valoreRilevato != null &&
|
||||
_allarme.valoreSoglia != null) ...[
|
||||
const Text(
|
||||
'Valori Rilevati',
|
||||
style: AppTextStyles.h3,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildInfoCard(
|
||||
icon: Icons.analytics,
|
||||
title: 'Valore Rilevato',
|
||||
value:
|
||||
'${_allarme.valoreRilevato!.toStringAsFixed(2)} ${_allarme.unitaMisura ?? ''}',
|
||||
color: _getSeverityColor(),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
icon: Icons.straighten,
|
||||
title: 'Soglia Impostata',
|
||||
value:
|
||||
'${_allarme.valoreSoglia!.toStringAsFixed(2)} ${_allarme.unitaMisura ?? ''}',
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
|
||||
// Dati sensori
|
||||
if (_allarme.datiSensori != null &&
|
||||
_allarme.datiSensori!.isNotEmpty) ...[
|
||||
const Text(
|
||||
'Dati Sensori',
|
||||
style: AppTextStyles.h3,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildSensorsData(),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
|
||||
// Informazioni temporali
|
||||
const Text(
|
||||
'Informazioni Temporali',
|
||||
style: AppTextStyles.h3,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildInfoCard(
|
||||
icon: Icons.access_time,
|
||||
title: 'Rilevato il',
|
||||
value: dateFormat.format(_allarme.timestampRilevamento),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
icon: Icons.schedule,
|
||||
title: 'Creato il',
|
||||
value: dateFormat.format(_allarme.createdAt),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Mappa con posizione sito
|
||||
if (_sito != null && _sito!.hasCoordinates) ...[
|
||||
const Text(
|
||||
'Posizione Sito',
|
||||
style: AppTextStyles.h3,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildMapView(),
|
||||
const SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
icon: Icons.place,
|
||||
title: 'Sito',
|
||||
value: '${_sito!.nome} - ${_sito!.localita}',
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
] else if (_isLoadingSito) ...[
|
||||
const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(20.0),
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
|
||||
// Dettagli tecnici
|
||||
const Text(
|
||||
'Dettagli Tecnici',
|
||||
style: AppTextStyles.h3,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildInfoCard(
|
||||
icon: Icons.tag,
|
||||
title: 'ID Allarme',
|
||||
value: '#${_allarme.id}',
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
icon: Icons.location_on,
|
||||
title: 'ID Sito',
|
||||
value: '#${_allarme.sitoId}',
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
bottomNavigationBar: !_isUpdating && _allarme.stato != 'risolto'
|
||||
? SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () => _updateStato('risolto'),
|
||||
icon: const Icon(Icons.check_circle),
|
||||
label: const Text('Segna come Risolto'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppColors.success,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoCard({
|
||||
required IconData icon,
|
||||
required String title,
|
||||
required String value,
|
||||
Color? color,
|
||||
}) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey[100],
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.grey[300]!),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, color: color ?? AppColors.primary, size: 20),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
title,
|
||||
style: AppTextStyles.bodySmall.copyWith(
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
value,
|
||||
style: AppTextStyles.bodyMedium.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSensorsData() {
|
||||
final jsonEncoder = const JsonEncoder.withIndent(' ');
|
||||
final prettyJson = jsonEncoder.convert(_allarme.datiSensori);
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey[900],
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.sensors,
|
||||
color: Colors.greenAccent,
|
||||
size: 20,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'JSON Data',
|
||||
style: TextStyle(
|
||||
color: Colors.greenAccent,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Text(
|
||||
prettyJson,
|
||||
style: const TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
color: Colors.white,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
IconData _getStatoIcon() {
|
||||
switch (_allarme.stato) {
|
||||
case 'nuovo':
|
||||
return Icons.fiber_new;
|
||||
case 'in_gestione':
|
||||
return Icons.engineering;
|
||||
case 'risolto':
|
||||
return Icons.check_circle;
|
||||
default:
|
||||
return Icons.info;
|
||||
}
|
||||
}
|
||||
|
||||
Color _getStatoColor() {
|
||||
switch (_allarme.stato) {
|
||||
case 'nuovo':
|
||||
return AppColors.critical;
|
||||
case 'in_gestione':
|
||||
return AppColors.warning;
|
||||
case 'risolto':
|
||||
return AppColors.success;
|
||||
default:
|
||||
return AppColors.textSecondary;
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildMapView() {
|
||||
if (_sito == null || !_sito!.hasCoordinates) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
final position = LatLng(_sito!.latitudine!, _sito!.longitudine!);
|
||||
|
||||
return Container(
|
||||
height: 200,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.grey[300]!),
|
||||
),
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: GoogleMap(
|
||||
initialCameraPosition: CameraPosition(
|
||||
target: position,
|
||||
zoom: 15,
|
||||
),
|
||||
markers: {
|
||||
Marker(
|
||||
markerId: MarkerId('sito_${_sito!.id}'),
|
||||
position: position,
|
||||
infoWindow: InfoWindow(
|
||||
title: _sito!.nome,
|
||||
snippet: _sito!.tipoReadable,
|
||||
),
|
||||
icon: BitmapDescriptor.defaultMarkerWithHue(
|
||||
_getSeverityColor() == AppColors.critical
|
||||
? BitmapDescriptor.hueRed
|
||||
: _getSeverityColor() == AppColors.warning
|
||||
? BitmapDescriptor.hueOrange
|
||||
: BitmapDescriptor.hueBlue,
|
||||
),
|
||||
),
|
||||
},
|
||||
myLocationButtonEnabled: false,
|
||||
zoomControlsEnabled: true,
|
||||
mapToolbarEnabled: false,
|
||||
onMapCreated: (controller) {
|
||||
_mapController = controller;
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
619
lib/screens/dashboard_screen.dart
Normal file
619
lib/screens/dashboard_screen.dart
Normal file
@@ -0,0 +1,619 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import '../models/statistiche.dart';
|
||||
import '../services/api_service.dart';
|
||||
import '../utils/constants.dart';
|
||||
|
||||
class DashboardScreen extends StatefulWidget {
|
||||
const DashboardScreen({super.key});
|
||||
|
||||
@override
|
||||
State<DashboardScreen> createState() => _DashboardScreenState();
|
||||
}
|
||||
|
||||
class _DashboardScreenState extends State<DashboardScreen> {
|
||||
final _apiService = ApiService();
|
||||
Statistiche? _statistiche;
|
||||
AllarmiPerGiornoResponse? _allarmiPerGiorno;
|
||||
bool _isLoading = true;
|
||||
String? _errorMessage;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadData();
|
||||
}
|
||||
|
||||
Future<void> _loadData() async {
|
||||
setState(() => _isLoading = true);
|
||||
|
||||
try {
|
||||
final stats = await _apiService.getStatistiche();
|
||||
final allarmiGiorno = await _apiService.getAllarmiPerGiorno(giorni: 30);
|
||||
|
||||
setState(() {
|
||||
_statistiche = stats;
|
||||
_allarmiPerGiorno = allarmiGiorno;
|
||||
_isLoading = false;
|
||||
_errorMessage = null;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_errorMessage = 'Errore caricamento dati: $e';
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Dashboard'),
|
||||
Text('Panoramica generale', style: TextStyle(fontSize: 12)),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: _loadData,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _errorMessage != null
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.error_outline,
|
||||
size: 64,
|
||||
color: AppColors.critical,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(_errorMessage!),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _loadData,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('Riprova'),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: RefreshIndicator(
|
||||
onRefresh: _loadData,
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// KPI Cards
|
||||
_buildKPISection(),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Grafico Severità
|
||||
_buildSeveritaChart(),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Grafico Stati
|
||||
_buildStatiChart(),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Grafico Temporale
|
||||
_buildTimelineChart(),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Grafico Siti per Tipo
|
||||
_buildSitiPerTipoChart(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildKPISection() {
|
||||
if (_statistiche == null) return const SizedBox.shrink();
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('Indicatori Chiave', style: AppTextStyles.h2),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildKPICard(
|
||||
'Allarmi Totali',
|
||||
_statistiche!.totaleAllarmi.toString(),
|
||||
Icons.notifications_active,
|
||||
AppColors.primary,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: AppSizes.paddingM),
|
||||
Expanded(
|
||||
child: _buildKPICard(
|
||||
'Siti Monitorati',
|
||||
_statistiche!.totaleSiti.toString(),
|
||||
Icons.location_on,
|
||||
AppColors.secondary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: AppSizes.paddingM),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildKPICard(
|
||||
'Aperti',
|
||||
_statistiche!.allarmiAperti.toString(),
|
||||
Icons.warning,
|
||||
AppColors.warning,
|
||||
subtitle:
|
||||
'${_statistiche!.percentualeAperti.toStringAsFixed(1)}%',
|
||||
),
|
||||
),
|
||||
const SizedBox(width: AppSizes.paddingM),
|
||||
Expanded(
|
||||
child: _buildKPICard(
|
||||
'Critici',
|
||||
_statistiche!.allarmiCritical.toString(),
|
||||
Icons.error,
|
||||
AppColors.critical,
|
||||
subtitle:
|
||||
'${_statistiche!.percentualeCritici.toStringAsFixed(1)}%',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildKPICard(
|
||||
String label,
|
||||
String value,
|
||||
IconData icon,
|
||||
Color color, {
|
||||
String? subtitle,
|
||||
}) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [color, color.withOpacity(0.7)],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: color.withOpacity(0.3),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Icon(icon, color: Colors.white, size: 28),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
value,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 32,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
color: Colors.white.withOpacity(0.9),
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
if (subtitle != null) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
subtitle,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSeveritaChart() {
|
||||
if (_statistiche == null) return const SizedBox.shrink();
|
||||
|
||||
final sections = [
|
||||
if (_statistiche!.allarmiCritical > 0)
|
||||
PieChartSectionData(
|
||||
color: AppColors.critical,
|
||||
value: _statistiche!.allarmiCritical.toDouble(),
|
||||
title: '${_statistiche!.allarmiCritical}',
|
||||
radius: 50,
|
||||
titleStyle: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
if (_statistiche!.allarmiWarning > 0)
|
||||
PieChartSectionData(
|
||||
color: AppColors.warning,
|
||||
value: _statistiche!.allarmiWarning.toDouble(),
|
||||
title: '${_statistiche!.allarmiWarning}',
|
||||
radius: 50,
|
||||
titleStyle: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
if (_statistiche!.allarmiInfo > 0)
|
||||
PieChartSectionData(
|
||||
color: AppColors.info,
|
||||
value: _statistiche!.allarmiInfo.toDouble(),
|
||||
title: '${_statistiche!.allarmiInfo}',
|
||||
radius: 50,
|
||||
titleStyle: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
if (sections.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return _buildChartCard(
|
||||
'Allarmi per Severità',
|
||||
Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
height: 200,
|
||||
child: PieChart(
|
||||
PieChartData(
|
||||
sections: sections,
|
||||
sectionsSpace: 2,
|
||||
centerSpaceRadius: 40,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildLegend([
|
||||
_LegendItem('Critici', AppColors.critical, _statistiche!.allarmiCritical),
|
||||
_LegendItem('Avvisi', AppColors.warning, _statistiche!.allarmiWarning),
|
||||
_LegendItem('Info', AppColors.info, _statistiche!.allarmiInfo),
|
||||
]),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatiChart() {
|
||||
if (_statistiche == null) return const SizedBox.shrink();
|
||||
|
||||
return _buildChartCard(
|
||||
'Allarmi per Stato',
|
||||
Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
height: 200,
|
||||
child: BarChart(
|
||||
BarChartData(
|
||||
alignment: BarChartAlignment.spaceAround,
|
||||
maxY: _statistiche!.totaleStato.toDouble() * 1.2,
|
||||
barTouchData: BarTouchData(enabled: false),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: (value, meta) {
|
||||
switch (value.toInt()) {
|
||||
case 0:
|
||||
return const Text('Nuovi', style: TextStyle(fontSize: 12));
|
||||
case 1:
|
||||
return const Text('In Gest.', style: TextStyle(fontSize: 12));
|
||||
case 2:
|
||||
return const Text('Risolti', style: TextStyle(fontSize: 12));
|
||||
default:
|
||||
return const Text('');
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
leftTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
barGroups: [
|
||||
BarChartGroupData(
|
||||
x: 0,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: _statistiche!.allarmiNuovo.toDouble(),
|
||||
color: AppColors.critical,
|
||||
width: 40,
|
||||
borderRadius: const BorderRadius.vertical(top: Radius.circular(4)),
|
||||
),
|
||||
],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 1,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: _statistiche!.allarmiInGestione.toDouble(),
|
||||
color: AppColors.warning,
|
||||
width: 40,
|
||||
borderRadius: const BorderRadius.vertical(top: Radius.circular(4)),
|
||||
),
|
||||
],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 2,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: _statistiche!.allarmiRisolto.toDouble(),
|
||||
color: AppColors.success,
|
||||
width: 40,
|
||||
borderRadius: const BorderRadius.vertical(top: Radius.circular(4)),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTimelineChart() {
|
||||
if (_allarmiPerGiorno == null || _allarmiPerGiorno!.dati.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
final spots = _allarmiPerGiorno!.dati.asMap().entries.map((entry) {
|
||||
return FlSpot(entry.key.toDouble(), entry.value.count.toDouble());
|
||||
}).toList();
|
||||
|
||||
final maxY = _allarmiPerGiorno!.dati
|
||||
.map((d) => d.count)
|
||||
.reduce((a, b) => a > b ? a : b)
|
||||
.toDouble();
|
||||
|
||||
return _buildChartCard(
|
||||
'Andamento Temporale (30 giorni)',
|
||||
SizedBox(
|
||||
height: 200,
|
||||
child: LineChart(
|
||||
LineChartData(
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawVerticalLine: false,
|
||||
horizontalInterval: 1,
|
||||
getDrawingHorizontalLine: (value) {
|
||||
return FlLine(
|
||||
color: Colors.grey[300],
|
||||
strokeWidth: 1,
|
||||
);
|
||||
},
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 30,
|
||||
interval: 5,
|
||||
getTitlesWidget: (value, meta) {
|
||||
if (value.toInt() >= _allarmiPerGiorno!.dati.length) {
|
||||
return const Text('');
|
||||
}
|
||||
final data = _allarmiPerGiorno!.dati[value.toInt()].data;
|
||||
return Text(
|
||||
DateFormat('d/M').format(data),
|
||||
style: const TextStyle(fontSize: 10),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
leftTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
minX: 0,
|
||||
maxX: spots.length.toDouble() - 1,
|
||||
minY: 0,
|
||||
maxY: maxY * 1.2,
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
spots: spots,
|
||||
isCurved: true,
|
||||
color: AppColors.primary,
|
||||
barWidth: 3,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(show: false),
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
color: AppColors.primary.withOpacity(0.1),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSitiPerTipoChart() {
|
||||
if (_statistiche == null) return const SizedBox.shrink();
|
||||
|
||||
final data = [
|
||||
_ChartData('Ponti', _statistiche!.sitiPonte, Colors.blue),
|
||||
_ChartData('Gallerie', _statistiche!.sitiGalleria, Colors.brown),
|
||||
_ChartData('Dighe', _statistiche!.sitiDiga, Colors.cyan),
|
||||
_ChartData('Frane', _statistiche!.sitiFrana, Colors.orange),
|
||||
_ChartData('Versanti', _statistiche!.sitiVersante, Colors.green),
|
||||
_ChartData('Edifici', _statistiche!.sitiEdificio, Colors.purple),
|
||||
].where((d) => d.value > 0).toList();
|
||||
|
||||
if (data.isEmpty) return const SizedBox.shrink();
|
||||
|
||||
return _buildChartCard(
|
||||
'Siti per Tipologia',
|
||||
SizedBox(
|
||||
height: 200,
|
||||
child: BarChart(
|
||||
BarChartData(
|
||||
alignment: BarChartAlignment.spaceAround,
|
||||
maxY: data.map((d) => d.value).reduce((a, b) => a > b ? a : b).toDouble() * 1.2,
|
||||
barTouchData: BarTouchData(enabled: false),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: (value, meta) {
|
||||
if (value.toInt() >= data.length) return const Text('');
|
||||
return Text(
|
||||
data[value.toInt()].label.substring(0, 3),
|
||||
style: const TextStyle(fontSize: 12),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
leftTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
barGroups: data.asMap().entries.map((entry) {
|
||||
return BarChartGroupData(
|
||||
x: entry.key,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: entry.value.value.toDouble(),
|
||||
color: entry.value.color,
|
||||
width: 30,
|
||||
borderRadius: const BorderRadius.vertical(top: Radius.circular(4)),
|
||||
),
|
||||
],
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildChartCard(String title, Widget child) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.05),
|
||||
blurRadius: 10,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(title, style: AppTextStyles.h3),
|
||||
const SizedBox(height: 16),
|
||||
child,
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLegend(List<_LegendItem> items) {
|
||||
return Wrap(
|
||||
spacing: 16,
|
||||
runSpacing: 8,
|
||||
children: items.map((item) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
width: 16,
|
||||
height: 16,
|
||||
decoration: BoxDecoration(
|
||||
color: item.color,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'${item.label}: ${item.value}',
|
||||
style: AppTextStyles.bodySmall,
|
||||
),
|
||||
],
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LegendItem {
|
||||
final String label;
|
||||
final Color color;
|
||||
final int value;
|
||||
|
||||
_LegendItem(this.label, this.color, this.value);
|
||||
}
|
||||
|
||||
class _ChartData {
|
||||
final String label;
|
||||
final int value;
|
||||
final Color color;
|
||||
|
||||
_ChartData(this.label, this.value, this.color);
|
||||
}
|
||||
226
lib/screens/home_screen.dart
Normal file
226
lib/screens/home_screen.dart
Normal file
@@ -0,0 +1,226 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../utils/constants.dart';
|
||||
import '../models/allarme.dart';
|
||||
import '../services/api_service.dart';
|
||||
import '../widgets/allarme_card.dart';
|
||||
|
||||
class HomeScreen extends StatefulWidget {
|
||||
const HomeScreen({super.key});
|
||||
|
||||
@override
|
||||
State<HomeScreen> createState() => _HomeScreenState();
|
||||
}
|
||||
|
||||
class _HomeScreenState extends State<HomeScreen> {
|
||||
final _apiService = ApiService();
|
||||
List<Allarme> _allarmi = [];
|
||||
List<Allarme> _allarmiFiltered = [];
|
||||
bool _isLoading = true;
|
||||
String? _errorMessage;
|
||||
String? _selectedSeverita;
|
||||
String _searchQuery = '';
|
||||
String _sortBy = 'data_desc'; // data_desc, data_asc, severita
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadAllarmi();
|
||||
}
|
||||
|
||||
Future<void> _loadAllarmi({bool refresh = false}) async {
|
||||
if (refresh) setState(() => _isLoading = true);
|
||||
|
||||
try {
|
||||
final response = await _apiService.getAllarmi(severita: _selectedSeverita);
|
||||
setState(() {
|
||||
_allarmi = response.items;
|
||||
_applyFiltersAndSort();
|
||||
_isLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_errorMessage = 'Errore caricamento allarmi';
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _applyFiltersAndSort() {
|
||||
// Filtra per ricerca
|
||||
var filtered = _allarmi.where((allarme) {
|
||||
if (_searchQuery.isEmpty) return true;
|
||||
final query = _searchQuery.toLowerCase();
|
||||
return allarme.titolo.toLowerCase().contains(query) ||
|
||||
allarme.descrizione?.toLowerCase().contains(query) == true ||
|
||||
allarme.tipoReadable.toLowerCase().contains(query);
|
||||
}).toList();
|
||||
|
||||
// Ordina
|
||||
switch (_sortBy) {
|
||||
case 'data_desc':
|
||||
filtered.sort((a, b) => b.timestampRilevamento.compareTo(a.timestampRilevamento));
|
||||
break;
|
||||
case 'data_asc':
|
||||
filtered.sort((a, b) => a.timestampRilevamento.compareTo(b.timestampRilevamento));
|
||||
break;
|
||||
case 'severita':
|
||||
filtered.sort((a, b) {
|
||||
final severityOrder = {'critical': 0, 'warning': 1, 'info': 2};
|
||||
return (severityOrder[a.severita] ?? 3).compareTo(severityOrder[b.severita] ?? 3);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
_allarmiFiltered = filtered;
|
||||
}
|
||||
|
||||
void _showFilterDialog() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Filtri'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ListTile(
|
||||
title: const Text('Tutti'),
|
||||
leading: Radio<String?>(value: null, groupValue: _selectedSeverita, onChanged: (val) { Navigator.pop(context); setState(() => _selectedSeverita = val); _loadAllarmi(refresh: true); }),
|
||||
),
|
||||
ListTile(
|
||||
title: const Text('CRITICO'),
|
||||
leading: Radio<String?>(value: 'critical', groupValue: _selectedSeverita, onChanged: (val) { Navigator.pop(context); setState(() => _selectedSeverita = val); _loadAllarmi(refresh: true); }),
|
||||
),
|
||||
ListTile(
|
||||
title: const Text('AVVISO'),
|
||||
leading: Radio<String?>(value: 'warning', groupValue: _selectedSeverita, onChanged: (val) { Navigator.pop(context); setState(() => _selectedSeverita = val); _loadAllarmi(refresh: true); }),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showSortDialog() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Ordina per'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildSortOption('data_desc', 'Più recenti', Icons.arrow_downward),
|
||||
_buildSortOption('data_asc', 'Meno recenti', Icons.arrow_upward),
|
||||
_buildSortOption('severita', 'Severità', Icons.warning),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSortOption(String value, String label, IconData icon) {
|
||||
final isSelected = _sortBy == value;
|
||||
|
||||
return ListTile(
|
||||
leading: Icon(
|
||||
icon,
|
||||
color: isSelected ? AppColors.primary : AppColors.textSecondary,
|
||||
),
|
||||
title: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
|
||||
color: isSelected ? AppColors.primary : null,
|
||||
),
|
||||
),
|
||||
trailing: isSelected ? const Icon(Icons.check, color: AppColors.primary) : null,
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
setState(() {
|
||||
_sortBy = value;
|
||||
_applyFiltersAndSort();
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Column(
|
||||
children: [Text('ASE Monitor'), Text('Allarmi Attivi', style: TextStyle(fontSize: 12))],
|
||||
),
|
||||
actions: [
|
||||
IconButton(icon: const Icon(Icons.sort), onPressed: _showSortDialog),
|
||||
IconButton(icon: const Icon(Icons.filter_list), onPressed: _showFilterDialog),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
// Search bar
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingM),
|
||||
child: TextField(
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Cerca allarmi...',
|
||||
prefixIcon: const Icon(Icons.search),
|
||||
suffixIcon: _searchQuery.isNotEmpty
|
||||
? IconButton(
|
||||
icon: const Icon(Icons.clear),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_searchQuery = '';
|
||||
_applyFiltersAndSort();
|
||||
});
|
||||
},
|
||||
)
|
||||
: null,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
filled: true,
|
||||
fillColor: Colors.grey[100],
|
||||
),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_searchQuery = value;
|
||||
_applyFiltersAndSort();
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
// Results count
|
||||
if (_searchQuery.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: AppSizes.paddingM),
|
||||
child: Text(
|
||||
'${_allarmiFiltered.length} risultati trovati',
|
||||
style: AppTextStyles.bodySmall.copyWith(color: AppColors.textSecondary),
|
||||
),
|
||||
),
|
||||
// List
|
||||
Expanded(
|
||||
child: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _errorMessage != null
|
||||
? Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [const Icon(Icons.error_outline, size: 64, color: AppColors.critical), Text(_errorMessage!)]))
|
||||
: _allarmiFiltered.isEmpty
|
||||
? const Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [Icon(Icons.check_circle_outline, size: 64, color: AppColors.success), SizedBox(height: 16), Text('Nessun allarme', style: AppTextStyles.h3)]))
|
||||
: RefreshIndicator(
|
||||
onRefresh: () => _loadAllarmi(refresh: true),
|
||||
child: ListView.builder(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingM),
|
||||
itemCount: _allarmiFiltered.length,
|
||||
itemBuilder: (context, index) => AllarmeCard(allarme: _allarmiFiltered[index]),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () => _loadAllarmi(refresh: true),
|
||||
child: const Icon(Icons.refresh),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
224
lib/screens/login_screen.dart
Normal file
224
lib/screens/login_screen.dart
Normal file
@@ -0,0 +1,224 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../utils/constants.dart';
|
||||
import '../services/auth_service.dart';
|
||||
import '../services/notification_service.dart';
|
||||
import 'main_screen.dart';
|
||||
|
||||
class LoginScreen extends StatefulWidget {
|
||||
const LoginScreen({super.key});
|
||||
|
||||
@override
|
||||
State<LoginScreen> createState() => _LoginScreenState();
|
||||
}
|
||||
|
||||
class _LoginScreenState extends State<LoginScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _emailController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
final _authService = AuthService();
|
||||
|
||||
bool _isLoading = false;
|
||||
bool _obscurePassword = true;
|
||||
String? _errorMessage;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_emailController.dispose();
|
||||
_passwordController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _handleLogin() async {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final success = await _authService.login(
|
||||
_emailController.text.trim(),
|
||||
_passwordController.text,
|
||||
);
|
||||
|
||||
if (success && mounted) {
|
||||
// Registra FCM token dopo login riuscito
|
||||
try {
|
||||
final fcmToken = await NotificationService().getFcmToken();
|
||||
if (fcmToken != null) {
|
||||
await _authService.saveFcmToken(fcmToken);
|
||||
print('✓ FCM token registrato dopo login');
|
||||
}
|
||||
} catch (e) {
|
||||
print('⚠️ Errore registrazione FCM token: $e');
|
||||
// Non blocchiamo il login se la registrazione token fallisce
|
||||
}
|
||||
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(builder: (context) => const MainScreen()),
|
||||
);
|
||||
} else {
|
||||
setState(() {
|
||||
_errorMessage = 'Email o password non corretti';
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_errorMessage = 'Errore durante il login';
|
||||
});
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: AppColors.primaryGradient,
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
_buildHeader(),
|
||||
const SizedBox(height: 60),
|
||||
_buildLoginCard(),
|
||||
const SizedBox(height: AppSizes.paddingL),
|
||||
_buildFooter(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader() {
|
||||
return Column(
|
||||
children: [
|
||||
Container(
|
||||
width: 120,
|
||||
height: 120,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.2),
|
||||
blurRadius: 20,
|
||||
offset: const Offset(0, 10),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.terrain, size: 50, color: AppColors.primary),
|
||||
SizedBox(height: 4),
|
||||
Text('ASE', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: AppColors.primary)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: AppSizes.paddingL),
|
||||
const Text('ASE Monitor', style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold, color: Colors.white)),
|
||||
const SizedBox(height: AppSizes.paddingS),
|
||||
Text('Advanced Slope Engineering', style: TextStyle(fontSize: 16, color: Colors.white.withOpacity(0.9))),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoginCard() {
|
||||
return Card(
|
||||
elevation: 8,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(AppSizes.radiusL)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text('Accedi', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), textAlign: TextAlign.center),
|
||||
const SizedBox(height: AppSizes.paddingL),
|
||||
TextFormField(
|
||||
controller: _emailController,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: const InputDecoration(labelText: 'Email', hintText: 'nome@azienda.it', prefixIcon: Icon(Icons.email_outlined)),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) return 'Inserisci email';
|
||||
if (!value.contains('@')) return 'Email non valida';
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: AppSizes.paddingM),
|
||||
TextFormField(
|
||||
controller: _passwordController,
|
||||
obscureText: _obscurePassword,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Password',
|
||||
prefixIcon: const Icon(Icons.lock_outline),
|
||||
suffixIcon: IconButton(
|
||||
icon: Icon(_obscurePassword ? Icons.visibility_outlined : Icons.visibility_off_outlined),
|
||||
onPressed: () => setState(() => _obscurePassword = !_obscurePassword),
|
||||
),
|
||||
),
|
||||
validator: (value) => (value == null || value.isEmpty) ? 'Inserisci password' : null,
|
||||
onFieldSubmitted: (_) => _handleLogin(),
|
||||
),
|
||||
if (_errorMessage != null) ...[
|
||||
const SizedBox(height: AppSizes.paddingM),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingM),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.critical.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(AppSizes.radiusS),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.error_outline, color: AppColors.critical, size: 20),
|
||||
const SizedBox(width: AppSizes.paddingS),
|
||||
Expanded(child: Text(_errorMessage!, style: const TextStyle(color: AppColors.critical))),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: AppSizes.paddingL),
|
||||
SizedBox(
|
||||
height: AppSizes.buttonHeight,
|
||||
child: ElevatedButton(
|
||||
onPressed: _isLoading ? null : _handleLogin,
|
||||
child: _isLoading
|
||||
? const SizedBox(width: 24, height: 24, child: CircularProgressIndicator(strokeWidth: 2, valueColor: AlwaysStoppedAnimation(Colors.white)))
|
||||
: const Row(mainAxisAlignment: MainAxisAlignment.center, children: [Icon(Icons.login), SizedBox(width: 8), Text('Accedi')]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFooter() {
|
||||
return Column(
|
||||
children: [
|
||||
Text('Versione ${AppConstants.appVersion}', style: TextStyle(color: Colors.white.withOpacity(0.7), fontSize: 12)),
|
||||
const SizedBox(height: 4),
|
||||
Text('© 2025 ${AppConstants.companyName}', style: TextStyle(color: Colors.white.withOpacity(0.7), fontSize: 12), textAlign: TextAlign.center),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
55
lib/screens/main_screen.dart
Normal file
55
lib/screens/main_screen.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../utils/constants.dart';
|
||||
import 'dashboard_screen.dart';
|
||||
import 'home_screen.dart';
|
||||
import 'siti_screen.dart';
|
||||
import 'profile_screen.dart';
|
||||
|
||||
class MainScreen extends StatefulWidget {
|
||||
const MainScreen({super.key});
|
||||
|
||||
@override
|
||||
State<MainScreen> createState() => _MainScreenState();
|
||||
}
|
||||
|
||||
class _MainScreenState extends State<MainScreen> {
|
||||
int _currentIndex = 0;
|
||||
|
||||
final List<Widget> _screens = [
|
||||
const DashboardScreen(),
|
||||
const HomeScreen(),
|
||||
const SitiScreen(),
|
||||
const ProfileScreen(),
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: _screens[_currentIndex],
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
currentIndex: _currentIndex,
|
||||
onTap: (index) => setState(() => _currentIndex = index),
|
||||
selectedItemColor: AppColors.primary,
|
||||
unselectedItemColor: AppColors.textSecondary,
|
||||
items: const [
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.dashboard),
|
||||
label: 'Dashboard',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.notifications_active),
|
||||
label: 'Allarmi',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.location_on),
|
||||
label: 'Siti',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.person),
|
||||
label: 'Profilo',
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
97
lib/screens/profile_screen.dart
Normal file
97
lib/screens/profile_screen.dart
Normal file
@@ -0,0 +1,97 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../utils/constants.dart';
|
||||
import '../services/auth_service.dart';
|
||||
import 'login_screen.dart';
|
||||
|
||||
class ProfileScreen extends StatelessWidget {
|
||||
const ProfileScreen({super.key});
|
||||
|
||||
Future<void> _handleLogout(BuildContext context) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Conferma Logout'),
|
||||
content: const Text('Sei sicuro di voler uscire?'),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.pop(context, false), child: const Text('Annulla')),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
style: ElevatedButton.styleFrom(backgroundColor: AppColors.critical),
|
||||
child: const Text('Esci'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed == true && context.mounted) {
|
||||
await AuthService().logout();
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (_) => const LoginScreen()), (_) => false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final user = AuthService().currentUser;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Profilo')),
|
||||
body: user == null
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingM),
|
||||
child: Column(
|
||||
children: [
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
child: Column(
|
||||
children: [
|
||||
CircleAvatar(
|
||||
radius: 50,
|
||||
backgroundColor: AppColors.primary,
|
||||
child: Text('${user.nome[0]}${user.cognome[0]}'.toUpperCase(), style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold, color: Colors.white)),
|
||||
),
|
||||
const SizedBox(height: AppSizes.paddingM),
|
||||
Text(user.nomeCompleto, style: AppTextStyles.h2, textAlign: TextAlign.center),
|
||||
Text(user.email, style: AppTextStyles.bodyMedium.copyWith(color: AppColors.textSecondary)),
|
||||
const SizedBox(height: AppSizes.paddingM),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: AppSizes.paddingM, vertical: AppSizes.paddingS),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.primary.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(AppSizes.radiusL),
|
||||
),
|
||||
child: Text(user.ruolo.toUpperCase(), style: const TextStyle(color: AppColors.primary, fontWeight: FontWeight.w600)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: AppSizes.paddingM),
|
||||
Card(
|
||||
child: Column(
|
||||
children: [
|
||||
ListTile(leading: const Icon(Icons.business_outlined), title: const Text('ID Cliente'), trailing: Text(user.clienteId.toString())),
|
||||
ListTile(leading: const Icon(Icons.verified_outlined), title: const Text('Versione'), trailing: Text(AppConstants.appVersion)),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: AppSizes.paddingM),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: AppSizes.buttonHeight,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () => _handleLogout(context),
|
||||
icon: const Icon(Icons.logout),
|
||||
label: const Text('Esci'),
|
||||
style: ElevatedButton.styleFrom(backgroundColor: AppColors.critical),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
221
lib/screens/siti_screen.dart
Normal file
221
lib/screens/siti_screen.dart
Normal file
@@ -0,0 +1,221 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../models/sito.dart';
|
||||
import '../services/api_service.dart';
|
||||
import '../widgets/sito_card.dart';
|
||||
import '../utils/constants.dart';
|
||||
|
||||
class SitiScreen extends StatefulWidget {
|
||||
const SitiScreen({super.key});
|
||||
|
||||
@override
|
||||
State<SitiScreen> createState() => _SitiScreenState();
|
||||
}
|
||||
|
||||
class _SitiScreenState extends State<SitiScreen> {
|
||||
final _apiService = ApiService();
|
||||
List<Sito> _siti = [];
|
||||
List<Sito> _sitiFiltered = [];
|
||||
bool _isLoading = true;
|
||||
String? _errorMessage;
|
||||
String? _selectedTipo;
|
||||
|
||||
final Map<String, String> _tipiSito = {
|
||||
'ponte': 'Ponte',
|
||||
'galleria': 'Galleria',
|
||||
'diga': 'Diga',
|
||||
'frana': 'Frana',
|
||||
'versante': 'Versante',
|
||||
'edificio': 'Edificio',
|
||||
};
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadSiti();
|
||||
}
|
||||
|
||||
Future<void> _loadSiti({bool refresh = false}) async {
|
||||
if (refresh) setState(() => _isLoading = true);
|
||||
|
||||
try {
|
||||
final siti = await _apiService.getSiti();
|
||||
setState(() {
|
||||
_siti = siti;
|
||||
_applyFilter();
|
||||
_isLoading = false;
|
||||
_errorMessage = null;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_errorMessage = 'Errore caricamento siti: $e';
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _applyFilter() {
|
||||
if (_selectedTipo == null) {
|
||||
_sitiFiltered = _siti;
|
||||
} else {
|
||||
_sitiFiltered = _siti.where((s) => s.tipo == _selectedTipo).toList();
|
||||
}
|
||||
}
|
||||
|
||||
void _showFilterDialog() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Filtra per Tipo'),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildFilterOption(null, 'Tutti i siti', Icons.all_inclusive),
|
||||
const Divider(),
|
||||
..._tipiSito.entries.map((entry) {
|
||||
return _buildFilterOption(
|
||||
entry.key,
|
||||
entry.value,
|
||||
_getIconForTipo(entry.key),
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFilterOption(String? tipo, String label, IconData icon) {
|
||||
final isSelected = _selectedTipo == tipo;
|
||||
|
||||
return ListTile(
|
||||
leading: Icon(
|
||||
icon,
|
||||
color: isSelected ? AppColors.primary : AppColors.textSecondary,
|
||||
),
|
||||
title: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
|
||||
color: isSelected ? AppColors.primary : null,
|
||||
),
|
||||
),
|
||||
trailing: isSelected
|
||||
? const Icon(Icons.check, color: AppColors.primary)
|
||||
: null,
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
setState(() {
|
||||
_selectedTipo = tipo;
|
||||
_applyFilter();
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
IconData _getIconForTipo(String tipo) {
|
||||
switch (tipo) {
|
||||
case 'ponte':
|
||||
return Icons.architecture;
|
||||
case 'galleria':
|
||||
return Icons.south_west;
|
||||
case 'diga':
|
||||
return Icons.water_damage;
|
||||
case 'frana':
|
||||
return Icons.landscape;
|
||||
case 'versante':
|
||||
return Icons.terrain;
|
||||
case 'edificio':
|
||||
return Icons.business;
|
||||
default:
|
||||
return Icons.location_on;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('Siti Monitorati'),
|
||||
Text(
|
||||
_selectedTipo != null
|
||||
? _tipiSito[_selectedTipo]!
|
||||
: '${_siti.length} siti totali',
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.filter_list),
|
||||
onPressed: _showFilterDialog,
|
||||
tooltip: 'Filtra per tipo',
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _errorMessage != null
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.error_outline,
|
||||
size: 64,
|
||||
color: AppColors.critical,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(_errorMessage!),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton.icon(
|
||||
onPressed: () => _loadSiti(refresh: true),
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('Riprova'),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: _sitiFiltered.isEmpty
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.location_off,
|
||||
size: 64,
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
_selectedTipo != null
|
||||
? 'Nessun sito di tipo ${_tipiSito[_selectedTipo]}'
|
||||
: 'Nessun sito disponibile',
|
||||
style: AppTextStyles.h3,
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: RefreshIndicator(
|
||||
onRefresh: () => _loadSiti(refresh: true),
|
||||
child: ListView.builder(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingM),
|
||||
itemCount: _sitiFiltered.length,
|
||||
itemBuilder: (context, index) {
|
||||
return SitoCard(sito: _sitiFiltered[index]);
|
||||
},
|
||||
),
|
||||
),
|
||||
floatingActionButton: _isLoading
|
||||
? null
|
||||
: FloatingActionButton(
|
||||
onPressed: () => _loadSiti(refresh: true),
|
||||
child: const Icon(Icons.refresh),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
494
lib/screens/sito_detail_screen.dart
Normal file
494
lib/screens/sito_detail_screen.dart
Normal file
@@ -0,0 +1,494 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import '../models/sito.dart';
|
||||
import '../models/allarme.dart';
|
||||
import '../services/api_service.dart';
|
||||
import '../widgets/allarme_card.dart';
|
||||
import '../utils/constants.dart';
|
||||
|
||||
class SitoDetailScreen extends StatefulWidget {
|
||||
final Sito sito;
|
||||
|
||||
const SitoDetailScreen({super.key, required this.sito});
|
||||
|
||||
@override
|
||||
State<SitoDetailScreen> createState() => _SitoDetailScreenState();
|
||||
}
|
||||
|
||||
class _SitoDetailScreenState extends State<SitoDetailScreen>
|
||||
with SingleTickerProviderStateMixin {
|
||||
final _apiService = ApiService();
|
||||
List<Allarme> _allarmi = [];
|
||||
bool _isLoadingAllarmi = true;
|
||||
String? _errorMessage;
|
||||
late TabController _tabController;
|
||||
GoogleMapController? _mapController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(length: 2, vsync: this);
|
||||
_loadAllarmi();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.dispose();
|
||||
_mapController?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _loadAllarmi() async {
|
||||
try {
|
||||
final response = await _apiService.getAllarmiBySito(widget.sito.id);
|
||||
setState(() {
|
||||
_allarmi = response.items;
|
||||
_isLoadingAllarmi = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_errorMessage = 'Errore caricamento allarmi: $e';
|
||||
_isLoadingAllarmi = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Color _getTipoColor() {
|
||||
switch (widget.sito.tipo) {
|
||||
case 'ponte':
|
||||
return Colors.blue;
|
||||
case 'galleria':
|
||||
return Colors.brown;
|
||||
case 'diga':
|
||||
return Colors.cyan;
|
||||
case 'frana':
|
||||
return Colors.orange;
|
||||
case 'versante':
|
||||
return Colors.green;
|
||||
case 'edificio':
|
||||
return Colors.purple;
|
||||
default:
|
||||
return AppColors.primary;
|
||||
}
|
||||
}
|
||||
|
||||
IconData _getTipoIcon() {
|
||||
switch (widget.sito.tipo) {
|
||||
case 'ponte':
|
||||
return Icons.architecture;
|
||||
case 'galleria':
|
||||
return Icons.south_west;
|
||||
case 'diga':
|
||||
return Icons.water_damage;
|
||||
case 'frana':
|
||||
return Icons.landscape;
|
||||
case 'versante':
|
||||
return Icons.terrain;
|
||||
case 'edificio':
|
||||
return Icons.business;
|
||||
default:
|
||||
return Icons.location_on;
|
||||
}
|
||||
}
|
||||
|
||||
int get _allarmiCritici =>
|
||||
_allarmi.where((a) => a.severita == 'critical').length;
|
||||
int get _allarmiAperti =>
|
||||
_allarmi.where((a) => a.stato != 'risolto').length;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dateFormat = DateFormat('dd/MM/yyyy');
|
||||
|
||||
return Scaffold(
|
||||
body: NestedScrollView(
|
||||
headerSliverBuilder: (context, innerBoxIsScrolled) {
|
||||
return [
|
||||
SliverAppBar(
|
||||
expandedHeight: 180,
|
||||
pinned: true,
|
||||
flexibleSpace: FlexibleSpaceBar(
|
||||
background: Container(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [
|
||||
_getTipoColor(),
|
||||
_getTipoColor().withOpacity(0.7),
|
||||
],
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(
|
||||
AppSizes.paddingL,
|
||||
AppSizes.paddingL,
|
||||
AppSizes.paddingL,
|
||||
70, // Spazio per le tab (altezza standard tab bar)
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Icon(
|
||||
_getTipoIcon(),
|
||||
size: 32,
|
||||
color: _getTipoColor(),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
widget.sito.tipoReadable,
|
||||
style: TextStyle(
|
||||
color: Colors.white.withOpacity(0.9),
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
widget.sito.nome,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
bottom: TabBar(
|
||||
controller: _tabController,
|
||||
tabs: const [
|
||||
Tab(text: 'Informazioni', icon: Icon(Icons.info_outline)),
|
||||
Tab(text: 'Allarmi', icon: Icon(Icons.notifications_active)),
|
||||
],
|
||||
),
|
||||
),
|
||||
];
|
||||
},
|
||||
body: TabBarView(
|
||||
controller: _tabController,
|
||||
children: [
|
||||
// Tab Informazioni
|
||||
_buildInfoTab(dateFormat),
|
||||
|
||||
// Tab Allarmi
|
||||
_buildAllarmiTab(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoTab(DateFormat dateFormat) {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Statistiche rapide
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildStatCard(
|
||||
'Allarmi Totali',
|
||||
_allarmi.length.toString(),
|
||||
Icons.notifications,
|
||||
AppColors.primary,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: AppSizes.paddingM),
|
||||
Expanded(
|
||||
child: _buildStatCard(
|
||||
'Critici',
|
||||
_allarmiCritici.toString(),
|
||||
Icons.warning,
|
||||
AppColors.critical,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: AppSizes.paddingM),
|
||||
Expanded(
|
||||
child: _buildStatCard(
|
||||
'Aperti',
|
||||
_allarmiAperti.toString(),
|
||||
Icons.pending,
|
||||
AppColors.warning,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Descrizione
|
||||
if (widget.sito.descrizione != null) ...[
|
||||
const Text('Descrizione', style: AppTextStyles.h3),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
widget.sito.descrizione!,
|
||||
style: AppTextStyles.bodyLarge,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
|
||||
// Mappa
|
||||
if (widget.sito.hasCoordinates) ...[
|
||||
const Text('Posizione', style: AppTextStyles.h3),
|
||||
const SizedBox(height: 12),
|
||||
_buildMapView(),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
|
||||
// Dettagli tecnici
|
||||
const Text('Dettagli', style: AppTextStyles.h3),
|
||||
const SizedBox(height: 12),
|
||||
_buildInfoCard(
|
||||
icon: Icons.place,
|
||||
title: 'Località',
|
||||
value: widget.sito.localita,
|
||||
),
|
||||
if (widget.sito.regione != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
icon: Icons.map,
|
||||
title: 'Regione',
|
||||
value: widget.sito.regione!,
|
||||
),
|
||||
],
|
||||
if (widget.sito.codiceIdentificativo != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
icon: Icons.qr_code,
|
||||
title: 'Codice Identificativo',
|
||||
value: widget.sito.codiceIdentificativo!,
|
||||
),
|
||||
],
|
||||
if (widget.sito.hasCoordinates) ...[
|
||||
const SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
icon: Icons.my_location,
|
||||
title: 'Coordinate',
|
||||
value:
|
||||
'${widget.sito.latitudine!.toStringAsFixed(6)}, ${widget.sito.longitudine!.toStringAsFixed(6)}',
|
||||
),
|
||||
],
|
||||
if (widget.sito.altitudine != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
icon: Icons.height,
|
||||
title: 'Altitudine',
|
||||
value: '${widget.sito.altitudine!.toStringAsFixed(0)} m s.l.m.',
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 8),
|
||||
_buildInfoCard(
|
||||
icon: Icons.calendar_today,
|
||||
title: 'Data creazione',
|
||||
value: dateFormat.format(widget.sito.createdAt),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAllarmiTab() {
|
||||
if (_isLoadingAllarmi) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (_errorMessage != null) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.error_outline, size: 64, color: AppColors.critical),
|
||||
const SizedBox(height: 16),
|
||||
Text(_errorMessage!),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _loadAllarmi,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('Riprova'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (_allarmi.isEmpty) {
|
||||
return const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.check_circle_outline, size: 64, color: AppColors.success),
|
||||
SizedBox(height: 16),
|
||||
Text('Nessun allarme registrato', style: AppTextStyles.h3),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'Questo sito non ha allarmi nello storico',
|
||||
style: AppTextStyles.bodyMedium,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: _loadAllarmi,
|
||||
child: ListView.builder(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingM),
|
||||
itemCount: _allarmi.length,
|
||||
itemBuilder: (context, index) {
|
||||
return AllarmeCard(allarme: _allarmi[index]);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatCard(String label, String value, IconData icon, Color color) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: color.withOpacity(0.3)),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(icon, color: color, size: 24),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: color,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoCard({
|
||||
required IconData icon,
|
||||
required String title,
|
||||
required String value,
|
||||
}) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey[100],
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.grey[300]!),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, color: _getTipoColor(), size: 20),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
title,
|
||||
style: AppTextStyles.bodySmall.copyWith(
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
value,
|
||||
style: AppTextStyles.bodyMedium.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMapView() {
|
||||
if (!widget.sito.hasCoordinates) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
final position =
|
||||
LatLng(widget.sito.latitudine!, widget.sito.longitudine!);
|
||||
|
||||
return Container(
|
||||
height: 250,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.grey[300]!),
|
||||
),
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: GoogleMap(
|
||||
initialCameraPosition: CameraPosition(
|
||||
target: position,
|
||||
zoom: 14,
|
||||
),
|
||||
markers: {
|
||||
Marker(
|
||||
markerId: MarkerId('sito_${widget.sito.id}'),
|
||||
position: position,
|
||||
infoWindow: InfoWindow(
|
||||
title: widget.sito.nome,
|
||||
snippet: widget.sito.tipoReadable,
|
||||
),
|
||||
icon: BitmapDescriptor.defaultMarkerWithHue(
|
||||
_getTipoColor() == Colors.blue
|
||||
? BitmapDescriptor.hueBlue
|
||||
: _getTipoColor() == Colors.green
|
||||
? BitmapDescriptor.hueGreen
|
||||
: _getTipoColor() == Colors.orange
|
||||
? BitmapDescriptor.hueOrange
|
||||
: BitmapDescriptor.hueRed,
|
||||
),
|
||||
),
|
||||
},
|
||||
myLocationButtonEnabled: true,
|
||||
zoomControlsEnabled: true,
|
||||
mapToolbarEnabled: false,
|
||||
onMapCreated: (controller) {
|
||||
_mapController = controller;
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user