Files
barcode_reader_pc/app/lib/screens/history_screen.dart
T

193 lines
8.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../services/connection_service.dart';
class HistoryScreen extends StatefulWidget {
final ConnectionService connectionService;
const HistoryScreen({super.key, required this.connectionService});
@override
State<HistoryScreen> createState() => _HistoryScreenState();
}
class _HistoryScreenState extends State<HistoryScreen> {
String _searchQuery = '';
final TextEditingController _searchController = TextEditingController();
@override
void dispose() {
_searchController.dispose();
super.dispose();
}
void _confirmClear() {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Cancellare Cronologia?'),
content: const Text('Tutte le scansioni salvate sul dispositivo verranno eliminate definitivamente.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: const Text('Annulla'),
),
FilledButton(
style: FilledButton.styleFrom(backgroundColor: Colors.red),
onPressed: () {
widget.connectionService.clearHistory();
Navigator.pop(ctx);
},
child: const Text('Cancella Tutto'),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: widget.connectionService,
builder: (context, _) {
final allHistory = widget.connectionService.history;
final filtered = _searchQuery.isEmpty
? allHistory
: allHistory.where((item) => item.code.toLowerCase().contains(_searchQuery.toLowerCase())).toList();
return Scaffold(
appBar: AppBar(
title: const Text('Cronologia Scansioni'),
actions: [
if (allHistory.isNotEmpty)
IconButton(
icon: const Icon(Icons.delete_outline),
tooltip: 'Cancella cronologia',
onPressed: _confirmClear,
),
],
),
body: Column(
children: [
if (allHistory.isNotEmpty)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: TextField(
controller: _searchController,
decoration: InputDecoration(
hintText: 'Cerca tra i codici...',
prefixIcon: const Icon(Icons.search),
suffixIcon: _searchQuery.isNotEmpty
? IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
_searchController.clear();
setState(() {
_searchQuery = '';
});
},
)
: null,
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
),
onChanged: (val) {
setState(() {
_searchQuery = val.trim();
});
},
),
),
Expanded(
child: filtered.isEmpty
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
_searchQuery.isEmpty ? Icons.history_toggle_off : Icons.search_off,
size: 64,
color: Colors.grey.shade400,
),
const SizedBox(height: 16),
Text(
_searchQuery.isEmpty
? 'Nessuna scansione presente'
: 'Nessun risultato trovato per "$_searchQuery"',
style: const TextStyle(color: Colors.grey, fontSize: 16),
),
],
),
)
: ListView.separated(
padding: const EdgeInsets.all(16),
itemCount: filtered.length,
separatorBuilder: (context, index) => const SizedBox(height: 8),
itemBuilder: (context, index) {
final item = filtered[index];
return Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
child: ListTile(
leading: CircleAvatar(
backgroundColor: item.sentSuccessfully
? Colors.green.withValues(alpha: 0.15)
: Colors.orange.withValues(alpha: 0.15),
child: Icon(
item.sentSuccessfully ? Icons.check : Icons.warning_amber,
color: item.sentSuccessfully ? Colors.green : Colors.orange,
size: 20,
),
),
title: SelectableText(
item.code,
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
subtitle: Text(
'${item.format} • ${item.timestamp.hour.toString().padLeft(2, '0')}:${item.timestamp.minute.toString().padLeft(2, '0')}:${item.timestamp.second.toString().padLeft(2, '0')} - ${item.timestamp.day}/${item.timestamp.month}/${item.timestamp.year}',
style: const TextStyle(fontSize: 12, color: Colors.grey),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(Icons.copy, size: 20),
tooltip: 'Copia codice',
onPressed: () {
Clipboard.setData(ClipboardData(text: item.code));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Copiato negli appunti'),
duration: Duration(seconds: 1),
),
);
},
),
IconButton(
icon: const Icon(Icons.send, size: 20),
tooltip: 'Invia di nuovo al PC',
onPressed: () {
widget.connectionService.sendCode(item.code, format: item.format);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Inviato "${item.code}" al PC'),
duration: const Duration(seconds: 1),
),
);
},
),
],
),
),
);
},
),
),
],
),
);
},
);
}
}