MakerBook/books_viewer/lib/pages/qr_code_scanner.dart

83 lines
2.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
class QRCodeScannerPage extends StatefulWidget {
const QRCodeScannerPage({Key? key}) : super(key: key);
@override
State<QRCodeScannerPage> createState() => _QRCodeScannerPageState();
}
class _QRCodeScannerPageState extends State<QRCodeScannerPage> {
bool lido = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Leia o QR Code')),
body: Stack(
children: [
// 📷 Câmera
MobileScanner(
fit: BoxFit.cover,
onDetect: (capture) {
if (lido) return;
final barcode = capture.barcodes.firstOrNull;
final value = barcode?.rawValue;
if (value == null) return;
lido = true;
debugPrint('[LOG] Leitura QRCode: $value');
Navigator.pop(context, value);
},
),
// 🕶️ Overlay com janela central
const Positioned.fill(
child: ScannerOverlay(),
),
],
),
);
}
}
class ScannerOverlay extends StatelessWidget {
const ScannerOverlay({super.key});
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
final width = constraints.maxWidth;
final height = constraints.maxHeight;
final scanAreaSize = width * 0.6;
final left = (width - scanAreaSize) / 2;
final top = (height - scanAreaSize) / 2;
return Stack(
children: [
// Camada escura
Container(color: Colors.black.withOpacity(0.5)),
// Janela central
Positioned(
left: left,
top: top,
width: scanAreaSize,
height: scanAreaSize,
child: Container(
decoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(color: Colors.white, width: 2),
borderRadius: BorderRadius.circular(16),
),
),
),
],
);
});
}
}