145 lines
3.7 KiB
Dart
145 lines
3.7 KiB
Dart
import 'package:chat/configs/app_colors.dart';
|
|
import 'package:chat/routes/user_routes.dart';
|
|
import 'package:chat/utils/ui_tools.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:scan/scan.dart';
|
|
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
|
|
|
|
class ScanPage extends StatefulWidget {
|
|
const ScanPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_ScanPageState createState() => _ScanPageState();
|
|
}
|
|
|
|
class _ScanPageState extends State<ScanPage> {
|
|
final ScanController _scanController = ScanController();
|
|
|
|
bool flashState = false;
|
|
|
|
/// 统一处理扫码结果,可能是扫码的,也可能是相册的
|
|
void parseScanResult(String str) {
|
|
List<String> split = str.split('|');
|
|
|
|
switch (split.first) {
|
|
case 'TRANSFER':
|
|
// 处理扫码转账
|
|
Get.back(result: split.last);
|
|
break;
|
|
case 'BEFRIEND':
|
|
// 加好友
|
|
break;
|
|
case 'JOINGROUP':
|
|
// 加群
|
|
break;
|
|
default:
|
|
UiTools.toast(str);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Stack(
|
|
children: [
|
|
ScanView(
|
|
controller: _scanController,
|
|
scanAreaScale: 0.7,
|
|
scanLineColor: AppColors.primary,
|
|
onCapture: (String data) {
|
|
parseScanResult(data);
|
|
},
|
|
),
|
|
AppBar(
|
|
backgroundColor: AppColors.transparent,
|
|
foregroundColor: AppColors.white,
|
|
),
|
|
Positioned(
|
|
left: 16,
|
|
right: 16,
|
|
bottom: 32,
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
_actionItem(
|
|
'我的',
|
|
Icons.qr_code,
|
|
() {
|
|
Get.toNamed(UserRoutes.qrCode);
|
|
},
|
|
),
|
|
_actionItem(
|
|
'手电筒',
|
|
flashState ? Icons.flash_on : Icons.flash_off,
|
|
() {
|
|
setState(() {
|
|
_scanController.toggleTorchMode();
|
|
flashState = !flashState;
|
|
});
|
|
},
|
|
),
|
|
_actionItem(
|
|
'相册',
|
|
Icons.image,
|
|
() async {
|
|
final result = await AssetPicker.pickAssets(
|
|
Get.context!,
|
|
pickerConfig: const AssetPickerConfig(
|
|
maxAssets: 1,
|
|
requestType: RequestType.image,
|
|
),
|
|
);
|
|
|
|
if (result == null) {
|
|
return;
|
|
}
|
|
|
|
var text = await Scan.parse((await result.first.file)!.path);
|
|
|
|
if (text == null) {
|
|
UiTools.toast('二维码识别失败');
|
|
}
|
|
|
|
parseScanResult(text!);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _actionItem(String text, IconData icon, VoidCallback onTap) {
|
|
return InkWell(
|
|
onTap: () {
|
|
onTap.call();
|
|
},
|
|
child: Column(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.all(8),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.white.withOpacity(0.4),
|
|
borderRadius: BorderRadius.circular(32),
|
|
),
|
|
child: Icon(
|
|
icon,
|
|
color: AppColors.white,
|
|
size: 24,
|
|
),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
text,
|
|
style: const TextStyle(
|
|
color: AppColors.white,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|