flutter_scankit 1.0.0 flutter_scankit: ^1.0.0 copied to clipboard
A scan code Flutter plugin, which is a Flutter package for huawei scanning SDK.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_easy_permission/constants.dart';
import 'package:flutter_easy_permission/easy_permissions.dart';
import 'package:flutter_scankit/flutter_scankit.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
const _permissions = const [
Permissions.READ_EXTERNAL_STORAGE,
Permissions.CAMERA
];
const _permissionGroup = const [
PermissionGroup.Camera,
PermissionGroup.Photos
];
class _MyAppState extends State<MyApp> {
FlutterScankit scanKit;
String code = "";
@override
void initState() {
super.initState();
scanKit = FlutterScankit();
scanKit.addResultListen((val) {
debugPrint("scanning result:$val");
setState(() {
code = val??"";
});
});
FlutterEasyPermission().addPermissionCallback(
onGranted: (requestCode, perms,perm) {
startScan();
},
onDenied: (requestCode, perms,perm, isPermanent) {});
}
@override
void dispose() {
scanKit.dispose();
super.dispose();
}
Future<void> startScan() async {
try {
await scanKit.startScan(scanTypes: [ScanTypes.ALL]);
} on PlatformException {}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(code),
SizedBox(height: 32,),
ElevatedButton(
child: Text("Scan code"),
onPressed: () async {
if (!await FlutterEasyPermission.has(perms: _permissions,permsGroup: _permissionGroup)) {
FlutterEasyPermission.request(perms: _permissions,permsGroup: _permissionGroup);
} else {
startScan();
}
},
)
],
),
),
),
);
}
}