app-starlock/lib/login/selectCountryRegion/selectCountryRegion_page.dart
2024-12-19 14:30:08 +08:00

172 lines
6.3 KiB
Dart
Executable File

import 'package:azlistview/azlistview.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:star_lock/login/selectCountryRegion/common/countryRegionEntity.dart';
import 'package:star_lock/network/api_repository.dart';
import 'package:star_lock/tools/baseGetXController.dart';
import '../../app_settings/app_colors.dart';
import '../../tools/keySearchWidget.dart';
import '../../tools/titleAppBar.dart';
import '../../translations/current_locale_tool.dart';
import 'common/index.dart';
class SelectCountryRegionPage extends StatefulWidget {
const SelectCountryRegionPage({Key? key}) : super(key: key);
@override
State<SelectCountryRegionPage> createState() =>
_SelectCountryRegionPageState();
}
class _SelectCountryRegionPageState extends State<SelectCountryRegionPage> {
List<CountryRegionModel> countriesList = <CountryRegionModel>[];
List<CountryRegionModel> topCountriesList = <CountryRegionModel>[];
TextEditingController searchController = TextEditingController();
String currentLanguage = '';
@override
void initState() {
super.initState();
currentLanguage = CurrentLocaleTool.getCurrentLocaleString();
SuspensionUtil.setShowSuspensionStatus(
countriesList.cast<ISuspensionBean>());
Future.delayed(const Duration(milliseconds: 20), getCountriesListRequest);
}
//请求国家/地区json文件
Future<void> getCountriesListRequest() async {
final CountryRegionEntity entity =
await ApiRepository.to.getCountryRegion('1');
countriesList.clear();
if (entity.errorCode!.codeIsSuccessful) {
countriesList.addAll(entity.dataList!);
for (final CountryRegionModel model in countriesList) {
if (model.code! == '86') {
topCountriesList.add(model);
break;
}
}
_handleList(countriesList);
}
}
Future<String> loadJsonFromAssets(String assetsPath) async {
return await rootBundle.loadString(assetsPath);
}
void _handleList(List<CountryRegionModel> list) {
if (list.isEmpty) {
return;
}
for (int i = 0, length = list.length; i < length; i++) {
final CountryRegionModel countryModel = list[i];
final String tag = countryModel.group!;
if (RegExp('[A-Z]').hasMatch(tag)) {
list[i].tagIndex = tag;
} else {
list[i].tagIndex = '#';
}
}
// A-Z sort.
SuspensionUtil.sortListBySuspensionTag(list);
// show sus tag.
SuspensionUtil.setShowSuspensionStatus(list);
setState(() {});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: const Color(0xFFFFFFFF),
appBar: TitleAppBar(
barTitle: '选择国家/地区'.tr,
haveBack: true,
backgroundColor: AppColors.mainColor,
),
body: Column(
children: <Widget>[
KeySearchWidget(
editingController: searchController,
backgroundColor: AppColors.mainBackgroundColor,
onSubmittedAction: () {
if (searchController.text.isEmpty) {
getCountriesListRequest();
} else {
final List<CountryRegionModel> searchList =
<CountryRegionModel>[];
for (int i = 0, length = countriesList.length;
i < length;
i++) {
final CountryRegionModel countryModel = countriesList[i];
if (countryModel.name!.contains(searchController.text) ||
countryModel.code!.contains(searchController.text)) {
searchList.add(countryModel);
}
}
countriesList = searchList;
_handleList(searchList);
}
setState(() {});
},
),
SizedBox(height: 20.h),
Visibility(
visible: searchController.text.isEmpty,
child: Utils.getSusItem(context, '')),
if (countriesList.isNotEmpty)
Visibility(
visible: searchController.text.isEmpty,
child: Utils.getListItem(context, topCountriesList[0], () {
final CountryRegionModel model = topCountriesList[0];
final Map<String, dynamic> resultMap = {};
resultMap['code'] = model.code ?? '';
resultMap['countryId'] = model.countryId.toString() ?? '';
resultMap['countryName'] = model.name ?? '';
Navigator.pop(context, resultMap);
}))
else
Container(),
Expanded(
child: AzListView(
data: countriesList,
itemCount: countriesList.length,
itemBuilder: (BuildContext context, int index) {
final CountryRegionModel model = countriesList[index];
return Utils.getListItem(context, model, () {
final Map<String, dynamic> resultMap = {};
resultMap['code'] = model.code;
resultMap['countryId'] = model.countryId.toString();
resultMap['countryName'] = model.name;
// AppLog.log("model.name:${model.name} model.code:${model.code} model.countryId:${model.countryId} model.flag:${model.flag} model.group:${model.group} model.tagIndex:${model.tagIndex}");
Navigator.pop(context, resultMap);
});
},
padding: EdgeInsets.zero,
susItemBuilder: (BuildContext context, int index) {
final CountryRegionModel model = countriesList[index];
final String tag = model.getSuspensionTag();
return Utils.getSusItem(context, tag);
},
indexBarData: currentLanguage == 'zh_CN'
? const <String>['', ...kIndexBarData]
: [],
),
),
],
),
),
);
}
}