Flutter二维码生成与扫描插件vietqr_flutter的使用
Flutter二维码生成与扫描插件vietqr_flutter的使用
Generator VietQR not using api.
此插件允许你生成符合VietQR标准的二维码,而无需调用API。你可以参考以下文档了解详细信息: VietQR Doc
Getting Started
基本使用方法,获取二维码:
String qrCode = VietQRGenerator.generate(
accountNumber: 'KFI9482626',
bankCode: '0006970441',
);
生成二维码图像的方法如下:
generatorQR(
vietQr: code,
image: const AssetImage('assets/images/bank.png'),
sizeQr: 300,
sizeEmbeddingImage: 50);
Demo QR:
Commemoration
该插件最初由 Anh Tuan 编写。
完整示例代码
import 'package:flutter/material.dart';
import 'package:vietqr_flutter/vietqr_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// 这个小部件是你的应用的根。
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo VietQR'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late String code = "";
[@override](/user/override)
void initState() {
code = _genQR(); // 初始化时生成二维码
super.initState();
}
String _genQR() {
String qrCode = VietQRGenerator.generate(
accountNumber: 'KFI9482626', // 账号
bankCode: '0006970441', // 银行代码
);
print(qrCode); // 打印生成的二维码字符串
return qrCode;
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Text(code), // 显示二维码字符串(可选)
Center(
child: generatorQR(
vietQr: code, // 传入生成的二维码字符串
image: const AssetImage('assets/images/bank.png'), // 嵌入的银行图标
sizeQr: 300, // 二维码大小
sizeEmbeddingImage: 50, // 嵌入图片大小
),
)
],
),
),
);
}
}
更多关于Flutter二维码生成与扫描插件vietqr_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter二维码生成与扫描插件vietqr_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用vietqr_flutter
插件来生成和扫描二维码的代码示例。
1. 添加依赖
首先,在你的pubspec.yaml
文件中添加vietqr_flutter
依赖:
dependencies:
flutter:
sdk: flutter
vietqr_flutter: ^x.y.z # 请替换为最新版本号
然后运行flutter pub get
来安装依赖。
2. 生成二维码
创建一个页面来生成二维码。这里是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:vietqr_flutter/vietqr_flutter.dart';
class QRCodeGeneratorPage extends StatefulWidget {
@override
_QRCodeGeneratorPageState createState() => _QRCodeGeneratorPageState();
}
class _QRCodeGeneratorPageState extends State<QRCodeGeneratorPage> {
final TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('生成二维码'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextField(
controller: _controller,
decoration: InputDecoration(
labelText: '输入数据',
),
),
SizedBox(height: 16),
ElevatedButton(
onPressed: () {
generateQRCode(_controller.text);
},
child: Text('生成二维码'),
),
],
),
),
);
}
void generateQRCode(String data) {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: QrImage(
data: data,
version: QrVersions.auto,
size: 200.0,
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text('关闭'),
),
],
),
);
}
}
3. 扫描二维码
接下来,创建一个页面来扫描二维码。这里是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:vietqr_flutter/vietqr_flutter.dart';
class QRCodeScannerPage extends StatefulWidget {
@override
_QRCodeScannerPageState createState() => _QRCodeScannerPageState();
}
class _QRCodeScannerPageState extends State<QRCodeScannerPage> {
BarcodeResult? _result;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('扫描二维码'),
),
body: Column(
children: <Widget>[
Expanded(
flex: 5,
child: QRView(
key: GlobalKey(debugLabel: 'QRView'),
onQRViewCreated: _onQRViewCreated,
),
),
Expanded(
flex: 1,
child: _result != null
? Text('扫描结果: ${_result!.code}')
: Container(),
),
],
),
);
}
void _onQRViewCreated(QRViewController controller) {
controller.scannedDataStream.listen((scanData) {
setState(() {
_result = scanData;
});
});
}
}
4. 注册页面路由
在你的main.dart
文件中注册这两个页面的路由:
import 'package:flutter/material.dart';
import 'qr_code_generator_page.dart';
import 'qr_code_scanner_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter QR Code Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter QR Code Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => QRCodeGeneratorPage()),
),
child: Text('生成二维码'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => QRCodeScannerPage()),
),
child: Text('扫描二维码'),
),
],
),
),
);
}
}
注意事项
- 权限处理:在Android和iOS上扫描二维码需要相机权限,请确保在
AndroidManifest.xml
和Info.plist
中正确配置相机权限。 - 错误处理:为了简洁,示例代码中没有包含详细的错误处理逻辑。在实际应用中,请添加必要的错误处理来增强应用的健壮性。
这样,你就可以在Flutter应用中使用vietqr_flutter
插件来生成和扫描二维码了。