Files
eatvio_app/lib/features/diary/presentation/pages/barcode_scanner_screen.dart

88 lines
2.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
class BarcodeScannerScreen extends StatefulWidget {
const BarcodeScannerScreen({super.key});
@override
State<BarcodeScannerScreen> createState() => _BarcodeScannerScreenState();
}
class _BarcodeScannerScreenState extends State<BarcodeScannerScreen> {
final MobileScannerController _controller = MobileScannerController();
String? _detectedBarcode;
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Сканирование штрихкода'),
centerTitle: true,
),
body: Column(
children: [
Expanded(
child: MobileScanner(
controller: _controller,
onDetect: (capture) {
final List<Barcode> barcodes = capture.barcodes;
if (barcodes.isEmpty) return;
final rawValue = barcodes.first.rawValue;
if (rawValue == null || rawValue == _detectedBarcode) return;
setState(() {
_detectedBarcode = rawValue;
});
},
),
),
Container(
width: double.infinity,
padding: const EdgeInsets.all(16),
decoration: const BoxDecoration(
color: Color(0xFFF6F6F6),
borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Найденный штрихкод',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
),
const SizedBox(height: 8),
Text(
_detectedBarcode ?? 'Наведите камеру на штрихкод',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: _detectedBarcode == null ? Colors.grey : Colors.black,
),
),
const SizedBox(height: 16),
if (_detectedBarcode != null)
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
Navigator.of(context).pop(_detectedBarcode);
},
child: const Text('Использовать штрихкод'),
),
),
],
),
),
],
),
);
}
}