PostsGalleryProvider.dart 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:douyin_demo/models/PostsModel.dart';
  4. import 'package:douyin_demo/widgets/WebRequest.dart';
  5. import 'package:flutter/material.dart';
  6. // import 'package:my_app/models/PostsModel.dart';
  7. // import 'package:my_app/tools/WebRequest.dart';
  8. import 'package:http/http.dart' as http;
  9. // import 'package:provider/provider.dart';
  10. class PostsGalleryProvider with ChangeNotifier {
  11. List<PostsModel> model1 = List<PostsModel>();
  12. List<PostsModel> model2 = List<PostsModel>();
  13. double _len1 = 0;
  14. double _len2 = 0;
  15. // get models1 => _model1;
  16. // get models2 => _model2;
  17. List<PostsModel> posts = List<PostsModel>();
  18. PostsGalleryProvider() {
  19. getPosts(0, 0);
  20. notifyListeners();
  21. }
  22. Future getPosts(orderType, ifRefresh) async {
  23. Uri url = await WebRequest().generate('posts/getPosts', {
  24. "openId": "ol_BV4zcyVJaOBtOTD5AfpkFERww",
  25. "dataFrom": "0",
  26. "count": "30",
  27. "refreshTime": DateTime.now().toString(),
  28. "currentSel": "1",
  29. "ulo": "0",
  30. "ula": "0"
  31. });
  32. var response = await http.get(url);
  33. //.then((response) {
  34. // var post = json.decode(response.body)["result"];
  35. setGalleryModel(response.body);
  36. notifyListeners();
  37. }
  38. setGalleryModel(String items) {
  39. var result = List<PostsModel>();
  40. var posts = json.decode(items)["result"];
  41. // result.add(posts);
  42. for (var item in posts) {
  43. result.add(PostsModel.fromJson(item));
  44. }
  45. for (var item in result) {
  46. if (_len1 <= _len2) {
  47. item.makerName="";
  48. item.picsPath="";
  49. item.postsLocation="";
  50. item.postsReaded=0;
  51. model1.add(item);
  52. _len1 += item.picsRate;
  53. } else {
  54. model2.add(item);
  55. _len2 += item.picsRate;
  56. }
  57. }
  58. // notifyListeners();
  59. }
  60. }