49e7bcdbfa
buildQrPayload e parseMealPlanFromQr erano metodi privati non testabili. Estratti in un modulo dedicato e importati da qr_share_sheet e qr_scan_page. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
87 lines
2.3 KiB
Dart
87 lines
2.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../../providers/meal_planner_provider.dart';
|
|
import '../../qr_codec.dart';
|
|
|
|
class QrScanPage extends StatefulWidget {
|
|
const QrScanPage({super.key});
|
|
|
|
@override
|
|
State<QrScanPage> createState() => _QrScanPageState();
|
|
}
|
|
|
|
class _QrScanPageState extends State<QrScanPage> {
|
|
bool _handled = false;
|
|
|
|
void _onDetect(BarcodeCapture capture, BuildContext context) {
|
|
if (_handled) return;
|
|
final raw = capture.barcodes.firstOrNull?.rawValue;
|
|
if (raw == null) return;
|
|
|
|
setState(() => _handled = true);
|
|
|
|
final (plan, error) = parseMealPlanFromQr(raw);
|
|
if (error != null) {
|
|
_showError(context, error);
|
|
return;
|
|
}
|
|
|
|
context.read<MealPlannerProvider>().importPlan(plan!);
|
|
|
|
if (context.mounted) {
|
|
Navigator.pop(context);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Piano ricevuto!')),
|
|
);
|
|
}
|
|
}
|
|
|
|
void _showError(BuildContext context, String msg) {
|
|
if (!context.mounted) return;
|
|
Navigator.pop(context);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(msg),
|
|
backgroundColor: Theme.of(context).colorScheme.error,
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final scheme = Theme.of(context).colorScheme;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Scansiona QR')),
|
|
body: Stack(
|
|
children: [
|
|
MobileScanner(
|
|
onDetect: (capture) => _onDetect(capture, context),
|
|
),
|
|
// cornice di mira
|
|
Center(
|
|
child: Container(
|
|
width: 220,
|
|
height: 220,
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: scheme.primary, width: 3),
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
),
|
|
),
|
|
Positioned(
|
|
bottom: 40,
|
|
left: 0, right: 0,
|
|
child: Text(
|
|
'Inquadra il codice QR dell\'altro dispositivo',
|
|
textAlign: TextAlign.center,
|
|
style: const TextStyle(color: Colors.white, fontSize: 14),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|