351 lines
12 KiB
Dart
351 lines
12 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
|
|
import '../services/connection_service.dart';
|
|
import '../services/discovery_service.dart';
|
|
|
|
class ConnectScreen extends StatefulWidget {
|
|
final ConnectionService connectionService;
|
|
|
|
const ConnectScreen({super.key, required this.connectionService});
|
|
|
|
@override
|
|
State<ConnectScreen> createState() => _ConnectScreenState();
|
|
}
|
|
|
|
class _ConnectScreenState extends State<ConnectScreen> {
|
|
late TextEditingController _ipController;
|
|
late TextEditingController _portController;
|
|
late TextEditingController _pinController;
|
|
|
|
bool _isSearching = false;
|
|
List<DiscoveredServer> _discoveredServers = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_ipController = TextEditingController(text: widget.connectionService.ip);
|
|
_portController = TextEditingController(text: widget.connectionService.port.toString());
|
|
_pinController = TextEditingController(text: widget.connectionService.pin);
|
|
|
|
// Avvia automaticamente una ricerca rapida su Wi-Fi all'apertura della schermata
|
|
_searchWifiServers();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_ipController.dispose();
|
|
_portController.dispose();
|
|
_pinController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _searchWifiServers() async {
|
|
setState(() {
|
|
_isSearching = true;
|
|
});
|
|
|
|
try {
|
|
final results = await DiscoveryService.discover(timeout: const Duration(seconds: 2));
|
|
if (mounted) {
|
|
setState(() {
|
|
_discoveredServers = results;
|
|
_isSearching = false;
|
|
});
|
|
}
|
|
} catch (_) {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isSearching = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void _scanServerQrCode() {
|
|
showModalBottomSheet(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
builder: (ctx) {
|
|
return SizedBox(
|
|
height: MediaQuery.of(ctx).size.height * 0.7,
|
|
child: Column(
|
|
children: [
|
|
AppBar(
|
|
title: const Text('Inquadra QR del PC'),
|
|
leading: IconButton(
|
|
icon: const Icon(Icons.close),
|
|
onPressed: () => Navigator.pop(ctx),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: MobileScanner(
|
|
onDetect: (capture) {
|
|
final barcode = capture.barcodes.firstOrNull;
|
|
if (barcode?.rawValue == null) return;
|
|
final val = barcode!.rawValue!;
|
|
|
|
try {
|
|
// Prova a parsare come JSON pairing
|
|
final json = jsonDecode(val) as Map<String, dynamic>;
|
|
final ip = json['ip'] as String?;
|
|
final port = json['port'] as int? ?? 8765;
|
|
final pin = json['pin'] as String? ?? '';
|
|
final name = json['name'] as String? ?? 'PC Server';
|
|
|
|
if (ip != null && ip.isNotEmpty) {
|
|
Navigator.pop(ctx);
|
|
_applyAndConnect(ip, port, pin, name);
|
|
return;
|
|
}
|
|
} catch (_) {
|
|
// Se è un URL come ws://192.168.1.50:8765/ws
|
|
final uri = Uri.tryParse(val);
|
|
if (uri != null && uri.host.isNotEmpty) {
|
|
Navigator.pop(ctx);
|
|
_applyAndConnect(uri.host, uri.port != 0 ? uri.port : 8765, '', 'PC');
|
|
return;
|
|
}
|
|
}
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
void _applyAndConnect(String ip, int port, String pin, String? name) {
|
|
widget.connectionService.setServer(
|
|
ip: ip,
|
|
port: port,
|
|
pin: pin,
|
|
serverName: name,
|
|
);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Connessione a $ip:$port...'),
|
|
backgroundColor: Colors.blueAccent,
|
|
),
|
|
);
|
|
Navigator.pop(context);
|
|
}
|
|
|
|
void _onManualConnect() {
|
|
final ip = _ipController.text.trim();
|
|
final port = int.tryParse(_portController.text.trim()) ?? 8765;
|
|
final pin = _pinController.text.trim();
|
|
|
|
if (ip.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Inserisci un indirizzo IP valido.')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
_applyAndConnect(ip, port, pin, null);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final status = widget.connectionService.status;
|
|
final isConnected = status == ConnectionStatus.connected;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Connessione al PC'),
|
|
),
|
|
body: ListView(
|
|
padding: const EdgeInsets.all(16),
|
|
children: [
|
|
// Stato attuale
|
|
Card(
|
|
color: isConnected
|
|
? Colors.green.withValues(alpha: 0.15)
|
|
: Theme.of(context).colorScheme.surfaceContainerHighest,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Row(
|
|
children: [
|
|
Icon(
|
|
isConnected ? Icons.check_circle : Icons.portable_wifi_off,
|
|
color: isConnected ? Colors.green : Colors.grey,
|
|
size: 32,
|
|
),
|
|
const SizedBox(width: 16),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
isConnected ? 'Connesso' : 'Non Connesso',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
color: isConnected ? Colors.green : null,
|
|
),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
widget.connectionService.statusMessage,
|
|
style: const TextStyle(fontSize: 13, color: Colors.grey),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
if (isConnected)
|
|
TextButton(
|
|
onPressed: () => widget.connectionService.disconnect(),
|
|
child: const Text('Disconnetti'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
// Metodo 1: Inquadra QR
|
|
FilledButton.icon(
|
|
style: FilledButton.styleFrom(
|
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
|
),
|
|
icon: const Icon(Icons.qr_code_scanner, size: 24),
|
|
label: const Text('Inquadra QR Code sul PC', style: TextStyle(fontSize: 16)),
|
|
onPressed: _scanServerQrCode,
|
|
),
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
// Metodo 2: Rilevamento automatico su Wi-Fi
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
const Text(
|
|
'PC Rilevati sulla Rete Wi-Fi',
|
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
),
|
|
IconButton(
|
|
icon: _isSearching
|
|
? const SizedBox(
|
|
width: 18,
|
|
height: 18,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(Icons.refresh),
|
|
tooltip: 'Aggiorna ricerca Wi-Fi',
|
|
onPressed: _isSearching ? null : _searchWifiServers,
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 8),
|
|
|
|
if (_discoveredServers.isEmpty)
|
|
Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: Colors.grey.withValues(alpha: 0.3)),
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
child: Text(
|
|
_isSearching
|
|
? 'Ricerca PC in corso sulla rete locale...'
|
|
: 'Nessun PC rilevato automaticamente. Assicurati che il server sia avviato e collegato allo stesso Wi-Fi, oppure inquadra il QR code.',
|
|
style: const TextStyle(color: Colors.grey, fontSize: 13),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
)
|
|
else
|
|
..._discoveredServers.map((srv) {
|
|
return Card(
|
|
margin: const EdgeInsets.only(bottom: 8),
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
|
child: ListTile(
|
|
leading: const CircleAvatar(
|
|
child: Icon(Icons.computer),
|
|
),
|
|
title: Text(srv.serverName, style: const TextStyle(fontWeight: FontWeight.bold)),
|
|
subtitle: Text('${srv.ip}:${srv.port} ${srv.pinRequired ? "• Richiede PIN" : ""}'),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: () {
|
|
_applyAndConnect(srv.ip, srv.port, '', srv.serverName);
|
|
},
|
|
),
|
|
);
|
|
}),
|
|
|
|
const SizedBox(height: 28),
|
|
|
|
// Metodo 3: Configurazione manuale
|
|
const Text(
|
|
'Configurazione Manuale',
|
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
),
|
|
const SizedBox(height: 12),
|
|
|
|
TextField(
|
|
controller: _ipController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Indirizzo IP del PC',
|
|
hintText: 'Es. 192.168.1.50',
|
|
prefixIcon: Icon(Icons.lan),
|
|
border: OutlineInputBorder(),
|
|
),
|
|
keyboardType: TextInputType.url,
|
|
),
|
|
const SizedBox(height: 12),
|
|
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
flex: 2,
|
|
child: TextField(
|
|
controller: _portController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Porta',
|
|
hintText: '8765',
|
|
prefixIcon: Icon(Icons.tag),
|
|
border: OutlineInputBorder(),
|
|
),
|
|
keyboardType: TextInputType.number,
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
flex: 3,
|
|
child: TextField(
|
|
controller: _pinController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'PIN (opzionale)',
|
|
hintText: 'Codice di sicurezza',
|
|
prefixIcon: Icon(Icons.lock_outline),
|
|
border: OutlineInputBorder(),
|
|
),
|
|
keyboardType: TextInputType.number,
|
|
obscureText: true,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
OutlinedButton.icon(
|
|
style: OutlinedButton.styleFrom(
|
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
|
),
|
|
icon: const Icon(Icons.save),
|
|
label: const Text('Salva e Connetti Manualmente'),
|
|
onPressed: _onManualConnect,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|