1
0
mirror of https://github.com/nisrulz/flutter-examples.git synced 2025-11-08 12:39:17 +00:00

New Example - QR Code Scanner (#122)

Co-authored-by: Mridul <mridul@Mriduls-MacBook-Pro.local>
This commit is contained in:
Mridul Dhiman
2022-10-22 23:42:12 +05:30
committed by GitHub
parent fe71b47e3e
commit 1564ccdff2
67 changed files with 1639 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:scan_qr_code/utils/qr_code_scanner.dart';
import 'package:scan_qr_code/utils/scanner_box_border_painter.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Code Scanner',
home: MyHomePage(title: 'Code Scanner Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final MobileScannerController _mobileScannerController = MobileScannerController();
late Function(Barcode barcode, MobileScannerArguments? args) _onDetect;
bool _isScanning = true;
@override
void initState() {
super.initState();
_onDetect = (Barcode barcode, MobileScannerArguments? args) {
_mobileScannerController.stop();
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Code info'),
content: Text(barcode.rawValue?.trim() ?? 'No data found', textAlign: TextAlign.center),
actions: [TextButton(onPressed: () => Navigator.pop(context), child: const Text('OK'))],
);
}).then((value) {
_mobileScannerController.start();
setState(() {
_isScanning = true;
});
});
setState(() {
_isScanning = false;
});
};
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: SizedBox.square(
dimension: MediaQuery.of(context).size.width - 48.0,
child: Padding(
padding: const EdgeInsets.all(2.5), // Required otherwise side edges hides under outside padding
child: CustomPaint(
foregroundPainter: ScannerBoxBorderPainter(borderColor: _isScanning ? Colors.black : Colors.green),
child: Padding(
padding: const EdgeInsets.all(2.5), // Required otherwise scanner hides under side edges
child: QRCodeScanner(mobileScannerController: _mobileScannerController, allowDuplicates: false, onDetect: _onDetect),
),
),
),
),
),
);
}
}

View File

@@ -0,0 +1,18 @@
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
class QRCodeScanner extends StatelessWidget {
final MobileScannerController mobileScannerController;
final bool allowDuplicates;
final Function(Barcode barcode, MobileScannerArguments? args) onDetect;
const QRCodeScanner({Key? key, required this.mobileScannerController,required this.allowDuplicates, required this.onDetect}) : super(key: key);
@override
Widget build(BuildContext context) {
return MobileScanner(
allowDuplicates: allowDuplicates,
controller: mobileScannerController,
onDetect: onDetect,
);
}
}

View File

@@ -0,0 +1,43 @@
import 'package:flutter/material.dart';
class ScannerBoxBorderPainter extends CustomPainter {
final Color borderColor;
ScannerBoxBorderPainter({required this.borderColor});
@override
void paint(Canvas canvas, Size size) {
double sh = size.height; // for convenient shortage
double sw = size.width; // for convenient shortage
double cornerSide = 78.0; // desirable value for corners side
Paint paint = Paint()
..color = borderColor
..strokeWidth = 5
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.square;
Path path = Path()
..moveTo(cornerSide, 0)
..lineTo(0, 0)
..lineTo(0, cornerSide)
..moveTo(0, sh - cornerSide)
..lineTo(0, sh)
..lineTo(cornerSide, sh)
..moveTo(sw - cornerSide, sh)
..lineTo(sw, sh)
..lineTo(sw, sh - cornerSide)
..moveTo(sw, cornerSide)
..lineTo(sw, 0)
..lineTo(sw - cornerSide, 0)
..moveTo(cornerSide, 0);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(ScannerBoxBorderPainter oldDelegate) => false;
@override
bool shouldRebuildSemantics(ScannerBoxBorderPainter oldDelegate) => false;
}