city_picker_data.dart 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import 'dart:convert';
  2. import 'package:flutter/services.dart';
  3. class CityResult {
  4. String province;
  5. String provinceCode;
  6. String city;
  7. String cityCode;
  8. String county;
  9. String countyCode;
  10. }
  11. Map<String, dynamic> _datas = {};
  12. bool _isInit = false;
  13. Future<Map<String, dynamic>> loadCityData() async {
  14. if (_isInit) {
  15. return _datas;
  16. }
  17. var s =
  18. await rootBundle.loadString("lib/assets/json/city_json.json");
  19. var params = json.decode(s);
  20. _datas.addAll(params);
  21. _isInit = true;
  22. return _datas;
  23. }
  24. void releaseCityData() {
  25. _datas.clear();
  26. _isInit = false;
  27. }
  28. List<int> findIndexs(String provinceName, String cityName, String countyName) {
  29. var proIndex = 0;
  30. var cityIndex = 0;
  31. var countyIndex = 0;
  32. if (proIndex == null && cityName == null && countyName == null) {
  33. return [0, 0, 0];
  34. }
  35. List<dynamic> pList = _datas["provinceList"];
  36. out:
  37. for (var item in pList) {
  38. if (item["name"] == provinceName) {
  39. proIndex = pList.indexOf(item);
  40. List<dynamic> cityList = item["cityList"];
  41. for (var city in cityList) {
  42. if (city["name"] == cityName) {
  43. cityIndex = cityList.indexOf(city);
  44. List<dynamic> countyList = city["countyList"];
  45. for (var county in countyList) {
  46. if (county["name"] == countyName) {
  47. countyIndex = countyList.indexOf(county);
  48. break out;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. return [proIndex, cityIndex, countyIndex];
  56. }