更新眠花糖选择城市
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:flutter/src/widgets/framework.dart';
|
||||
import 'package:flutterflow_ui/flutterflow_ui.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:vbvs_app/common/color/ServiceConstant.dart';
|
||||
import 'package:vbvs_app/common/pojo/city.dart';
|
||||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||||
import 'package:vbvs_app/common/util/requestWithLog.dart';
|
||||
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
|
||||
@@ -23,6 +24,7 @@ class PeopleInfoModel {
|
||||
String? phone;
|
||||
String? contact;
|
||||
List peopleList = [];
|
||||
List<CityModel> cityModels = [];
|
||||
PeopleInfoModel();
|
||||
|
||||
factory PeopleInfoModel.fromJson(Map<String, dynamic> json) {
|
||||
@@ -43,25 +45,33 @@ class PeopleInfoController extends GetControllerEx<PeopleInfoModel> {
|
||||
attr = GetModel(PeopleInfoModel()).obs;
|
||||
}
|
||||
|
||||
final CityModelController cityController = Get.find<CityModelController>();
|
||||
|
||||
@override
|
||||
Future<void> onInit() async {
|
||||
super.onInit();
|
||||
// await getPeoples(Get.arguments['mac']); // 控制器创建时立即执行
|
||||
cityDataFuture = cityController.loadAndSetCityData().then((success) {
|
||||
return cityController.cityList;
|
||||
});
|
||||
}
|
||||
|
||||
late Future<List<CityModel>> cityDataFuture;
|
||||
|
||||
getPeoples(String mac) async {
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.person_info;
|
||||
String queryUrl = "$serviceAddress$serviceName$serviceApi?mac=$mac";
|
||||
|
||||
requestWithLog(
|
||||
await requestWithLog(
|
||||
logTitle: "获取设备的人员信息列表",
|
||||
method: MyHttpMethod.get,
|
||||
queryUrl: queryUrl,
|
||||
onSuccess: (res) {
|
||||
if (res.data != null && res.data is List) {
|
||||
model.peopleList = res.data;
|
||||
model.cityModels = []; // 清空之前的数据
|
||||
initializeCityData(); // 初始化城市数据
|
||||
updateAll();
|
||||
print("peopleList: ${model.peopleList}");
|
||||
}
|
||||
@@ -96,4 +106,74 @@ class PeopleInfoController extends GetControllerEx<PeopleInfoModel> {
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future initializeCityData() async {
|
||||
try {
|
||||
// 确保城市数据已加载
|
||||
if (!cityController.isDataLoaded) {
|
||||
await cityController.loadAndSetCityData();
|
||||
}
|
||||
|
||||
// 清空现有的城市模型列表
|
||||
model.cityModels = [];
|
||||
|
||||
// 遍历peopleList中的每个人,查找对应的城市数据
|
||||
for (var person in model.peopleList) {
|
||||
if (person is Map<String, dynamic>) {
|
||||
// 获取person中的city_id
|
||||
final cityId = person['city_id']?.toString();
|
||||
|
||||
if (cityId != null && cityId.isNotEmpty) {
|
||||
ef.log("开始查找city_id: $cityId");
|
||||
|
||||
// 使用cityController查找完整的城市数据
|
||||
final CityModel? completeCityData = cityController.findCityById(cityId);
|
||||
|
||||
if (completeCityData != null) {
|
||||
// 将找到的城市数据添加到cityModels列表中
|
||||
model.cityModels.add(completeCityData);
|
||||
ef.log("成功添加城市数据: ${completeCityData.displayName} for person: ${person['name'] ?? 'Unknown'}");
|
||||
} else {
|
||||
// 如果找不到对应的城市数据,添加一个空的CityModel,但保留city_id
|
||||
final CityModel emptyCityModel = CityModel(id: int.tryParse(cityId));
|
||||
model.cityModels.add(emptyCityModel);
|
||||
ef.log("未找到对应城市数据,创建默认CityModel for city_id: $cityId");
|
||||
}
|
||||
} else {
|
||||
// 如果person没有city_id,添加一个空的CityModel
|
||||
model.cityModels.add(CityModel());
|
||||
ef.log("person没有city_id,添加空CityModel");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 确保cityModels列表长度与peopleList一致
|
||||
while (model.cityModels.length < model.peopleList.length) {
|
||||
model.cityModels.add(CityModel());
|
||||
}
|
||||
|
||||
// 更新UI
|
||||
updateAll();
|
||||
|
||||
ef.log("初始化城市数据完成,peopleList长度: ${model.peopleList.length}, cityModels长度: ${model.cityModels.length}");
|
||||
} catch (e) {
|
||||
ef.log("初始化城市数据失败: $e");
|
||||
}
|
||||
}
|
||||
|
||||
// 可选:添加一个方法来获取特定person的城市数据
|
||||
CityModel? getCityForPerson(int personIndex) {
|
||||
if (personIndex >= 0 && personIndex < model.cityModels.length) {
|
||||
return model.cityModels[personIndex];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 可选:添加一个方法来更新特定person的城市数据
|
||||
void updateCityForPerson(int personIndex, CityModel cityModel) {
|
||||
if (personIndex >= 0 && personIndex < model.cityModels.length) {
|
||||
model.cityModels[personIndex] = cityModel;
|
||||
updateAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user