flutter_qr_scanner_widget 0.0.1
flutter_qr_scanner_widget: ^0.0.1 copied to clipboard
A QR Scanner widget for Flutter
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_qr_scanner_widget/flutter_qr_scanner_widget.dart';
import 'package:permission_handler/permission_handler.dart';
void main() => runApp(const MaterialApp(home: FlutterQrScannerExample()));
class FlutterQrScannerExample extends StatefulWidget {
const FlutterQrScannerExample({Key? key}) : super(key: key);
@override
State<FlutterQrScannerExample> createState() =>
_FlutterQrScannerExampleState();
}
class _FlutterQrScannerExampleState extends State<FlutterQrScannerExample>
with WidgetsBindingObserver {
FlutterQrScannerController? _flutterQrScannerController;
bool isCameraPermissionGranted = false;
String scannedText = 'Some Scanned Text';
bool isFlashOn = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_checkCameraPermission();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
// Resume the scanner
_flutterQrScannerController?.resumeCamera();
} else if (state == AppLifecycleState.paused) {
// Pause the scanner
_flutterQrScannerController?.pauseCamera();
}
}
void _checkCameraPermission() async {
var status = await Permission.camera.status;
if (!status.isGranted) {
// We don't have permission yet.
isCameraPermissionGranted = false;
} else {
// We have permission. Update the UI if necessary.
setState(() {
isCameraPermissionGranted = true;
});
}
}
void _requestPermission() async {
final result = await Permission.camera.request();
setState(() {
isCameraPermissionGranted = result.isGranted;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flutter Scan QR example')),
body: SizedBox(
width: double.infinity,
height: double.infinity,
child: isCameraPermissionGranted
? Stack(
children: [
SizedBox.expand(
child: FlutterQrScanner(
onMapViewCreated: _onMapViewCreated,
),
),
Positioned.fill(
top: 40,
child: Align(
alignment: Alignment.topCenter,
child: Text(
scannedText,
style: const TextStyle(
color: Colors.white,
fontSize: 32,
),
),
),
),
Positioned.fill(
bottom: 40,
child: Align(
alignment: Alignment.bottomCenter,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Toggle Flash Icon
InkWell(
onTap: () {
_flutterQrScannerController?.toggleFlash();
setState(() {
isFlashOn = !isFlashOn;
});
},
child: Container(
width: 60,
height: 60,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
),
child: Icon(
isFlashOn ? Icons.flash_on : Icons.flash_off,
size: 36,
color: Colors.black,
),
),
),
const SizedBox(
width: 60,
),
// Switch camera icon
InkWell(
onTap: () {
_flutterQrScannerController?.toggleCamera();
},
child: Container(
width: 60,
height: 60,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
),
child: const Icon(
Icons.cameraswitch,
size: 36,
color: Colors.black,
),
),
),
],
),
),
),
],
)
: SizedBox.expand(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Camera permission is required to proceed.'),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _requestPermission,
child: const Text('Request Permission'),
),
],
),
),
),
),
);
}
// load default
void _onMapViewCreated(FlutterQrScannerController controller) {
_flutterQrScannerController = controller;
_flutterQrScannerController!.setOnResultReceivedCallback((scannedValue) {
setState(() {
scannedText = scannedValue;
});
});
}
}