From 01df8622e03e1f21105a69b44eebf942b710b7cb Mon Sep 17 00:00:00 2001 From: guozhigq Date: Thu, 31 Aug 2023 21:42:59 +0800 Subject: [PATCH] =?UTF-8?q?mod:=20=E5=85=B3=E6=B3=A8=E3=80=81=E7=B2=89?= =?UTF-8?q?=E4=B8=9D=E9=A1=B5=E9=9D=A2=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pages/fan/controller.dart | 17 ++++++++++-- lib/pages/fan/view.dart | 45 ++++++++++++++++++++++-------- lib/pages/follow/controller.dart | 16 +++++++++-- lib/pages/follow/view.dart | 47 ++++++++++++++++++++++++-------- 4 files changed, 97 insertions(+), 28 deletions(-) diff --git a/lib/pages/fan/controller.dart b/lib/pages/fan/controller.dart index 4f389b92..4f1d2ae8 100644 --- a/lib/pages/fan/controller.dart +++ b/lib/pages/fan/controller.dart @@ -1,3 +1,4 @@ +import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:hive/hive.dart'; import 'package:pilipala/http/fan.dart'; @@ -7,11 +8,13 @@ import 'package:pilipala/utils/storage.dart'; class FansController extends GetxController { Box userInfoCache = GStrorage.userInfo; int pn = 1; + int ps = 20; int total = 0; RxList fansList = [FansItemModel()].obs; late int mid; late String name; var userInfo; + RxString loadingText = '加载中...'.obs; @override void onInit() { @@ -26,23 +29,31 @@ class FansController extends GetxController { Future queryFans(type) async { if (type == 'init') { pn = 1; + loadingText.value == '加载中...'; + } + if (loadingText.value == '没有更多了') { + return; } var res = await FanHttp.fans( vmid: mid, pn: pn, - ps: 20, + ps: ps, orderType: 'attention', ); if (res['status']) { if (type == 'init') { fansList.value = res['data'].list; total = res['data'].total; - } else if (type == 'onRefresh') { - fansList.insertAll(0, res['data'].list); } else if (type == 'onLoad') { fansList.addAll(res['data'].list); } + print(total); + if ((pn == 1 && total < ps) || res['data'].list.isEmpty) { + loadingText.value = '没有更多了'; + } pn += 1; + } else { + SmartDialog.showToast(res['msg']); } return res; } diff --git a/lib/pages/fan/view.dart b/lib/pages/fan/view.dart index a0b42528..6a0af3c6 100644 --- a/lib/pages/fan/view.dart +++ b/lib/pages/fan/view.dart @@ -1,6 +1,8 @@ +import 'package:easy_debounce/easy_throttle.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:pilipala/common/widgets/http_error.dart'; +import 'package:pilipala/common/widgets/no_data.dart'; import 'package:pilipala/models/fans/result.dart'; import 'controller.dart'; @@ -17,7 +19,6 @@ class _FansPageState extends State { final FansController _fansController = Get.put(FansController()); final ScrollController scrollController = ScrollController(); Future? _futureBuilderFuture; - bool _isLoadingMore = false; @override void initState() { @@ -27,11 +28,9 @@ class _FansPageState extends State { () async { if (scrollController.position.pixels >= scrollController.position.maxScrollExtent - 200) { - if (!_isLoadingMore) { - _isLoadingMore = true; - await _fansController.queryFans('onLoad'); - _isLoadingMore = false; - } + EasyThrottle.throttle('follow', const Duration(seconds: 1), () { + _fansController.queryFans('onLoad'); + }); } }, ); @@ -66,14 +65,38 @@ class _FansPageState extends State { if (data['status']) { List list = _fansController.fansList; return Obx( - () => list.length == 1 - ? const SizedBox() - : ListView.builder( + () => list.isNotEmpty + ? ListView.builder( controller: scrollController, - itemCount: list.length, + itemCount: list.length + 1, itemBuilder: (BuildContext context, int index) { - return fanItem(item: list[index]); + if (index == list.length) { + return Container( + height: + MediaQuery.of(context).padding.bottom + 60, + padding: EdgeInsets.only( + bottom: + MediaQuery.of(context).padding.bottom), + child: Center( + child: Obx( + () => Text( + _fansController.loadingText.value, + style: TextStyle( + color: Theme.of(context) + .colorScheme + .outline, + fontSize: 13), + ), + ), + ), + ); + } else { + return fanItem(item: list[index]); + } }, + ) + : const CustomScrollView( + slivers: [NoData()], ), ); } else { diff --git a/lib/pages/follow/controller.dart b/lib/pages/follow/controller.dart index a64e20f6..fe1bfabc 100644 --- a/lib/pages/follow/controller.dart +++ b/lib/pages/follow/controller.dart @@ -1,3 +1,4 @@ +import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:hive/hive.dart'; import 'package:pilipala/http/follow.dart'; @@ -7,11 +8,13 @@ import 'package:pilipala/utils/storage.dart'; class FollowController extends GetxController { Box userInfoCache = GStrorage.userInfo; int pn = 1; + int ps = 20; int total = 0; RxList followList = [FollowItemModel()].obs; late int mid; late String name; var userInfo; + RxString loadingText = '加载中...'.obs; @override void onInit() { @@ -26,23 +29,30 @@ class FollowController extends GetxController { Future queryFollowings(type) async { if (type == 'init') { pn = 1; + loadingText.value == '加载中...'; + } + if (loadingText.value == '没有更多了') { + return; } var res = await FollowHttp.followings( vmid: mid, pn: pn, - ps: 20, + ps: ps, orderType: 'attention', ); if (res['status']) { if (type == 'init') { followList.value = res['data'].list; total = res['data'].total; - } else if (type == 'onRefresh') { - followList.insertAll(0, res['data'].list); } else if (type == 'onLoad') { followList.addAll(res['data'].list); } + if ((pn == 1 && total < ps) || res['data'].list.isEmpty) { + loadingText.value = '没有更多了'; + } pn += 1; + } else { + SmartDialog.showToast(res['msg']); } return res; } diff --git a/lib/pages/follow/view.dart b/lib/pages/follow/view.dart index d2a4b423..9e290f0f 100644 --- a/lib/pages/follow/view.dart +++ b/lib/pages/follow/view.dart @@ -1,6 +1,8 @@ +import 'package:easy_debounce/easy_throttle.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:pilipala/common/widgets/http_error.dart'; +import 'package:pilipala/common/widgets/no_data.dart'; import 'package:pilipala/models/follow/result.dart'; import 'controller.dart'; @@ -17,7 +19,6 @@ class _FollowPageState extends State { final FollowController _followController = Get.put(FollowController()); final ScrollController scrollController = ScrollController(); Future? _futureBuilderFuture; - bool _isLoadingMore = false; @override void initState() { @@ -27,11 +28,9 @@ class _FollowPageState extends State { () async { if (scrollController.position.pixels >= scrollController.position.maxScrollExtent - 200) { - if (!_isLoadingMore) { - _isLoadingMore = true; - await _followController.queryFollowings('onLoad'); - _isLoadingMore = false; - } + EasyThrottle.throttle('follow', const Duration(seconds: 1), () { + _followController.queryFollowings('onLoad'); + }); } }, ); @@ -67,14 +66,40 @@ class _FollowPageState extends State { if (data['status']) { List list = _followController.followList; return Obx( - () => list.length == 1 - ? const SizedBox() - : ListView.builder( + () => list.isNotEmpty + ? ListView.builder( controller: scrollController, - itemCount: list.length, + itemCount: list.length + 1, itemBuilder: (BuildContext context, int index) { - return followItem(item: list[index]); + if (index == list.length) { + return Container( + height: + MediaQuery.of(context).padding.bottom + + 60, + padding: EdgeInsets.only( + bottom: MediaQuery.of(context) + .padding + .bottom), + child: Center( + child: Obx( + () => Text( + _followController.loadingText.value, + style: TextStyle( + color: Theme.of(context) + .colorScheme + .outline, + fontSize: 13), + ), + ), + ), + ); + } else { + return followItem(item: list[index]); + } }, + ) + : const CustomScrollView( + slivers: [NoData()], ), ); } else {