更新眠花糖选择城市

This commit is contained in:
wyf
2025-12-17 11:01:30 +08:00
parent f2657e4238
commit 18c3b86e8a
7 changed files with 572 additions and 77 deletions

View File

@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:intl/intl.dart';
import 'package:vbvs_app/common/color/ServiceConstant.dart';
import 'package:vbvs_app/common/pojo/city.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';
@@ -12,7 +13,9 @@ import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/pages/common/selectDialog.dart';
import 'package:vbvs_app/pages/mh_page/device/controller/mht_bluetooth_controller.dart';
import 'package:vbvs_app/pages/mh_page/homepage/controller/mht_home_controller.dart';
import 'package:vbvs_app/pages/person/select_city.dart';
//保存人员信息
class MHTPeopleInfoPage extends StatefulWidget {
const MHTPeopleInfoPage({Key? key}) : super(key: key);
@@ -22,10 +25,13 @@ class MHTPeopleInfoPage extends StatefulWidget {
class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
final MHTBlueToothController bluetoothController = Get.find();
final CityModelController cityController = Get.find<CityModelController>();
final List<TextEditingController> _nameControllers = [];
final List<TextEditingController> _contactControllers = [];
List<Map<String, dynamic>> peopleList = [];
List<CityModel> cityModels = []; // 存储每个person的城市数据
bool isLoading = true;
late Future<List<CityModel>> cityDataFuture;
@override
void initState() {
@@ -34,11 +40,19 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
}
void _initializeData() async {
// 先加载城市数据
cityDataFuture = cityController.loadAndSetCityData().then((success) {
return cityController.cityList;
});
// 等待城市数据加载完成后再初始化其他数据
await cityDataFuture;
final device = bluetoothController.currentFullDevice;
//todo 初始化传感器id
// Initialize person A
peopleList.add({
'mac'.tr: device?.macA,
'mac': device?.macA,
'gender': 1,
'id': device!.macAID,
});
@@ -46,12 +60,15 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
// Initialize person B if exists
if (device?.macB != null && device!.macB!.isNotEmpty) {
peopleList.add({
'mac'.tr: device.macB,
'mac': device.macB,
'gender': 1,
'id': device!.macBID,
});
}
// 初始化城市模型列表
cityModels = List.filled(peopleList.length, CityModel());
// Initialize controllers after data is loaded
_initializeControllers();
setState(() => isLoading = false);
@@ -100,12 +117,11 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.personnel_info;
String type = "user_message_setting";
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
try {
var body = {
'mac'.tr: personData['mac'.tr],
'mac': personData['mac'],
'name': personData['name'],
'gender': personData['gender'],
'height': personData['height'],
@@ -115,6 +131,8 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
: personData['birthday'],
'contact': personData['contact'],
'id': personData['id'],
'UTC': personData['UTC'],
'city_id': personData['city_id'],
};
await requestWithLog(
logTitle: "保存用户信息".tr,
@@ -130,22 +148,83 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
print(res);
},
);
// showToast("保存成功(${personData['mac']}", color: color_success);
} catch (e) {
print("Error saving person data: $e");
showToast("保存失败(${personData['mac']}");
}
}
// 根据城市ID查找完整的城市数据
CityModel? findCompleteCityDataById(int? id, List<CityModel> cityData) {
if (id == null) return null;
try {
// 遍历三级数据结构查找匹配的ID
for (var country in cityData) {
// 检查国家节点是否有ID匹配
if (country.id == id) {
return CityModel(
id: country.id,
value: country.value,
label: country.label,
country: country.value ?? country.country,
province: null,
city: null,
UTC: country.UTC,
children: country.children,
);
}
// 检查省份节点
for (var province in country.children ?? []) {
if (province.id == id) {
return CityModel(
id: province.id,
value: province.value,
label: province.label,
country: country.value ?? country.country,
province: province.value ?? province.province,
city: null,
UTC: province.UTC,
children: province.children,
);
}
// 检查城市节点
for (var city in province.children ?? []) {
if (city.id == id) {
return CityModel(
id: city.id,
value: city.value,
label: city.label,
country: country.value ?? country.country,
province: province.value ?? province.province,
city: city.value ?? city.city,
UTC: city.UTC,
children: city.children,
);
}
}
}
}
return null;
} catch (e) {
print("根据ID查找完整城市数据失败$e");
return null;
}
}
@override
Widget build(BuildContext context) {
if (isLoading) {
return Center(child:CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
themeController.currentColor.sc1,
),
),);
return Center(
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
themeController.currentColor.sc1,
),
),
);
}
return LayoutBuilder(
@@ -193,19 +272,84 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
borderRadius: 16.rpx,
gradientDirection: GradientDirection.vertical,
onTap: () async {
// Save all people data
for (var person in peopleList) {
await _savePersonData(person, context);
// 数据验证
bool isValid = true;
for (int i = 0; i < peopleList.length; i++) {
var person = peopleList[i];
// 验证身高
if (person['height'] != null &&
person['height'].toString().isNotEmpty &&
int.tryParse(person['height'].toString()) ==
null) {
TopSlideNotification.show(context,
text: "请选择身高".tr,
textColor: Color(0xFFFF7159));
isValid = false;
break;
}
// 验证体重
if (person['weight'] != null &&
person['weight'].toString().isNotEmpty &&
int.tryParse(person['weight'].toString()) ==
null) {
TopSlideNotification.show(context,
text: "请选择体重".tr,
textColor: Color(0xFFFF7159));
isValid = false;
break;
}
// 验证联系方式
if (person['contact'] != null &&
person['contact'].toString().isNotEmpty &&
!MyUtils.isValidPhoneNumber(
person['contact'].toString())) {
TopSlideNotification.show(context,
text: "请输入正确的联系人电话".tr,
textColor: Color(0xFFFF7159));
isValid = false;
break;
}
// 验证城市选择
final cityModel = cityModels[i];
if (cityModel.id == null) {
TopSlideNotification.show(context,
text: "请选择城市".tr,
textColor: Color(0xFFFF7159));
isValid = false;
break;
}
}
if (isValid) {
// 保存所有人员数据
for (int i = 0; i < peopleList.length; i++) {
final person = peopleList[i];
final cityModel = cityModels[i];
// 添加城市信息到person数据
person['UTC'] = cityModel.UTC;
person['city_id'] = cityModel.id;
await _savePersonData(person, context);
}
TopSlideNotification.show(context, text: "保存成功".tr);
MHTHomeController mhtHomeController = Get.find();
mhtHomeController.getPersonList();
// 跳转到下一页
Map data = {};
final device =
bluetoothController.currentFullDevice;
data['_id'] = device!.deviceID;
data['isNextStep'] = true;
Get.toNamed("/editBedPage", arguments: data);
}
TopSlideNotification.show(context, text: "保存成功".tr);
MHTHomeController mhtHomeController = Get.find();
mhtHomeController.getPersonList();
// Get.offNamed("/bindDeviceSuccess");
Map data = {};
final device = bluetoothController.currentFullDevice;
data['_id'] = device!.deviceID;
data['isNextStep'] = true;
Get.toNamed("/editBedPage", arguments: data);
},
colors: const [
Color(0xFFFCFCFC),
@@ -330,7 +474,7 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
const Duration(milliseconds: 250),
() {
showOneSelectionDialog(context,
title: "选择性别".tr,
title: "选择性别".tr,
arr: ["".tr, "".tr],
checkIndex: peopleList[index]
['gender'],
@@ -400,7 +544,7 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
() {
showHeightPickerDialog(
context,
title: "选择分数".tr,
title: "选择身高".tr,
initialHeight: int.tryParse(
peopleList[index]['height'] ??
'170') ??
@@ -467,8 +611,7 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
() {
showWeightPickerDialog(
context,
title: "选择体重".tr,
title: "选择体重".tr,
initialWeight: "50",
onConfirm: (int selectedWeight) {
setState(() {
@@ -531,7 +674,7 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
const Duration(milliseconds: 250),
() {
showDateSelectionDialog(context,
title: "选择生日".tr,
title: "选择生日".tr,
checkDate: peopleList[index]
['birthday'] is DateTime
? peopleList[index]['birthday']
@@ -650,6 +793,80 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
),
),
getLine(),
// 城市选择部分
Container(
height: 90.rpx,
margin: EdgeInsets.only(
left: 40.rpx, right: 35.rpx),
child: InkWell(
onTap: () {
FocusScope.of(context)
.requestFocus(FocusNode());
Future.delayed(
const Duration(milliseconds: 250),
() {
showCitySelectionDialog(
context,
selectedCity: cityModels[index],
onCityChanged: (CityModel newCity) {
setState(() {
cityModels[index] = newCity;
});
},
title: "选择城市".tr,
cityDataFuture: cityDataFuture,
colors: CitySelectionColors(
pickerBackgroundColor:
stringToColor("#003058"),
confirmTextColor:
stringToColor("#84F5FF"),
selectedCityColor:
stringToColor("#84F5FF"),
),
);
});
},
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
'请选择城市'.tr,
style: TextStyle(
fontFamily: 'Readex Pro',
color: Color(0xFF9EA4B7),
fontSize: 30.rpx,
letterSpacing: 0,
),
),
Row(
children: [
Text(
cityModels[index].id != null
? MyUtils
.getDetailedCityDisplayText(
cityModels[index])
: "请选择城市".tr,
style: TextStyle(
color:
cityModels[index].id != null
? Colors.white
: themeController
.currentColor.sc4,
fontSize: 30.rpx,
),
),
SizedBox(width: 16.rpx),
Icon(Icons.expand_more,
color: Colors.white,
size: 48.rpx),
],
),
],
),
),
),
getLine(),
],
))
],
@@ -667,3 +884,4 @@ class _MHTPeopleInfoPageState extends State<MHTPeopleInfoPage> {
);
}
}