import 'package:ef/base/widget/flutterflow/FlutterFlowTheme.dart'; import 'package:ef/ef.dart'; import 'package:flutter/material.dart'; import 'package:vbvs_app/common/color/ServiceConstant.dart'; import 'package:vbvs_app/common/color/appConstants.dart'; import 'package:vbvs_app/common/util/FitTool.dart'; import 'package:vbvs_app/common/util/MyUtils.dart'; import 'package:vbvs_app/common/util/requestWithLog.dart'; import 'package:vbvs_app/component/tool/CustomCard.dart'; import 'package:vbvs_app/component/tool/TopSlideNotification.dart'; import 'package:vbvs_app/controller/device/body_device_controller.dart'; import 'package:vbvs_app/controller/device/device_share_list_controller.dart'; import 'package:vbvs_app/pages/device_bind/componnet/bind_dialog.dart'; import 'package:vbvs_app/pages/mh_page/test/WebviewTestModel.dart'; class THShareDeviceDetailWidget extends GetView { final scaffoldKey = GlobalKey(); List data; // 保持原始类型 String device; // 新增:设备信息 THShareDeviceDetailWidget( {super.key, required this.data, required this.device}); // 修改构造函数 DeviceShareListController deviceShareListController = Get.find(); // 转换为List的getter List> get dataList { if (data is List>) { return data as List>; } else if (data is List) { // 尝试转换 return (data as List).map((item) { if (item is Map) { return item; } else if (item is Map) { // 转换为Map return Map.from(item); } return {}; }).toList(); } return []; } // 获取分享id String? get shareId { try { for (var item in dataList) { if (item.containsKey('id') && item['id'] != null) { return item['id'].toString(); } } return null; } catch (e) { print('获取shareId错误: $e'); return null; } } // 获取uid String? get uid { try { for (var item in dataList) { if (item.containsKey('uid') && item['uid'] != null) { return item['uid'].toString(); } } return null; } catch (e) { print('获取uid错误: $e'); return null; } } // 获取opType int get currentOpType { try { for (var item in dataList) { if (item.containsKey('opType') && item['opType'] != null) { return int.tryParse(item['opType'].toString()) ?? 1; } } return 1; } catch (e) { print('获取opType错误: $e'); return 1; } } // 切换wifi配置开关的方法 void toggleWifiConfig() { int newOpType; if (opType.value == 1) { // 如果当前是允许配置wifi,则切换为不允许 newOpType = 2; } else { // 如果当前是不允许配置wifi,则切换为允许 newOpType = 1; } // 更新数据列表中的opType bool updated = false; for (var i = 0; i < dataList.length; i++) { if (dataList[i].containsKey('opType')) { // 如果是原始数据是List类型,我们需要更新原始数据 if (data is List) { (data as List)[i]['opType'] = newOpType; } else if (data is List) { // 尝试更新原始数据 try { (data as List)[i] = {...dataList[i], 'opType': newOpType}; } catch (e) { print('更新opType错误: $e'); } } updated = true; break; } } // 如果没有找到opType项,添加一个 if (!updated) { if (data is List) { (data as List).add({'opType': newOpType}); } else if (data is List) { (data as List).add({'opType': newOpType}); } } opType.value = newOpType; print('切换opType为: $newOpType'); } // 检查是否允许配置wifi bool get isAllowWifiConfig => opType.value == 1; getLine() { return Divider( color: const Color(0XFF929699), thickness: 0.5.rpx, ); } var opType = 2.obs; bool isProgrammaticPop = false; DateTime? _lastBackPressedTime; BodyDeviceController bodyDeviceController = Get.find(); @override Widget build(BuildContext context) { // 初始化opType WidgetsBinding.instance.addPostFrameCallback((_) { final current = currentOpType; if (opType.value != current) { opType.value = current; } print('初始化opType: ${opType.value}'); }); return GestureDetector( child: Container( decoration: const BoxDecoration( image: DecorationImage( image: AssetImage('assets/img/bgNoImg.png'), // 本地图片 fit: BoxFit.fill, ), ), child: Scaffold( key: scaffoldKey, backgroundColor: Colors.transparent, appBar: AppBar( iconTheme: IconThemeData(color: themeController.currentColor.sc3), backgroundColor: themeController.currentColor.sc17, automaticallyImplyLeading: false, titleSpacing: 0, title: SizedBox( width: double.infinity, height: 180.rpx, child: Stack( alignment: Alignment.center, children: [ Text( '详情'.tr, textAlign: TextAlign.center, style: TextStyle( color: Colors.white, fontSize: 30.rpx, ), ), Positioned( left: 0.rpx, child: returnIconButtomNew(onBack: () async { // await _updateDeviceInfo(context); }), ), ], ), ), centerTitle: false, ), body: PopScope( canPop: false, onPopInvokedWithResult: (disposition, result) async { if (isProgrammaticPop) { isProgrammaticPop = false; return; } await _updateDeviceInfo(context); Get.back(); }, child: Container( width: MediaQuery.sizeOf(context).width, height: MediaQuery.sizeOf(context).height * 1, child: Padding( padding: EdgeInsetsDirectional.fromSTEB(30.rpx, 34.rpx, 30.rpx, 0), child: Column( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( width: MediaQuery.sizeOf(context).width, decoration: BoxDecoration( borderRadius: BorderRadius.circular(8), ), child: Column( mainAxisSize: MainAxisSize.max, crossAxisAlignment: CrossAxisAlignment.start, children: [ // 展示数据项(k:v格式) Padding( padding: EdgeInsetsDirectional.only( start: 27.rpx, bottom: 18.rpx, ), child: Column( mainAxisSize: MainAxisSize.max, crossAxisAlignment: CrossAxisAlignment.start, children: _buildDataItems(), ), ), getLine(), SizedBox(height: 25.rpx), // opType选择部分 - 保持原来的样式 Container( decoration: BoxDecoration( // color: Color(0xFF003058), borderRadius: BorderRadius.circular(16.rpx)), child: Padding( padding: EdgeInsetsDirectional.only( start: 30.rpx, top: 0.rpx, end: 30.rpx, bottom: 0.rpx), child: Column( children: [ GestureDetector( onTap: () { print('点击允许配置wifi'); toggleWifiConfig(); }, child: Container( width: MediaQuery.sizeOf(context).width, constraints: const BoxConstraints( minHeight: 46, ), decoration: BoxDecoration( borderRadius: BorderRadius.circular(0), ), child: Row( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( '允许对方配置wifi'.tr, style: TextStyle( fontFamily: 'Readex Pro', color: const Color(0xFFFFFFFF), fontSize: 27.rpx, letterSpacing: 0.0, ), ), Obx(() { print( 'Obx重建,当前opType: ${opType.value}'); return Container( height: 33.rpx, child: AspectRatio( aspectRatio: 1, child: Center( child: Container( height: 33.rpx, width: 33.rpx, decoration: BoxDecoration( borderRadius: BorderRadius .circular( 33.rpx / 2), border: Border.all( width: opType.value == 1 ? 1 : 0.5, color: Color(0xFFC8CBD2), ), ), child: opType.value == 1 ? Center( child: ClipOval( child: Container( width: 33.rpx * 0.6, height: 33.rpx * 0.6, color: const Color( 0xFF6BFDAC), ), ), ) : null, ), ), ), ); }) ], ), ), ), ], ), )), ], ), ), // 解除分享按钮 Padding( padding: EdgeInsetsDirectional.fromSTEB( 100.rpx, 0, 100.rpx, AppConstants.page_button_bottom_padding, ), child: Container( width: MediaQuery.sizeOf(context).width, height: MediaQuery.sizeOf(context).height * 0.056, constraints: const BoxConstraints(minHeight: 46), decoration: BoxDecoration( color: Colors.transparent, borderRadius: BorderRadius.circular(16.rpx), ), child: CustomCard( // borderRadius: 16.rpx, borderRadius: AppConstants().button_container_radius, // 圆角半径 gradientDirection: GradientDirection.vertical, onTap: () async { if (uid != null) { showConfirmDialog(context, Container(), "删除提示".tr, onConfirm: () async { String serviceAddress = ServiceConstant.service_address; String serviceName = ServiceConstant.server_service; String serviceApi = ServiceConstant.device_bind; String queryUrl = "${serviceAddress}${serviceName}${serviceApi}"; final data = { "mac": device, // 修改:使用传入的device参数 "uid": uid, }; await requestWithLog( logTitle: "删除分享设备", method: MyHttpMethod.delete, queryUrl: queryUrl, data: data, onSuccess: (res) { TopSlideNotification.show(context, text: res.msg!); // deviceShareListController.model.all = 0; // deviceShareListController // .selectedShareInfo.value = []; deviceShareListController .getDeviceShareList( device); // 修改:使用传入的device参数 deviceShareListController.updateAll(); Get.back(); }, onFailure: (res) { TopSlideNotification.show(context, text: res.msg!, textColor: themeController.currentColor.sc9); deviceShareListController.updateAll(); }, ); }, onCancel: () {}); } }, colors: [ themeController.currentColor.sc9, ], child: Container( width: double.infinity, height: 90.rpx, alignment: Alignment.center, decoration: BoxDecoration( borderRadius: BorderRadius.circular(16.rpx), ), child: Text( "解除分享".tr, style: TextStyle( color: themeController.currentColor.sc3, fontSize: 30.rpx, ), ), ), ), ), ), ], ), ), ), ), ), ), ); } // 构建数据项列表(k:v格式) List _buildDataItems() { List widgets = []; bool isFirst = true; // 标记第一个有效元素 for (var item in dataList) { // 只显示包含k和v的项 if (item.containsKey('k') && item.containsKey('v') && item['k'] != null && item['v'] != null) { String key = item['k'].toString(); String value = item['v'].toString(); // 跳过 opType if (key.toLowerCase().contains('optype')) { continue; } widgets.add( Padding( padding: EdgeInsets.only(bottom: 10.rpx), child: Text( '$key: $value', style: TextStyle( fontFamily: 'Readex Pro', color: isFirst ? const Color(0xFFFFFFFF) : const Color(0xFF9EA4B7), fontSize: isFirst ? 28.rpx : 20.rpx, letterSpacing: 0.0, ), ), ), ); // 第一个有效元素处理完后,标记为 false isFirst = false; } } return widgets; } // 更新设备信息 Future _updateDeviceInfo(BuildContext context) async { try { String? id = shareId; if (id == null) { TopSlideNotification.show( context, text: "设备ID不存在".tr, textColor: Color(0xFFFF7159), ); return; } String serviceAddress = ServiceConstant.service_address; String serviceName = ServiceConstant.server_service; String serviceApi = ServiceConstant.device_show; String queryUrl = "$serviceAddress$serviceName$serviceApi"; print('准备更新设备信息,id: $id, opType: ${opType.value}'); await requestWithLog( logTitle: "更新设备信息".tr, method: MyHttpMethod.put, queryUrl: queryUrl, data: { "id": id, "opType": opType.value, }, onSuccess: (res) { TopSlideNotification.show( context, text: "更新成功".tr, textColor: Color(0XFF00C1AA), ); bodyDeviceController.getDeviceList(); try { WebviewTestController webviewTestController = Get.find(); webviewTestController.web.jsbridge?.dart.alterDevice(); } catch (e) { ef.log("[h5]通知列表更新错误:$e"); } isProgrammaticPop = true; }, onFailure: (res) { TopSlideNotification.show( context, text: "更新失败".tr, textColor: Color(0xFFFF7159), ); }, ); } catch (e) { print('更新设备信息错误: $e'); TopSlideNotification.show( context, text: "更新失败".tr, textColor: Color(0xFFFF7159), ); } } }