app-starlock/star_lock/lib/login/selectCountryRegion/selectCountryRegion_page.dart
2024-02-01 11:22:44 +08:00

131 lines
4.6 KiB
Dart

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/trans_lib.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 = [];
TextEditingController searchController = TextEditingController();
@override
void initState() {
super.initState();
SuspensionUtil.setShowSuspensionStatus(
countriesList.cast<ISuspensionBean>());
Future.delayed(const Duration(milliseconds: 20), () {
getCountriesListRequest();
});
}
//请求国家/地区json文件
Future<void> getCountriesListRequest() async {
CountryRegionEntity entity = await ApiRepository.to.getCountryRegion('1');
countriesList.clear();
if (entity.errorCode!.codeIsSuccessful) {
countriesList.addAll(entity.dataList!);
_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++) {
CountryRegionModel countryModel = list[i];
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(() {
Get.log('list.length:${list.length}');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: const Color(0xFFFFFFFF),
appBar: TitleAppBar(
barTitle:
"${TranslationLoader.lanKeys!.select!.tr} ${TranslationLoader.lanKeys!.countryAndRegion!.tr}",
haveBack: true,
backgroundColor: AppColors.mainColor,
),
body: Column(
children: [
KeySearchWidget(
editingController: searchController,
backgroundColor: AppColors.mainBackgroundColor,
onSubmittedAction: () {
var searchList = <CountryRegionModel>[];
for (int i = 0, length = countriesList.length; i < length; i++) {
CountryRegionModel countryModel = countriesList[i];
if(countryModel.name!.contains(searchController.text) || countryModel.code!.contains(searchController.text)){
searchList.add(countryModel);
Get.log('countryModel.name:${countryModel.name} countryModel.code:${countryModel.code}');
}
}
Get.log('searchList.length:${searchList.length}');
_handleList(searchList);
},
),
SizedBox(height:20.h),
Expanded(
child: AzListView(
data: countriesList,
itemCount: countriesList.length,
itemBuilder: (BuildContext context, int index) {
CountryRegionModel model = countriesList[index];
return Utils.getListItem(context, model, () {
Map<String, dynamic> resultMap = {};
resultMap['code'] = model.code;
resultMap['countryId'] = model.countryId.toString();
resultMap['countryName'] = model.name;
Navigator.pop(context, resultMap);
});
},
padding: EdgeInsets.zero,
susItemBuilder: (BuildContext context, int index) {
CountryRegionModel model = countriesList[index];
String tag = model.getSuspensionTag();
return Utils.getSusItem(context, tag);
},
indexBarData: const ['', ...kIndexBarData],
),
),
],
),
);
}
}