home_provide.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import 'dart:convert';
  2. import 'dart:async';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_note/config/service_url.dart';
  5. import 'package:flutter_note/model/home_page_model.dart';
  6. import 'package:flutter_note/model/hot_goods_model.dart';
  7. import 'package:flutter_note/service/service_method.dart';
  8. class HomeProvide with ChangeNotifier {
  9. HomePageModel _homeEntity; // 首页显示内容
  10. List<HotGoodsData> _hotGoodsList = []; // 火爆专区
  11. int _page = 0;
  12. bool _showBack = false;
  13. String _district = '定位失败';
  14. double _longitude = 115.02932;
  15. double _latitude = 35.76189;
  16. HomePageModel get homeEntity => _homeEntity;
  17. List<HotGoodsData> get hodGoodsList => _hotGoodsList;
  18. bool get showBack => _showBack;
  19. String get district => _district;
  20. double get longitude => _longitude;
  21. double get latitude => _latitude;
  22. changeDistrict(String value, double longitude, double latitude) {
  23. _district = value;
  24. _longitude = longitude;
  25. _latitude = latitude;
  26. notifyListeners();
  27. }
  28. initHomeEntity(double lon, double lat) async {
  29. _homeEntity = await _getHomePageContent(lon, lat);
  30. notifyListeners();
  31. }
  32. initHotGoodsList() async {
  33. _page = 0;
  34. var hot = await _getHomePageHots(_page);
  35. _hotGoodsList.clear();
  36. _hotGoodsList.addAll(hot.data);
  37. notifyListeners();
  38. }
  39. loadMoreHotGoods() async {
  40. _page++;
  41. var moreHot = await _getHomePageHots(_page);
  42. _hotGoodsList.addAll(moreHot.data);
  43. notifyListeners();
  44. }
  45. enableBack(bool state) {
  46. _showBack = state;
  47. notifyListeners();
  48. }
  49. Future<HomePageModel> _getHomePageContent(double lon, double lat) async {
  50. var response = await request(servicePath['homePageContent'],
  51. formData: {'lon': /*'115.02932'*/ lon, 'lat': /*'35.76189'*/ lat });
  52. return HomePageModel.fromJson(json.decode(response.data.toString()));
  53. }
  54. Future<HotGoodsModel> _getHomePageHots(int page) async {
  55. var response =
  56. await request(servicePath['homePageHotPart'], formData: {'page': page});
  57. return HotGoodsModel.fromJson(json.decode(response.data));
  58. }
  59. }