import 'package:flutter/material.dart';
import 'package:scan/scan.dart'; // 导入 scan 插件
import 'package:permission_handler/permission_handler.dart'; // 导入权限请求插件
import 'package:image_picker/image_picker.dart'; // 导入相册选择插件
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: QRScannerScreen(),
);
}
}
class QRScannerScreen extends StatefulWidget {
@override
_QRScannerScreenState createState() => _QRScannerScreenState();
}
class _QRScannerScreenState extends State<QRScannerScreen> {
ScanController controller = ScanController(); // 创建扫描控制器
String qrcode = 'Unknown'; // 默认二维码内容
// 执行二维码扫描前请求权限
_requestPermissions() async {
var cameraStatus = await Permission.camera.request();
if (cameraStatus.isGranted) {
// 权限已授予,开始扫描
print("相机权限已授予,开始扫描");
// 点击按钮后跳转到全屏扫描界面
Navigator.push(
context,
MaterialPageRoute(builder: (context) => QRScanPage(controller: controller)),
).then((result) {
controller.pause(); // 暂停扫描
// 扫描完成后获取返回的二维码数据并更新显示
if (result != null) {
setState(() {
qrcode = result; // 更新二维码内容
});
}
});
} else {
// 权限被拒绝或未授权
print("相机权限未授予,请授权");
// 可以引导用户去设置页面
}
}
// 选择相册中的二维码图片
_selectImageFromGallery() async {
final picker = ImagePicker();
final pickedFile = await picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
String result = await Scan.parse(pickedFile.path) ?? ''; // 解析选中的图片
setState(() {
qrcode = result; // 更新二维码内容
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('二维码扫描器'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// 显示二维码扫描结果
const Text(
'二维码扫描结果:',
style: TextStyle(fontSize: 18),
),
const SizedBox(height: 10),
Text(
qrcode,
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 30),
// 扫描按钮
ElevatedButton(
onPressed: _requestPermissions, // 执行权限请求
child: const Text('请求权限并开始扫描'),
),
const SizedBox(height: 30),
// 选择相册按钮
ElevatedButton(
onPressed: _selectImageFromGallery, // 选择相册
child: const Text('从相册选择二维码图片'),
),
],
),
),
);
}
}
class QRScanPage extends StatelessWidget {
final ScanController controller;
const QRScanPage({super.key, required this.controller});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('扫描二维码')),
body: Stack(
alignment: Alignment.center,
children: [
// 使用 ScanView 进行全屏扫描
ScanView(
controller: controller,
scanAreaScale: 0.8, // 设置扫描区域占满整个屏幕
scanLineColor: Colors.green.shade400, // 设置扫描线颜色
onCapture: (data) {
// 扫描到二维码后,返回数据
controller.pause(); // 暂停扫描
Navigator.pop(context, data); // 返回扫描结果
},
),
// 在屏幕上添加选择相册按钮
Positioned(
bottom: 100,
child: ElevatedButton(
onPressed: () async {
// 选择相册
final picker = ImagePicker();
final pickedFile = await picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
String result = await Scan.parse(pickedFile.path) ?? ''; // 解析选中的图片
Navigator.pop(context, result); // 返回二维码结果
}
},
child: const Text(
'选择相册',
style: TextStyle(
fontSize: 18,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
],
),
);
}
}
|