user_activity_page.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import 'package:easy_refresh/easy_refresh.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import 'package:news_app/constant/size_res.dart';
  6. import 'package:news_app/extension/base.dart';
  7. import 'package:news_app/provider/user_favorite_provider.dart';
  8. import 'package:news_app/widget/empty_1_widget.dart';
  9. import 'package:news_app/widget/my_txt.dart';
  10. import '../../constant/color_res.dart';
  11. import '../../model/activity_model.dart';
  12. import '../../provider/user_activity_provider.dart';
  13. import '../activity/activity_card_widget.dart';
  14. /// @author: bo.zeng
  15. /// @email: cnhbwds@gmail.com
  16. /// @date: 2025 2025/4/9 16:00
  17. /// @description:
  18. final userActivityProvider =
  19. NotifierProvider<UserActivityProvider, UserActivity>(
  20. () => UserActivityProvider(),
  21. );
  22. class UserActivityPage extends ConsumerWidget {
  23. const UserActivityPage({super.key});
  24. @override
  25. Widget build(BuildContext context, WidgetRef ref) {
  26. return DefaultTabController(
  27. length: 2,
  28. child: Scaffold(
  29. appBar: AppBar(
  30. title: myTxt(
  31. text: "我的活动",
  32. fontSize: 18.sp,
  33. fontWeight: FontWeight.bold,
  34. ),
  35. // 移除阴影
  36. scrolledUnderElevation: 0,
  37. // 禁用滚动时的阴影变化
  38. backgroundColor: Colors.white,
  39. centerTitle: true,
  40. bottom: TabBar(
  41. tabAlignment: TabAlignment.center,
  42. isScrollable: false,
  43. indicatorColor: color2877FF,
  44. dividerHeight: 0,
  45. labelColor: color2877FF,
  46. // 选中标签颜色
  47. unselectedLabelColor: color7788A0,
  48. labelStyle: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold),
  49. unselectedLabelStyle: TextStyle(
  50. fontSize: 16.sp,
  51. fontWeight: FontWeight.bold,
  52. ),
  53. tabs: [Tab(text: "报名通过"), Tab(text: "报名未通过")],
  54. ),
  55. ),
  56. body: TabBarView(
  57. children: [
  58. _UserActivityListWidget(type: 1),
  59. _UserActivityListWidget(type: 2),
  60. ],
  61. ),
  62. ),
  63. );
  64. }
  65. }
  66. class _UserActivityListWidget extends ConsumerStatefulWidget {
  67. final int type;
  68. const _UserActivityListWidget({required this.type});
  69. @override
  70. ConsumerState<ConsumerStatefulWidget> createState() {
  71. return _UserActivityListWidgetState();
  72. }
  73. }
  74. class _UserActivityListWidgetState
  75. extends ConsumerState<_UserActivityListWidget>
  76. with AutomaticKeepAliveClientMixin {
  77. int pageNum = 0;
  78. int currentTotal = 0;
  79. @override
  80. void initState() {
  81. super.initState();
  82. ref
  83. .read(userActivityProvider.notifier)
  84. .fetchUserActivity(type: widget.type, pageNum: pageNum);
  85. }
  86. @override
  87. Widget build(BuildContext context) {
  88. super.build(context);
  89. var activity = ref.watch(userActivityProvider);
  90. ref.listen(userActivityProvider, (pre, next) {
  91. currentTotal = next.rows?.length ?? 0;
  92. });
  93. return activity.rows?.isEmpty == true
  94. ? Empty1widget()
  95. : EasyRefresh.builder(
  96. onRefresh: () async {
  97. pageNum = 0;
  98. await ref
  99. .read(userActivityProvider.notifier)
  100. .fetchUserActivity(type: widget.type, pageNum: pageNum);
  101. return IndicatorResult.success;
  102. },
  103. onLoad: () async {
  104. if (currentTotal >= activity.total.safeValue) {
  105. return IndicatorResult.noMore;
  106. } else {
  107. pageNum++;
  108. await ref
  109. .read(userActivityProvider.notifier)
  110. .fetchUserActivity(type: widget.type, pageNum: pageNum);
  111. return IndicatorResult.success;
  112. }
  113. },
  114. childBuilder: (context, py) {
  115. return ListView.separated(
  116. physics: py,
  117. separatorBuilder: (context, index) {
  118. return SizedBox(height: 10.h);
  119. },
  120. padding: EdgeInsets.symmetric(
  121. horizontal: horizontalPadding,
  122. vertical: horizontalPadding,
  123. ),
  124. itemCount: activity.rows?.length ?? 0,
  125. itemBuilder: (context, index) {
  126. return ActivityCardWidget(
  127. cellData: activity.rows?[index] ?? ActivityModelRecord(),
  128. );
  129. },
  130. );
  131. },
  132. );
  133. }
  134. @override
  135. bool get wantKeepAlive => true;
  136. }