Flutter 3.X二维码扫描功能
1. pubspec.yaml文件添加依赖
flutter_qr_bar_scanner: ^3.0.2
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
flutter_qr_bar_scanner: ^3.0.2
2使用
import 'package:flutter/material.dart';
import 'package:flutter_qr_bar_scanner/qr_bar_scanner_camera.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter QR/Bar Code Reader',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter QR/Bar Code Reader'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String? _qrInfo = 'Scan a QR/Bar code';
bool _camState = false;
_qrCallback(String? code) {
setState(() {
_camState = false;
_qrInfo = code;
});
}
_scanCode() {
setState(() {
_camState = true;
});
}
@override
void initState() {
super.initState();
_scanCode();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title!),
),
body: _camState
? Center(
child: SizedBox(
height: 1000,
width: 500,
child: QRBarScannerCamera(
onError: (context, error) => Text(
error.toString(),
style: TextStyle(color: Colors.red),
),
qrCodeCallback: (code) {
_qrCallback(code);
},
),
),
)
: Center(
child: Text(_qrInfo!),
),
);
}
}
3. 源代码
4.第二种方式
4.1 pubspec.yaml文件添加依赖
flutter_hms_scan_kit: ^1.0.5
4.2 使用
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hms_scan_kit/flutter_hms_scan_kit.dart';
import 'package:flutter_hms_scan_kit/scan_result.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
ScanResult? _scanResult;
List<int>? _code;
void initState() {
super.initState();
initPlatformState();
generateCode();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await FlutterHmsScanKit.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
Future<void> scan() async {
_scanResult = await FlutterHmsScanKit.scan;
setState(() {});
}
Future<void> generateCode() async {
var bytes = await rootBundle.load("assets/images/ic_logo.png");
_code = await FlutterHmsScanKit.generateCode(
content: "这是条码",
type: ScanTypeFormat.QRCODE_SCAN_TYPE,
width: 300,
height: 300,
color: "#7CB342",
logo: bytes.buffer.asUint8List(),
);
setState(() {});
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter华为扫码'),
),
body: Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 50),
child: SingleChildScrollView(
child: Column(
children: [
Text('类型: ${_scanResult?.scanTypeForm}\n'),
Text('内容类型: ${_scanResult?.scanType}\n'),
Text('扫码内容: ${_scanResult?.value}\n'),
ElevatedButton(
onPressed: scan,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 100),
child: Text("扫描"),
),
),
SizedBox(height: 50),
Text('生成条码\n'),
if (_code != null)
Image.memory(Uint8List.fromList(_code!))
else
Container(height: 300, color: Colors.transparent),
ElevatedButton(
onPressed: generateCode,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 100),
child: Text("生成条码"),
),
),
],
),
),
),
),
);
}
}