lono_scan 1.0.0+2
lono_scan: ^1.0.0+2 copied to clipboard
Scan qrcode & barcode in widget tree with custom options. Accurately decode qrcode & barcode image from path.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:lono_scan/lono_scan.dart';
import 'package:scan_example/lono_scan.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String qrcode = '';
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion = await LonoScan.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.orange,
centerTitle: true,
title: const Text('Lono scan example app'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Running on: $_platformVersion',
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.w500),
),
const SizedBox(height: 20),
Row(
children: [
_buildActionButton(
label: 'Scan from Image',
icon: Icons.image,
onPressed: () async {
ImagePicker picker = ImagePicker();
final XFile? res = await picker.pickImage(
source: ImageSource.gallery,
);
if (res != null) {
String? str = await LonoScan.parse(res.path);
if (str != null) {
setState(() {
qrcode = str;
});
}
else {
setState(() {
qrcode = 'Null';
});
}
}
},
),
const SizedBox(width: 16),
_buildActionButton(
label: 'Scan Page',
icon: Icons.qr_code_scanner,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LonoScanPage()),
);
},
),
],
),
const SizedBox(height: 30),
Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.orange.shade50,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.orange.shade300),
),
child: Column(
children: [
Text(
'Scan result:',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Colors.orange.shade700,
),
),
const SizedBox(height: 8),
Text(
qrcode.isNotEmpty ? qrcode : 'No result',
style: TextStyle(
fontSize: 16, color: Colors.black87),
textAlign: TextAlign.center,
),
],
),
),
],
),
),
),
),
},
);
}
Widget _buildActionButton({
required String label,
required IconData icon,
required VoidCallback onPressed,
}) {
return Expanded(
child: ElevatedButton.icon(
onPressed: onPressed,
icon: Icon(
icon,
size: 20,
color: Colors.white,
),
label: Text(label),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
elevation: 4,
),
),
);
}
}