303 lines
9.9 KiB
Dart
303 lines
9.9 KiB
Dart
import 'package:ef/ef.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_svg/svg.dart';
|
|
import 'package:img_picker/img_picker.dart';
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
import 'package:vbvs_app/common/color/app_uri_status.dart';
|
|
import 'package:vbvs_app/common/util/FitTool.dart';
|
|
import 'package:vbvs_app/common/util/MyUtils.dart';
|
|
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
|
|
import 'package:vbvs_app/controller/device/blueteeth_bind_controller.dart';
|
|
import 'package:vbvs_app/model/api_response.dart';
|
|
import 'package:vbvs_app/pages/device_bind/componnet/bind_dialog.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
class MobileScannerTestPage extends StatefulWidget {
|
|
const MobileScannerTestPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<MobileScannerTestPage> createState() => _MobileScannerTestPageState();
|
|
}
|
|
|
|
class _MobileScannerTestPageState extends State<MobileScannerTestPage>
|
|
with TickerProviderStateMixin {
|
|
String? scannedText;
|
|
bool isScanning = true;
|
|
bool isTorchOn = false;
|
|
|
|
late AnimationController _controller;
|
|
late Animation<double> _animation;
|
|
late MobileScannerController _scannerController;
|
|
BlueteethBindController blueteethBindController = Get.find();
|
|
|
|
final double scanAreaSize = 480.rpx;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_scannerController = MobileScannerController();
|
|
_controller = AnimationController(
|
|
duration: const Duration(seconds: 2),
|
|
vsync: this,
|
|
)..repeat(reverse: true);
|
|
|
|
_animation = Tween<double>(begin: 0, end: 1).animate(CurvedAnimation(
|
|
parent: _controller,
|
|
curve: Curves.easeInOut,
|
|
));
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
_scannerController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _onDetect(BarcodeCapture capture) {
|
|
if (!isScanning) return;
|
|
|
|
final Barcode? barcode = capture.barcodes.first;
|
|
final String? value = barcode?.rawValue;
|
|
|
|
if (value != null) {
|
|
setState(() {
|
|
scannedText = value;
|
|
isScanning = false;
|
|
if (scannedText != null && scannedText!.isNotEmpty) {
|
|
blueteethBindController.scanMac.value = scannedText!;
|
|
showConfirmDialog(
|
|
context,
|
|
Container(),
|
|
'蓝牙绑定.确定绑定提示'.tr,
|
|
onConfirm: () async {
|
|
ApiResponse response =
|
|
await blueteethBindController.bindDeviceByScan(scannedText!);
|
|
if (response.code == HttpStatusCodes.ok) {
|
|
TopSlideNotification.show(
|
|
context,
|
|
text: "蓝牙绑定.连接成功".tr,
|
|
textColor: themeController.currentColor.sc2,
|
|
);
|
|
} else {
|
|
TopSlideNotification.show(
|
|
context,
|
|
text: response.msg ?? "蓝牙绑定.连接异常".tr,
|
|
textColor: themeController.currentColor.sc9,
|
|
);
|
|
}
|
|
},
|
|
onCancel: () {
|
|
print('用户点击了取消');
|
|
},
|
|
);
|
|
}
|
|
});
|
|
|
|
Future.delayed(const Duration(seconds: 2), () {
|
|
setState(() {
|
|
isScanning = true;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
void _toggleTorch() async {
|
|
await _scannerController.toggleTorch();
|
|
setState(() {
|
|
isTorchOn = !isTorchOn;
|
|
});
|
|
}
|
|
|
|
Future<void> _pickImageFromGallery() async {
|
|
final picker = ImagePicker();
|
|
final pickedFile = await picker.pickImage(source: ImageSource.gallery);
|
|
if (pickedFile != null) {
|
|
final bytes = await pickedFile.readAsBytes();
|
|
final image = await decodeImageFromList(bytes);
|
|
// 处理相册图片扫码逻辑
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.transparent,
|
|
appBar: AppBar(
|
|
systemOverlayStyle: SystemUiOverlayStyle(
|
|
statusBarColor: Colors.transparent,
|
|
statusBarIconBrightness: Brightness.light,
|
|
statusBarBrightness: Brightness.light,
|
|
),
|
|
backgroundColor: themeController.currentColor.sc17,
|
|
automaticallyImplyLeading: false,
|
|
iconTheme: IconThemeData(color: themeController.currentColor.sc3),
|
|
titleSpacing: 0,
|
|
title: Container(
|
|
width: double.infinity,
|
|
height: 180.rpx,
|
|
child: Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
Text(
|
|
'扫一扫.标题'.tr,
|
|
style: TextStyle(
|
|
fontFamily: 'Readex Pro',
|
|
color: themeController.currentColor.sc3,
|
|
letterSpacing: 0,
|
|
fontSize: 30.rpx,
|
|
),
|
|
),
|
|
Positioned(
|
|
left: 0,
|
|
child: returnIconButtom,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
centerTitle: false,
|
|
),
|
|
body: Stack(
|
|
children: [
|
|
// 扫描区域(全屏)
|
|
MobileScanner(
|
|
controller: _scannerController,
|
|
onDetect: _onDetect,
|
|
),
|
|
|
|
// 扫描框和提示(顶部)- 使用 Positioned 更精确
|
|
Positioned(
|
|
top: 0,
|
|
left: 0,
|
|
right: 0,
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Padding(
|
|
padding: EdgeInsets.only(top: 219.rpx),
|
|
child: SizedBox(
|
|
width: scanAreaSize,
|
|
height: scanAreaSize,
|
|
child: Stack(
|
|
children: [
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: themeController.currentColor.sc5,
|
|
),
|
|
),
|
|
AnimatedBuilder(
|
|
animation: _animation,
|
|
builder: (context, child) {
|
|
return Positioned(
|
|
top: scanAreaSize * _animation.value,
|
|
left: 0,
|
|
right: 0,
|
|
child: Container(
|
|
height: 1.rpx,
|
|
color: themeController.currentColor.sc2,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 31.rpx),
|
|
Text(
|
|
'扫一扫.提示'.tr,
|
|
style: TextStyle(
|
|
color: themeController.currentColor.sc2,
|
|
fontSize: 26.rpx,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
// 底部按钮区域 - 使用 Positioned 固定在底部
|
|
Positioned(
|
|
bottom: 83.rpx,
|
|
left: 0,
|
|
right: 0,
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
GestureDetector(
|
|
onTap: _pickImageFromGallery,
|
|
child: Column(
|
|
children: [
|
|
Container(
|
|
width: 91.rpx,
|
|
height: 91.rpx,
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(50.rpx),
|
|
color: stringToColor("#242835"),
|
|
),
|
|
child: Center(
|
|
child: SvgPicture.asset(
|
|
width: 34.rpx,
|
|
height: 26.rpx,
|
|
'assets/img/icon/album.svg',
|
|
fit: BoxFit.cover,
|
|
color: themeController.currentColor.sc3,
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 10.rpx),
|
|
Text(
|
|
'扫一扫.相册'.tr,
|
|
style: TextStyle(
|
|
color: themeController.currentColor.sc3,
|
|
fontSize: 24.rpx,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
SizedBox(width: 170.rpx),
|
|
GestureDetector(
|
|
onTap: _toggleTorch,
|
|
child: Column(
|
|
children: [
|
|
Container(
|
|
width: 91.rpx,
|
|
height: 91.rpx,
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(50.rpx),
|
|
color: isTorchOn
|
|
? themeController.currentColor.sc3
|
|
: stringToColor("#242835"),
|
|
),
|
|
child: Center(
|
|
child: SvgPicture.asset(
|
|
width: 26.rpx,
|
|
height: 36.rpx,
|
|
'assets/img/icon/light.svg',
|
|
fit: BoxFit.cover,
|
|
color: isTorchOn
|
|
? stringToColor("#242835")
|
|
: themeController.currentColor.sc3,
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 10.rpx),
|
|
Text(
|
|
'扫一扫.手电筒'.tr,
|
|
style: TextStyle(
|
|
color: themeController.currentColor.sc3,
|
|
fontSize: 24.rpx,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|