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 createState() => _SelectCountryRegionPageState(); } class _SelectCountryRegionPageState extends State { List countriesList = []; List topCountriesList = []; TextEditingController searchController = TextEditingController(); @override void initState() { super.initState(); SuspensionUtil.setShowSuspensionStatus(countriesList.cast()); Future.delayed(const Duration(milliseconds: 20), getCountriesListRequest); } //请求国家/地区json文件 Future 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.name! == '中国'){ topCountriesList.add(model); break; } } _handleList(countriesList); } } Future loadJsonFromAssets(String assetsPath) async { return await rootBundle.loadString(assetsPath); } void _handleList(List 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: '${TranslationLoader.lanKeys!.select!.tr} ${TranslationLoader.lanKeys!.countryAndRegion!.tr}', haveBack: true, backgroundColor: AppColors.mainColor, ), body: Column( children: [ KeySearchWidget( editingController: searchController, backgroundColor: AppColors.mainBackgroundColor, onSubmittedAction: () { if(searchController.text.isEmpty){ getCountriesListRequest(); }else{ final List searchList = []; 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 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 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: const ['★', ...kIndexBarData], ), ), ], ), ), ); } }