更新城市选择

This commit is contained in:
wyf
2025-11-21 10:34:18 +08:00
parent b2a621c2d3
commit 991bf97fd1
27 changed files with 79242 additions and 467 deletions

View File

@@ -8,8 +8,11 @@ import 'package:flutter_svg/svg.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:vbvs_app/common/color/appConstants.dart';
import 'package:vbvs_app/common/color/app_uri_status.dart';
import 'package:vbvs_app/common/pojo/city.dart';
import 'package:vbvs_app/common/util/EventBus.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/common/util/eventType.dart';
import 'package:vbvs_app/component/tool/ClickableContainer.dart';
import 'package:vbvs_app/component/tool/CustomCard.dart';
import 'package:vbvs_app/component/tool/NewTopSlideNotification.dart';
@@ -50,6 +53,16 @@ class _DeviceDataComponentWidgetState extends State<DeviceDataComponentWidget> {
@override
void initState() {
super.initState();
late StreamSubscription<SwitchLanguageEvent> subscription;
// 监听切换语言
subscription = EventBus().on<SwitchLanguageEvent>().listen((event) async {
final CityModelController cityController =
Get.find<CityModelController>();
ef.log("切换语言事件通知:${event.language}");
cityController.cityList = [];
await initializeCityData();
});
}
void _showPopup() {
@@ -454,7 +467,8 @@ class _DeviceDataComponentWidgetState extends State<DeviceDataComponentWidget> {
color: Colors.black,
),
cursorColor: Colors.white,
initialValue: widget.device['person']['name'] == null ||
initialValue: widget.device['person'] == null ||
widget.device['person']['name'] == null ||
widget.device['person']['name'] == ""
? "体征监测设备".tr
: widget.device['person']['name'],
@@ -1093,6 +1107,22 @@ class _DeviceDataComponentWidgetState extends State<DeviceDataComponentWidget> {
personController.dateTime =
MyUtils.formatBirthdayTime(
widget.device['person']['birthday']);
if (widget.device['person']['city_id'] != null) {
// 根据city_id查找完整的城市数据
final int cityId =
widget.device['person']['city_id'];
await initializeCityData();
final CityModel? completeCityData =
_findCityDataById(cityId);
if (completeCityData != null) {
personController.cityModel = completeCityData;
} else {
personController.cityModel = null;
}
} else {
personController.cityModel = null;
}
} else {
personController.update_person_mac.value =
widget.device['mac'];
@@ -1104,6 +1134,7 @@ class _DeviceDataComponentWidgetState extends State<DeviceDataComponentWidget> {
personController.height.value = "";
personController.weight.value = "";
personController.diseaseList.value = [];
personController.cityModel = null;
}
await Get.toNamed("/updatePersonPage",
arguments: widget.device['bind_type']);
@@ -1497,4 +1528,96 @@ class _DeviceDataComponentWidgetState extends State<DeviceDataComponentWidget> {
return 4;
}
}
// 根据ID查找完整的城市数据
CityModel? _findCityDataById(int cityId) {
try {
if (cityId == null) {
return null;
}
// 从加载的城市数据中查找
final cityData = Get.find<CityModelController>().cityList;
// 遍历三级数据结构查找匹配的ID
for (var country in cityData) {
// 检查国家节点
if (country.id == cityId) {
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 == cityId) {
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 == cityId) {
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) {
ef.log("根据ID查找城市数据失败$e");
return null;
}
}
Future initializeCityData() async {
final CityModelController cityController = Get.find<CityModelController>();
// 确保城市数据已加载
if (!cityController.isDataLoaded) {
await cityController.loadAndSetCityData();
}
// 如果device中有city_id查找并设置城市数据
if (widget.device['person']['city_id'] != null) {
final String cityId = widget.device['person']['city_id'].toString();
ef.log("开始查找city_id: $cityId");
final CityModel? completeCityData = cityController.findCityById(cityId);
if (completeCityData != null) {
personController.cityModel = completeCityData;
ef.log("成功设置城市数据: ${completeCityData.displayName}");
} else {
personController.cityModel = CityModel(id: int.tryParse(cityId));
ef.log("未找到对应城市数据创建默认CityModel");
}
personController.updateAll();
}
}
}