336 lines
12 KiB
Dart
336 lines
12 KiB
Dart
import 'dart:io';
|
||
|
||
import 'package:ef/ef.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||
import 'package:flutterflow_ui/flutterflow_ui.dart';
|
||
import 'package:permission_handler/permission_handler.dart';
|
||
import 'package:vbvs_app/common/color/appConstants.dart';
|
||
import 'package:vbvs_app/common/util/CommonVariables.dart';
|
||
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
|
||
import 'package:vbvs_app/common/util/FitTool.dart';
|
||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||
import 'package:vbvs_app/component/tool/CustomCard.dart';
|
||
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
|
||
import 'package:vbvs_app/controller/user_info_controller.dart';
|
||
import 'package:vbvs_app/pages/common/selectDialog.dart';
|
||
import 'package:vbvs_app/pages/main_bottom/component/main_page_b_bottom_change.dart';
|
||
import 'package:vbvs_app/pages/mh_page/test/WebviewTestModel.dart';
|
||
|
||
class HomeDeviceStausWidget extends StatefulWidget {
|
||
final deviceStatus;
|
||
|
||
const HomeDeviceStausWidget({super.key, required this.deviceStatus});
|
||
|
||
@override
|
||
State<HomeDeviceStausWidget> createState() => _HomeDeviceStausWidgetState();
|
||
}
|
||
|
||
class _HomeDeviceStausWidgetState extends State<HomeDeviceStausWidget> {
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Container(
|
||
width: double.infinity,
|
||
constraints: BoxConstraints(minHeight: 180.rpx),
|
||
decoration: BoxDecoration(
|
||
color: Color(0xFF003058),
|
||
borderRadius: BorderRadius.circular(16.rpx),
|
||
),
|
||
child: Padding(
|
||
padding: EdgeInsetsDirectional.fromSTEB(67.rpx, 0.rpx, 30.rpx, 0.rpx),
|
||
child: Row(
|
||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||
children: [
|
||
Flexible(
|
||
child: Column(
|
||
mainAxisAlignment: MainAxisAlignment.start,
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
Container(
|
||
width: 170.rpx,
|
||
height: 81.rpx,
|
||
child: ClipRRect(
|
||
borderRadius: BorderRadius.circular(8.rpx),
|
||
child: Image.network(
|
||
widget
|
||
.deviceStatus['device_image'], // 从DeviceStatus获取图片
|
||
width: 200.rpx,
|
||
height: 200.rpx,
|
||
fit: BoxFit.cover,
|
||
),
|
||
),
|
||
),
|
||
Text(
|
||
'${(widget.deviceStatus['name'] ?? '').isEmpty ? (widget.deviceStatus['device_type'] == 2 ? '智能电动床'.tr : widget.deviceStatus['device_type'] == 3 ? '律动智能床垫'.tr : '未命名'.tr) : widget.deviceStatus['name']}',
|
||
style: TextStyle(
|
||
color: Colors.white,
|
||
fontSize: 26.rpx,
|
||
letterSpacing: 0.0.rpx,
|
||
),
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
),
|
||
].divide(SizedBox(height: 26.rpx)),
|
||
),
|
||
),
|
||
SizedBox(width: 26.rpx),
|
||
Column(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
// Text(
|
||
// widget.deviceStatus['status'] == 1 ? '在线'.tr : '离线'.tr,
|
||
// style: TextStyle(
|
||
// color: widget.deviceStatus['status'] == 1
|
||
// ? stringToColor("#07BD60")
|
||
// : stringToColor("#F84B20"),
|
||
// fontSize: 26.rpx,
|
||
// letterSpacing: 0.0.rpx,
|
||
// ),
|
||
// ),
|
||
CustomCard(
|
||
gradientDirection: GradientDirection.vertical,
|
||
borderRadius: 10.rpx,
|
||
onTap: () async {
|
||
try {
|
||
var device = widget.deviceStatus;
|
||
WebviewTestController webviewTestController = Get.find();
|
||
|
||
webviewTestController.web.jsbridge?.dart.switchLanguage(
|
||
mhLanguageController
|
||
.selectLanguage.value!.language_code);
|
||
|
||
await checkBlueToothPermissin();
|
||
|
||
webviewTestController.web.jsbridge?.dart
|
||
.pageActive(false);
|
||
|
||
await Future.delayed(Duration(seconds: 1));
|
||
|
||
MainPageBBottomChange.jumpTo(2);
|
||
|
||
// 🔹 重试调用 appToHtmlDevice
|
||
int attempt = 0;
|
||
const int maxAttempts = 10;
|
||
bool success = false;
|
||
|
||
while (attempt < maxAttempts && !success) {
|
||
try {
|
||
bool? flag = await webviewTestController
|
||
.web.jsbridge?.dart
|
||
.appToHtmlDevice(device);
|
||
|
||
if (flag == true) {
|
||
success = true; // 成功,跳出循环
|
||
} else {
|
||
ef.log(
|
||
"appToHtmlDevice 返回 false,第 ${attempt + 1} 次重试");
|
||
}
|
||
} catch (e) {
|
||
ef.log("appToHtmlDevice 异常,第 ${attempt + 1} 次重试: $e");
|
||
}
|
||
|
||
attempt++;
|
||
if (!success) {
|
||
await Future.delayed(Duration(seconds: 1));
|
||
}
|
||
}
|
||
|
||
if (!success) {
|
||
ef.log("appToHtmlDevice 超过最大重试次数 $maxAttempts,调用失败");
|
||
}
|
||
} catch (e) {
|
||
DailyLogUtils.writeError("进入控制页发生异常: $e");
|
||
}
|
||
},
|
||
colors:
|
||
AppConstants().mhtButtongradientColors, // 你原本没有渐变,单色即可
|
||
enableGradient: true, // 关闭渐变
|
||
enableAnimation: true, // 启用缩放动画
|
||
child: SizedBox(
|
||
width: 150.rpx,
|
||
height: 61.rpx,
|
||
child: Center(
|
||
child: Text(
|
||
'控制'.tr,
|
||
style: TextStyle(
|
||
color: Color(0xFF003058),
|
||
fontSize: 26.rpx,
|
||
letterSpacing: 0.0.rpx,
|
||
),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
].divide(SizedBox(height: 36.rpx)),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
// Future<Map> checkBlueToothPermissin() async {
|
||
// UserInfoController userInfoController = Get.find();
|
||
// bool show = false;
|
||
// Map data = {};
|
||
// var bluetoothScanGranted;
|
||
// var bluetoothConnectGranted;
|
||
// var locationGranted;
|
||
// try {
|
||
// // 如果没初始化过,就检查并请求权限
|
||
// if (userInfoController.initLocationpermission != 1) {
|
||
// bluetoothScanGranted = await Permission.bluetoothScan.isGranted;
|
||
// bluetoothConnectGranted = await Permission.bluetoothConnect.isGranted;
|
||
// locationGranted = await Permission.location.isGranted;
|
||
|
||
// // 如果权限没全部授予
|
||
// if (!bluetoothScanGranted ||
|
||
// !bluetoothConnectGranted ||
|
||
// !locationGranted) {
|
||
// show = true;
|
||
// // 同屏显示权限说明弹窗(阻塞等待用户点击确认)
|
||
// showPermissionInfoDialog(
|
||
// Get.context!,
|
||
// CommonVariables().bluetoothpermissionInfo,
|
||
// );
|
||
|
||
// // 再请求权限
|
||
// await [
|
||
// Permission.bluetoothScan,
|
||
// Permission.bluetoothConnect,
|
||
// Permission.location,
|
||
// ].request();
|
||
|
||
// // 再次检查最新权限状态
|
||
// bluetoothScanGranted = await Permission.bluetoothScan.isGranted;
|
||
// bluetoothConnectGranted = await Permission.bluetoothConnect.isGranted;
|
||
// locationGranted = await Permission.location.isGranted;
|
||
// }
|
||
// }
|
||
// // 通知 web 端
|
||
// data = {
|
||
// 'bluetoothScanGranted': bluetoothScanGranted,
|
||
// 'bluetoothConnectGranted': bluetoothConnectGranted,
|
||
// 'locationGranted': locationGranted,
|
||
// };
|
||
// WebviewTestController webviewTestController = Get.find();
|
||
// // webviewTestController.web.jsbridge?.dart.updatePermission(data);
|
||
// } catch (e) {
|
||
// ef.log("蓝牙权限:$e");
|
||
// } finally {
|
||
// if (show) {
|
||
// Navigator.of(Get.context!, rootNavigator: true).pop();
|
||
// }
|
||
// }
|
||
|
||
// // 标记初始化完成
|
||
// userInfoController.initLocationpermission = 1;
|
||
// return data;
|
||
// }
|
||
|
||
Future<Map> checkBlueToothPermissin() async {
|
||
UserInfoController userInfoController = Get.find();
|
||
bool show = false;
|
||
Map data = {};
|
||
var bluetoothScanGranted;
|
||
var bluetoothConnectGranted;
|
||
var locationGranted;
|
||
|
||
if (Platform.isIOS) {
|
||
try {
|
||
PermissionStatus isBleGranted = await Permission.bluetooth.request();
|
||
if (!isBleGranted.isGranted) {
|
||
// startBluetoothScanning();
|
||
await FlutterBluePlus.startScan(timeout: Duration(seconds: 10));
|
||
data = {
|
||
'bluetoothScanGranted': true,
|
||
'bluetoothConnectGranted': true,
|
||
};
|
||
} else {
|
||
try {
|
||
await FlutterBluePlus.startScan(timeout: Duration(seconds: 10));
|
||
} catch (e) {
|
||
TopSlideNotification.show(Get.context!,
|
||
text: "蓝牙权限未开启,请在设置中开启蓝牙权限".tr,
|
||
textColor: themeController.currentColor.sc9);
|
||
}
|
||
}
|
||
return data;
|
||
} catch (e) {
|
||
ef.log("${e.toString()}");
|
||
}
|
||
} else {
|
||
try {
|
||
// 先显示权限说明弹窗(同屏显示)
|
||
bool bluetoothScanPermanentlyDenied =
|
||
await Permission.bluetoothScan.isPermanentlyDenied;
|
||
bool bluetoothConnectPermanentlyDenied =
|
||
await Permission.bluetoothConnect.isPermanentlyDenied;
|
||
bool locationPermanentlyDenied =
|
||
await Permission.location.isPermanentlyDenied;
|
||
|
||
if (bluetoothScanPermanentlyDenied ||
|
||
bluetoothConnectPermanentlyDenied ||
|
||
locationPermanentlyDenied) {
|
||
return data; // 直接返回,不再弹窗申请
|
||
}
|
||
if (userInfoController.initLocationpermission == 1) {
|
||
bluetoothScanGranted = await Permission.bluetoothScan.isGranted;
|
||
bluetoothConnectGranted = await Permission.bluetoothConnect.isGranted;
|
||
locationGranted = await Permission.location.isGranted;
|
||
data = {
|
||
'bluetoothScanGranted': bluetoothScanGranted,
|
||
'bluetoothConnectGranted': bluetoothConnectGranted,
|
||
'locationGranted': locationGranted,
|
||
};
|
||
return data;
|
||
}
|
||
showPermissionInfoDialog(
|
||
Get.context!,
|
||
CommonVariables().bluetoothpermissionInfo,
|
||
);
|
||
show = true;
|
||
|
||
// 检查并请求权限
|
||
bluetoothScanGranted = await Permission.bluetoothScan.isGranted;
|
||
bluetoothConnectGranted = await Permission.bluetoothConnect.isGranted;
|
||
locationGranted = await Permission.location.isGranted;
|
||
|
||
if (!bluetoothScanGranted ||
|
||
!bluetoothConnectGranted ||
|
||
!locationGranted) {
|
||
await [
|
||
Permission.bluetoothScan,
|
||
Permission.bluetoothConnect,
|
||
Permission.location,
|
||
].request();
|
||
|
||
// 更新权限状态
|
||
bluetoothScanGranted = await Permission.bluetoothScan.isGranted;
|
||
bluetoothConnectGranted = await Permission.bluetoothConnect.isGranted;
|
||
locationGranted = await Permission.location.isGranted;
|
||
}
|
||
|
||
data = {
|
||
'bluetoothScanGranted': bluetoothScanGranted,
|
||
'bluetoothConnectGranted': bluetoothConnectGranted,
|
||
'locationGranted': locationGranted,
|
||
};
|
||
|
||
// 通知 web 端
|
||
WebviewTestController webviewTestController = Get.find();
|
||
// webviewTestController.web.jsbridge?.dart.updatePermission(data);
|
||
} catch (e) {
|
||
ef.log("蓝牙权限:$e");
|
||
} finally {
|
||
if (show) {
|
||
Navigator.of(Get.context!, rootNavigator: true).pop();
|
||
}
|
||
}
|
||
}
|
||
|
||
userInfoController.initLocationpermission = 1;
|
||
return data;
|
||
}
|