feat(app): scan a printed seed label back into its record

Closes the physical↔digital loop the labels opened: the QR on a printed
envelope (tane://seed, already encoded by seed_label_codec) can now be
scanned from the inventory bar. A known label opens its variety; an
unknown one asks before adding anything, then creates the variety (species
linked by exact scientific name when bundled) with a lot carrying the
label's year and origin. Scanner is pure ZXing (zxing_barcode_scanner,
MIT) — no ML Kit, no Play Services, F-Droid-safe, the stack Ğ1nkgo ships.
Mobile-only; other platforms don't show the button (OCR precedent).
The post-scan flow is a plain function (handleScannedPayload) so it's
widget-tested without a camera.
This commit is contained in:
vjrj 2026-07-18 12:26:27 +02:00
parent bb5b3d4a43
commit f1aa8f8e66
18 changed files with 1130 additions and 42 deletions

View file

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import '../data/species_repository.dart';
import '../data/variety_repository.dart';
import '../db/enums.dart';
import '../di/injector.dart';
@ -14,6 +15,7 @@ import 'draft_triage.dart';
import 'edge_fade.dart';
import 'filter_chips.dart';
import 'label_print_sheet.dart';
import 'qr_scan.dart';
import 'quantity_kind_l10n.dart';
import 'quantity_picker.dart';
import 'quick_add_sheet.dart';
@ -133,6 +135,15 @@ class InventoryListScreen extends StatelessWidget {
tooltip: t.printLabels.action,
onPressed: context.read<InventoryCubit>().startSelection,
),
// Scan a printed seed label back into its record the physical
// envelope re-finds its digital thread. Mobile-only (camera).
if (qrScanSupported)
IconButton(
key: const Key('inventory.scanLabel'),
icon: const Icon(Icons.qr_code_scanner),
tooltip: t.scan.action,
onPressed: () => _scanLabel(context),
),
IconButton(
key: const Key('inventory.captureBurst'),
icon: const Icon(Icons.add_a_photo_outlined),
@ -233,6 +244,20 @@ class InventoryListScreen extends StatelessWidget {
return parts.isEmpty ? null : parts.join(' · ');
}
Future<void> _scanLabel(BuildContext context) async {
final repository = context.read<VarietyRepository>();
final species = context.read<SpeciesRepository>();
final payload = await scanSeedLabelQr(context);
if (payload == null || !context.mounted) return;
await handleScannedPayload(
context,
repository: repository,
species: species,
payload: payload,
openVariety: (id) => context.push('/variety/$id'),
);
}
Future<void> _captureBurst(BuildContext context) async {
final t = context.t;
final repository = context.read<VarietyRepository>();

View file

@ -0,0 +1,120 @@
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:zxing_barcode_scanner/zxing_barcode_scanner.dart';
import '../data/species_repository.dart';
import '../data/variety_repository.dart';
import '../i18n/strings.g.dart';
import '../services/seed_label_scan.dart';
/// Whether this platform can open the camera scanner. Mobile-only for now
/// (pure-ZXing platform views; no Google Play Services) elsewhere the scan
/// button simply isn't offered, same pattern as the OCR capture.
bool get qrScanSupported => !kIsWeb && (Platform.isAndroid || Platform.isIOS);
/// Opens the full-screen camera scanner and returns the first decoded QR
/// payload, or null if the person backed out. (Ğ1nkgo's scanner pattern on
/// the same pure-ZXing stack.)
Future<String?> scanSeedLabelQr(BuildContext context) {
return Navigator.of(context).push<String>(
MaterialPageRoute<String>(builder: (_) => const _ScannerScreen()),
);
}
/// Acts on a scanned payload: a known seed opens its record (via
/// [openVariety]); an unknown seed label asks before adding anything; anything
/// else says so and moves on. Navigation is injected so the flow is
/// widget-testable without a camera or a router.
Future<void> handleScannedPayload(
BuildContext context, {
required VarietyRepository repository,
required SpeciesRepository species,
required String payload,
required void Function(String varietyId) openVariety,
}) async {
final t = context.t;
final messenger = ScaffoldMessenger.of(context);
final result = await resolveScannedLabel(repository, payload);
if (!context.mounted) return;
if (result.varietyId case final id?) {
openVariety(id);
return;
}
final data = result.data;
if (data == null) {
messenger.showSnackBar(SnackBar(content: Text(t.scan.notALabel)));
return;
}
final add = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
key: const Key('scan.addPrompt'),
title: Text(t.scan.addTitle),
content: Text(t.scan.addBody(label: data.varietyLabel)),
actions: [
TextButton(
key: const Key('scan.cancel'),
onPressed: () => Navigator.of(context).pop(false),
child: Text(t.common.cancel),
),
FilledButton(
key: const Key('scan.add'),
onPressed: () => Navigator.of(context).pop(true),
child: Text(t.scan.add),
),
],
),
);
if (add != true) return;
final varietyId = await importScannedLabel(
repository: repository,
species: species,
data: data,
);
messenger.showSnackBar(SnackBar(content: Text(t.scan.added)));
openVariety(varietyId);
}
class _ScannerScreen extends StatefulWidget {
const _ScannerScreen();
@override
State<_ScannerScreen> createState() => _ScannerScreenState();
}
class _ScannerScreenState extends State<_ScannerScreen> {
bool _done = false;
void _onScan(List<BarcodeResult> results) {
if (_done || !mounted || results.isEmpty) return;
final text = results.first.text;
if (text == null || text.isEmpty) return;
_done = true;
// A beat so the camera view settles before popping (Ğ1nkgo does the same).
Future<void>.delayed(const Duration(milliseconds: 150), () {
if (mounted) Navigator.of(context).pop(text);
});
}
@override
Widget build(BuildContext context) {
final t = context.t;
return Scaffold(
appBar: AppBar(title: Text(t.scan.title)),
body: ZxingBarcodeScanner(
onScan: _onScan,
onError: (error) => Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Text(error.message ?? t.common.offline),
),
),
),
);
}
}