更新眠花糖选择城市

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

@@ -37,16 +37,35 @@ Future<List<CityModel>> loadCityData() async {
}
}
// 定义城市选择器颜色配置
class CitySelectionColors {
final Color? pickerBackgroundColor; // 选择器整体背景色
final Color? confirmTextColor; // 确定按钮文字颜色
final Color? selectedCityColor; // 选中的城市背景色
const CitySelectionColors({
this.pickerBackgroundColor,
this.confirmTextColor,
this.selectedCityColor,
});
}
Future showCitySelectionDialog(
BuildContext context, {
required CityModel selectedCity,
Function? onCityChanged,
String title = "选择城市",
required Future<List<CityModel>> cityDataFuture, // 改为required参数
CitySelectionColors? colors, // 新增:颜色配置参数
}) {
ThemeController themeController = Get.find();
final bool isChinese = Get.locale?.languageCode == 'zh' ?? true;
// 使用传入的颜色,如果没传则使用主题颜色
final Color pickerBackgroundColor = colors?.pickerBackgroundColor ?? themeController.currentColor.sc17;
final Color confirmTextColor = colors?.confirmTextColor ?? themeController.currentColor.sc2;
final Color selectedCityColor = colors?.selectedCityColor ?? themeController.currentColor.sc2;
final RxList<String> countries = <String>[].obs;
final RxList<String> provinces = <String>[].obs;
final RxList<String> cities = <String>[].obs;
@@ -243,12 +262,21 @@ Future showCitySelectionDialog(
builder: (context, snapshot) {
// 数据加载中
if (snapshot.connectionState == ConnectionState.waiting) {
return _buildLoadingBottomSheet(themeController);
return _buildLoadingBottomSheet(
themeController,
pickerBackgroundColor,
confirmTextColor,
);
}
// 数据加载错误
if (snapshot.hasError || !snapshot.hasData) {
return _buildErrorBottomSheet(themeController, context);
return _buildErrorBottomSheet(
themeController,
context,
pickerBackgroundColor,
confirmTextColor,
);
}
final cityData = snapshot.data!;
@@ -265,6 +293,9 @@ Future showCitySelectionDialog(
title,
cityData,
onCityChanged,
pickerBackgroundColor: pickerBackgroundColor,
confirmTextColor: confirmTextColor,
selectedCityColor: selectedCityColor,
countries: countries,
provinces: provinces,
cities: cities,
@@ -350,6 +381,9 @@ Widget _buildCityPickerContent(
String title,
List<CityModel> cityData,
Function? onCityChanged, {
required Color pickerBackgroundColor,
required Color confirmTextColor,
required Color selectedCityColor,
required RxList<String> countries,
required RxList<String> provinces,
required RxList<String> cities,
@@ -481,7 +515,7 @@ Widget _buildCityPickerContent(
return Container(
width: double.infinity,
decoration: BoxDecoration(
color: themeController.currentColor.sc17,
color: pickerBackgroundColor, // 使用传入的选择器整体颜色
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.rpx),
topRight: Radius.circular(20.rpx),
@@ -549,7 +583,7 @@ Widget _buildCityPickerContent(
child: Text("确定".tr,
style: TextStyle(
fontSize: 30.rpx,
color: themeController.currentColor.sc2,
color: confirmTextColor, // 使用传入的确定文字颜色
)),
),
);
@@ -600,7 +634,7 @@ Widget _buildCityPickerContent(
height: 90.rpx,
margin: EdgeInsets.symmetric(horizontal: 0.rpx),
decoration: BoxDecoration(
color: themeController.currentColor.sc2,
color: selectedCityColor, // 使用传入的选中城市颜色
borderRadius: BorderRadius.circular(16.rpx),
),
),
@@ -677,11 +711,15 @@ Widget _buildCityPickerContent(
}
// 加载中 BottomSheet
Widget _buildLoadingBottomSheet(ThemeController themeController) {
Widget _buildLoadingBottomSheet(
ThemeController themeController,
Color pickerBackgroundColor,
Color confirmTextColor,
) {
return Container(
width: double.infinity,
decoration: BoxDecoration(
color: themeController.currentColor.sc17,
color: pickerBackgroundColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.rpx),
topRight: Radius.circular(20.rpx),
@@ -692,7 +730,7 @@ Widget _buildLoadingBottomSheet(ThemeController themeController) {
mainAxisSize: MainAxisSize.min,
children: [
CircularProgressIndicator(
color: themeController.currentColor.sc2,
color: confirmTextColor,
),
SizedBox(height: 20.rpx),
Text(
@@ -709,11 +747,15 @@ Widget _buildLoadingBottomSheet(ThemeController themeController) {
// 错误 BottomSheet
Widget _buildErrorBottomSheet(
ThemeController themeController, BuildContext context) {
ThemeController themeController,
BuildContext context,
Color pickerBackgroundColor,
Color confirmTextColor,
) {
return Container(
width: double.infinity,
decoration: BoxDecoration(
color: themeController.currentColor.sc17,
color: pickerBackgroundColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.rpx),
topRight: Radius.circular(20.rpx),
@@ -739,7 +781,7 @@ Widget _buildErrorBottomSheet(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 30.rpx, vertical: 15.rpx),
decoration: BoxDecoration(
color: themeController.currentColor.sc2,
color: confirmTextColor,
borderRadius: BorderRadius.circular(8.rpx),
),
child: Text(
@@ -754,4 +796,4 @@ Widget _buildErrorBottomSheet(
],
),
);
}
}

View File

@@ -632,7 +632,7 @@ class _UpdatePageState extends State<UpdatePersonPage> {
},
child: Center(
child: Text(
_getDetailedCityDisplayText(
MyUtils.getDetailedCityDisplayText(
personController.cityModel),
textAlign: TextAlign.right,
style: TextStyle(
@@ -650,6 +650,7 @@ class _UpdatePageState extends State<UpdatePersonPage> {
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(
0, 117.rpx, 0, 0),
@@ -858,28 +859,4 @@ class _UpdatePageState extends State<UpdatePersonPage> {
);
}
// 获取城市显示文本
// 更详细的显示逻辑(可选)
String _getDetailedCityDisplayText(CityModel? cityModel) {
if (cityModel == null) {
return '选择城市'.tr;
}
// 根据数据层级显示不同的格式
if (cityModel.city != null && cityModel.city!.isNotEmpty) {
// 三级数据:国家-省份-城市
return '${cityModel.country ?? ''}-${cityModel.province ?? ''}-${cityModel.city ?? cityModel.value ?? ''}';
} else if (cityModel.province != null && cityModel.province!.isNotEmpty) {
// 二级数据:国家-省份
return '${cityModel.country ?? ''}-${cityModel.province ?? cityModel.value ?? ''}';
} else if (cityModel.country != null && cityModel.country!.isNotEmpty) {
// 一级数据:国家
return cityModel.country!;
} else if (cityModel.value != null && cityModel.value!.isNotEmpty) {
// 只有 value 字段
return cityModel.value!;
} else {
return '选择城市'.tr;
}
}
}