136 lines
4.4 KiB
Dart
136 lines
4.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
import '../../theme/app_theme.dart';
|
|
|
|
class ScannerScreen extends StatefulWidget {
|
|
final String titulo;
|
|
const ScannerScreen({Key? key, this.titulo = 'Escanear código'}) : super(key: key);
|
|
|
|
@override
|
|
State<ScannerScreen> createState() => _ScannerScreenState();
|
|
}
|
|
|
|
class _ScannerScreenState extends State<ScannerScreen> {
|
|
final _controller = MobileScannerController();
|
|
bool _scanned = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _onDetect(BarcodeCapture capture) {
|
|
if (_scanned || capture.barcodes.isEmpty) return;
|
|
final rawValue = capture.barcodes.first.rawValue;
|
|
if (rawValue == null || rawValue.isEmpty) return;
|
|
_scanned = true;
|
|
Navigator.pop(context, rawValue);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.black,
|
|
appBar: AppBar(
|
|
backgroundColor: Colors.black,
|
|
foregroundColor: Colors.white,
|
|
elevation: 0,
|
|
title: Text(widget.titulo),
|
|
actions: [
|
|
ValueListenableBuilder<MobileScannerState>(
|
|
valueListenable: _controller,
|
|
builder: (_, state, __) => IconButton(
|
|
tooltip: state.torchState == TorchState.on ? 'Apagar linterna' : 'Encender linterna',
|
|
icon: Icon(
|
|
state.torchState == TorchState.on ? Icons.flash_on : Icons.flash_off,
|
|
color: state.torchState == TorchState.on ? Colors.yellow : Colors.white,
|
|
),
|
|
onPressed: _controller.toggleTorch,
|
|
),
|
|
),
|
|
IconButton(
|
|
tooltip: 'Cambiar cámara',
|
|
icon: const Icon(Icons.cameraswitch, color: Colors.white),
|
|
onPressed: () => _controller.switchCamera(),
|
|
),
|
|
],
|
|
),
|
|
body: Stack(
|
|
children: [
|
|
MobileScanner(
|
|
controller: _controller,
|
|
fit: BoxFit.cover,
|
|
onDetect: _onDetect,
|
|
),
|
|
CustomPaint(
|
|
painter: _ScanOverlayPainter(),
|
|
child: const SizedBox.expand(),
|
|
),
|
|
Positioned(
|
|
left: 0,
|
|
right: 0,
|
|
bottom: 56,
|
|
child: Column(
|
|
children: const [
|
|
Icon(Icons.qr_code_scanner, color: Colors.white38, size: 28),
|
|
SizedBox(height: 10),
|
|
Text(
|
|
'Apunta al código QR o código de barras',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(color: Colors.white70, fontSize: 13),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ScanOverlayPainter extends CustomPainter {
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
final frameSize = size.width * 0.72;
|
|
final left = (size.width - frameSize) / 2;
|
|
final top = (size.height - frameSize) / 2 - 40;
|
|
final frameRect = Rect.fromLTWH(left, top, frameSize, frameSize);
|
|
|
|
// Semi-transparent overlay with hole
|
|
final overlayPaint = Paint()..color = Colors.black.withOpacity(0.55);
|
|
final outer = Path()..addRect(Rect.fromLTWH(0, 0, size.width, size.height));
|
|
final inner = Path()..addRRect(RRect.fromRectAndRadius(frameRect, const Radius.circular(10)));
|
|
canvas.drawPath(Path.combine(PathOperation.difference, outer, inner), overlayPaint);
|
|
|
|
// Corner brackets
|
|
final bracketPaint = Paint()
|
|
..color = AppTheme.primaryColor
|
|
..style = PaintingStyle.stroke
|
|
..strokeWidth = 3.5
|
|
..strokeCap = StrokeCap.round;
|
|
|
|
const cl = 22.0; // corner length
|
|
final l = frameRect.left;
|
|
final t = frameRect.top;
|
|
final r = frameRect.right;
|
|
final b = frameRect.bottom;
|
|
|
|
// Top-left
|
|
canvas.drawLine(Offset(l, t + cl), Offset(l, t), bracketPaint);
|
|
canvas.drawLine(Offset(l, t), Offset(l + cl, t), bracketPaint);
|
|
// Top-right
|
|
canvas.drawLine(Offset(r - cl, t), Offset(r, t), bracketPaint);
|
|
canvas.drawLine(Offset(r, t), Offset(r, t + cl), bracketPaint);
|
|
// Bottom-left
|
|
canvas.drawLine(Offset(l, b - cl), Offset(l, b), bracketPaint);
|
|
canvas.drawLine(Offset(l, b), Offset(l + cl, b), bracketPaint);
|
|
// Bottom-right
|
|
canvas.drawLine(Offset(r - cl, b), Offset(r, b), bracketPaint);
|
|
canvas.drawLine(Offset(r, b), Offset(r, b - cl), bracketPaint);
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(_) => false;
|
|
}
|