Files
biteplan/lib/features/meal_planner/presentation/pages/qr_scan_page.dart
T

87 lines
2.3 KiB
Dart
Raw Normal View History

2026-05-17 23:09:16 +02:00
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';
2026-05-17 23:09:16 +02:00
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;
}
2026-05-17 23:09:16 +02:00
context.read<MealPlannerProvider>().importPlan(plan!);
2026-05-17 23:09:16 +02:00
if (context.mounted) {
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Piano ricevuto!')),
);
2026-05-17 23:09:16 +02:00
}
}
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),
),
),
],
),
);
}
}