area.dart 3.1 KB

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