area.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'package:amap_location_fluttify/amap_location_fluttify.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:provider/provider.dart';
  4. import 'package:sport/application.dart';
  5. import 'package:sport/provider/login_info_model.dart';
  6. import 'package:sport/provider/user_model.dart';
  7. import 'package:sport/widgets/dialog/alert_dialog.dart';
  8. import 'package:sport/widgets/dialog/request_dialog.dart';
  9. import 'package:sport/widgets/image.dart';
  10. import 'package:sport/widgets/misc.dart';
  11. const List<String> RANK_AREA = ["全国榜", "全省榜", "全市榜"];
  12. class AreaPage extends StatefulWidget {
  13. final String area;
  14. AreaPage({this.area});
  15. @override
  16. State<StatefulWidget> createState() => _PageState();
  17. }
  18. class _PageState extends State<AreaPage> {
  19. String _rank;
  20. @override
  21. void initState() {
  22. super.initState();
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. _rank = widget.area ?? RANK_AREA[0];
  27. return PopupMenuButton(
  28. onSelected: (v) async {
  29. var userModel = Provider.of<UserModel>(context, listen: false);
  30. var user = userModel.user;
  31. if (v != RANK_AREA[0]) {
  32. if (user.city?.isEmpty == true) {
  33. await userModel.initUser();
  34. user = userModel.user;
  35. }
  36. if (user.city?.isEmpty == true) {
  37. if (await showDialog(
  38. context: context,
  39. builder: (context) => CustomAlertDialog(title: '是否获取你的当前位置', ok: () => Navigator.of(context).pop(true)),
  40. ) ==
  41. true) {
  42. await request(context, () async {
  43. Location location = await Application.location();
  44. print(location);
  45. if (location != null) {
  46. await new LoginInfoModel().updateUserInfo(context, user.name, user.gender, districtId: int.parse(location.adCode));
  47. user = userModel.user;
  48. }
  49. });
  50. } else {
  51. return;
  52. }
  53. }
  54. }
  55. if (_rank == v) return;
  56. _rank = v;
  57. bool all = _rank == RANK_AREA[0];
  58. bool province = _rank == RANK_AREA[1];
  59. AreaNotification(_rank, all, province, user.provinceId, user.cityId, user.districtId).dispatch(context);
  60. setState(() {});
  61. },
  62. itemBuilder: (BuildContext context) {
  63. return divideMenus(RANK_AREA
  64. .map((e) => menuItemSelected(e, e, e == _rank))
  65. .toList());
  66. },
  67. child: Row(
  68. mainAxisSize: MainAxisSize.min,
  69. children: <Widget>[
  70. Text(
  71. _rank, style: Theme.of(context).textTheme.bodyText1
  72. ),
  73. Padding(
  74. padding: const EdgeInsets.fromLTRB(6, 6, 12.0, 6),
  75. child: arrowBottom(),
  76. )
  77. ],
  78. ),
  79. );
  80. }
  81. }
  82. class AreaNotification extends Notification {
  83. String rank;
  84. bool all, province;
  85. int provinceId, cityId, districtId;
  86. AreaNotification(this.rank, this.all, this.province, this.provinceId, this.cityId, this.districtId);
  87. }