mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-08-24 16:50:47 +00:00
Convert 17 multi-file apps to the validated pattern: lib/main.dart keeps only the MaterialApp bootstrap; a new lib/example.dart holds the full entry screen and focused example code, importing existing supporting files (screens/, tabs/, services/, models/, widgets/, utils/). Apps: analytics_integration, animation_example, biometrics, bottom_sheet, custom_home_drawer, google_signin, grid_layout, handling_routes, image_editor, scan_qr_code, statless_counter_app, using_bottom_nav_bar, using_custom_fonts, using_listview, using_listwheelscrollview, using_platform_adaptive, view_pdf_file
81 lines
2.4 KiB
Dart
81 lines
2.4 KiB
Dart
// Example: Scan QR code
|
|
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';
|
|
|
|
class Example extends StatefulWidget {
|
|
const Example({super.key, this.title = 'Code Scanner Demo'});
|
|
|
|
final String title;
|
|
|
|
@override
|
|
State<Example> createState() => _ExampleState();
|
|
}
|
|
|
|
class _ExampleState extends State<Example> {
|
|
final MobileScannerController _mobileScannerController =
|
|
MobileScannerController();
|
|
late void Function(BarcodeCapture) _onDetect;
|
|
bool _isScanning = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_onDetect = (BarcodeCapture capture) {
|
|
_mobileScannerController.stop();
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: const Text('Code info'),
|
|
content: Text(
|
|
capture.barcodes.first.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,
|
|
onDetect: _onDetect),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|