Files
tuiche/lib/pages/mh_page/room_picker.dart
2025-07-21 16:50:57 +08:00

342 lines
16 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:vbvs_app/common/color/ServiceConstant.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/mh_controller/device_list_controller.dart';
import 'package:vbvs_app/pages/mh_page/test/WebviewTestModel.dart';
class RoomPickerPage extends StatefulWidget {
final Map data;
RoomPickerPage({Key? key, required this.data});
@override
_RoomPickerPageState createState() => _RoomPickerPageState();
}
class _RoomPickerPageState extends State<RoomPickerPage> {
late Map<String, dynamic> editedData;
RxList rooms = [].obs;
late FixedExtentScrollController scrollController;
DeviceListController deviceListController = Get.find();
int selectedIndex = 0;
Key key = Key(DateTime.now().toString());
@override
void initState() {
super.initState();
editedData = Map.from(widget.data);
getRoomList();
scrollController = FixedExtentScrollController(initialItem: 0);
}
getRoomList() async {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.room_list;
String queryUrl = "$serviceAddress$serviceName$serviceApi";
await requestWithLog(
logTitle: '查询房间列表',
method: MyHttpMethod.get,
queryUrl: queryUrl,
onSuccess: (res) {
rooms.assignAll(res.data);
// 如果传入 roomName匹配对应索引
String? roomName = widget.data['roomName'];
if (roomName != null && roomName.isNotEmpty) {
int index = rooms.indexWhere((e) => e['name'] == roomName);
if (index != -1) {
selectedIndex = index;
} else {
selectedIndex = 0; // 找不到时默认选第一个
}
} else {
selectedIndex = 0;
}
scrollController =
FixedExtentScrollController(initialItem: selectedIndex);
setState(() {
key = Key(DateTime.now().toString());
}); // 更新 UI
},
);
}
BoxConstraints? bodysize;
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, cc) {
bodysize = cc;
return GestureDetector(
// onTap: () => FocusScope.of(context).unfocus(),,
child: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/new_background.png'), // 本地图片
fit: BoxFit.fill, // 填满整个 Container
),
),
child: Scaffold(
backgroundColor: Colors.transparent, // 深蓝背景
appBar: AppBar(
backgroundColor: Colors.transparent,
iconTheme: const IconThemeData(color: Colors.white),
automaticallyImplyLeading: false,
titleSpacing: 0,
title: SizedBox(
width: double.infinity,
height: 180.rpx,
child: Stack(
alignment: Alignment.center,
children: [
// 中间居中的标题
Text(
'房间选择',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 30.rpx,
),
),
// 左侧图标
Positioned(
left: 0.rpx,
child: returnIconButtomNew,
),
if (widget.data != null &&
(widget.data['isNextStep'] != null &&
widget.data['isNextStep'] == true))
Positioned(
right: 30.rpx,
child: CustomCard(
borderRadius: 16.rpx,
gradientDirection: GradientDirection.vertical,
onTap: () async {
// Get.toNamed("/applyRepairPage");
try {
String serviceAddress =
ServiceConstant.service_address;
String serviceName =
ServiceConstant.server_service;
String serviceApi =
ServiceConstant.device_show;
String queryUrl =
"$serviceAddress$serviceName$serviceApi";
await requestWithLog(
logTitle: "更新设备信息",
method: MyHttpMethod.put,
queryUrl: queryUrl,
data: {
"id": editedData["_id"],
"roomId": rooms[selectedIndex]['_id'],
},
onSuccess: (res) {
editedData['roomName'] =
rooms[selectedIndex]['name'];
TopSlideNotification.show(context,
text: "更新成功".tr,
textColor: Color(0XFF00C1AA));
deviceListController.getDeviceList();
Get.offNamed("/bindDeviceSuccess");
},
onFailure: (res) {
TopSlideNotification.show(context,
text: "更新失败".tr,
textColor: Color(0xFFFF7159));
},
);
} catch (e) {
print(e);
}
},
colors: const [
Color(0xFFFCFCFC),
Color(0xFFF8FAF9),
Color(0XFFECF6F3),
Color(0XFFD9F0E9),
Color(0xFFCEECE3)
],
child: Container(
width: 120.rpx,
height: 60.rpx,
alignment: Alignment.center,
child: Text(
"下一步",
style: TextStyle(
fontFamily: 'Readex Pro',
color: Color(0XFF011D33),
letterSpacing: 0,
fontSize: 30.rpx,
),
),
),
),
)
],
),
),
centerTitle: false,
),
body: SafeArea(
child: Column(
children: [
const Spacer(),
// 滚轮选择器
SizedBox(
height: 360.rpx,
child: Stack(
alignment: Alignment.center,
children: [
// 滚轮列表
Obx(
() => ListWheelScrollView.useDelegate(
key: key,
controller: scrollController,
itemExtent: 120.rpx,
perspective: 0.003,
physics: const FixedExtentScrollPhysics(),
onSelectedItemChanged: (index) {
setState(() => selectedIndex = index);
},
childDelegate: ListWheelChildBuilderDelegate(
builder: (context, index) {
if (index >= rooms.length) return null;
final isSelected = index == selectedIndex;
return Center(
child: Text(
rooms[index]['name'],
style: TextStyle(
fontSize:
isSelected ? 36.rpx : 30.rpx,
color: isSelected
? Colors.white
: const Color(0xFF929699),
),
),
);
},
childCount: rooms.length,
),
),
),
// 上边线
Positioned(
top: 120.rpx,
left: 60.rpx,
right: 60.rpx,
child: Container(
height: 1,
color: const Color(0xFF929699),
),
),
// 下边线
Positioned(
top: 240.rpx,
left: 60.rpx,
right: 60.rpx,
child: Container(
height: 1,
color: const Color(0xFF929699),
),
),
],
),
),
const Spacer(),
// 完成按钮
if (widget.data['isNextStep'] == null ||
widget.data['isNextStep'] == false)
Padding(
padding: EdgeInsets.only(
left: 30.rpx,
right: 30.rpx,
bottom: 83.rpx,
),
child: SizedBox(
width: double.infinity,
height: 50,
child: CustomCard(
borderRadius: 16.rpx,
gradientDirection: GradientDirection.vertical,
onTap: () async {
// Get.toNamed("/applyRepairPage");
try {
String serviceAddress =
ServiceConstant.service_address;
String serviceName =
ServiceConstant.server_service;
String serviceApi =
ServiceConstant.device_show;
String queryUrl =
"$serviceAddress$serviceName$serviceApi";
await requestWithLog(
logTitle: "更新设备信息",
method: MyHttpMethod.put,
queryUrl: queryUrl,
data: {
"id": editedData["_id"],
"roomId": rooms[selectedIndex]['_id'],
},
onSuccess: (res) {
editedData['roomName'] =
rooms[selectedIndex]['name'];
TopSlideNotification.show(context,
text: "更新成功".tr,
textColor: Color(0XFF00C1AA));
deviceListController.getDeviceList();
try {
WebviewTestController
webviewTestController =
Get.find();
webviewTestController
.web.jsbridge?.dart
.alterDevice();
} catch (e) {
ef.log("[h5]通知列表更新错误:$e");
}
Get.back(result: editedData);
},
onFailure: (res) {
TopSlideNotification.show(context,
text: "更新失败".tr,
textColor: Color(0xFFFF7159));
},
);
} catch (e) {
print(e);
}
},
colors: const [
Color(0xFFFCFCFC),
Color(0xFFF8FAF9),
Color(0XFFECF6F3),
Color(0XFFD9F0E9),
Color(0xFFCEECE3)
],
child: Container(
width: double.infinity,
height: 90.rpx,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
),
child: Text("完成".tr,
style: TextStyle(
color: const Color(0xFF003058),
fontSize: 30.rpx,
)),
),
)),
),
],
),
),
)));
});
}
}