special_list_page.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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:go_router/go_router.dart';
  6. import 'package:news_app/constant/api_const.dart';
  7. import 'package:news_app/constant/color_res.dart';
  8. import 'package:news_app/constant/size_res.dart';
  9. import 'package:news_app/model/special_model.dart';
  10. import 'package:news_app/provider/special_list_provider.dart';
  11. import 'package:news_app/util/theme_util.dart';
  12. import 'package:news_app/widget/empty_1_widget.dart';
  13. import 'package:news_app/widget/load_image.dart';
  14. import '../../model/special_detail.dart';
  15. import '../../provider/special_detail_provider.dart';
  16. import '../../widget/my_txt.dart';
  17. /// @author: bo.zeng
  18. /// @email: cnhbwds@gmail.com
  19. /// @date: 2025 2025/4/29 22:16
  20. /// @description:
  21. class SpecialListPage extends ConsumerStatefulWidget {
  22. static final String routeName = '/special/List';
  23. final String cid;
  24. const SpecialListPage({super.key, required this.cid});
  25. @override
  26. ConsumerState<SpecialListPage> createState() => _SpecialListPageState();
  27. }
  28. final specialListProvider = NotifierProvider<SpecialListProvider, SpecialModel>(
  29. () {
  30. return SpecialListProvider();
  31. },
  32. );
  33. final specialDetailProvider =
  34. NotifierProvider<SpecialDetailProvider, SpecialDetail>(() {
  35. return SpecialDetailProvider();
  36. });
  37. class _SpecialListPageState extends ConsumerState<SpecialListPage> {
  38. int pageNum = 0;
  39. int currentTotal = 0;
  40. @override
  41. void initState() {
  42. super.initState();
  43. setImmersiveStatusBar();
  44. Future.wait([
  45. ref
  46. .read(specialDetailProvider.notifier)
  47. .fetchSpecialDetail(cid: widget.cid),
  48. ref
  49. .read(specialListProvider.notifier)
  50. .fetchSpecialList(cid: widget.cid, pageNum: pageNum),
  51. ]);
  52. }
  53. void refresh() async {}
  54. Widget _buildHeader(BuildContext context, SpecialDetail data) {
  55. return SizedBox(
  56. width: double.infinity,
  57. height: 190.h, // 你自己项目里的适配高度
  58. child: Stack(
  59. fit: StackFit.expand, // 横向完全撑满
  60. children: [
  61. LoadImage(data.logo ?? testImageUrlGirl, fit: BoxFit.cover),
  62. Padding(
  63. padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 40.h),
  64. child: Column(
  65. crossAxisAlignment: CrossAxisAlignment.start,
  66. children: [
  67. SizedBox(height: 14.h),
  68. Row(
  69. children: [
  70. GestureDetector(
  71. onTap: () => context.pop(),
  72. child: Icon(Icons.arrow_back_ios, color: Colors.white),
  73. ),
  74. ],
  75. ),
  76. ],
  77. ),
  78. ),
  79. ],
  80. ),
  81. );
  82. }
  83. @override
  84. Widget build(BuildContext context) {
  85. final specialDetailData = ref.watch(specialDetailProvider);
  86. final specialListData = ref.watch(specialListProvider);
  87. ref.listen(specialListProvider, (pre, next) {
  88. currentTotal = next.total ?? 0;
  89. });
  90. return Material(
  91. color: colorF5F7FD,
  92. child: EasyRefresh.builder(
  93. onRefresh: () async {
  94. pageNum = 0;
  95. await Future.wait([
  96. ref
  97. .read(specialDetailProvider.notifier)
  98. .fetchSpecialDetail(cid: widget.cid),
  99. ref
  100. .read(specialListProvider.notifier)
  101. .fetchSpecialList(cid: widget.cid, pageNum: pageNum),
  102. ]);
  103. return IndicatorResult.success;
  104. },
  105. onLoad: () async {
  106. if (currentTotal == specialListData.total) {
  107. return IndicatorResult.noMore;
  108. } else {
  109. pageNum++;
  110. await ref
  111. .read(specialListProvider.notifier)
  112. .fetchSpecialList(cid: widget.cid, pageNum: pageNum);
  113. return IndicatorResult.success;
  114. }
  115. },
  116. childBuilder: (ctx, py) {
  117. return CustomScrollView(
  118. physics: py,
  119. slivers: [
  120. SliverToBoxAdapter(
  121. child: _buildHeader(context, specialDetailData),
  122. ),
  123. SliverToBoxAdapter(
  124. child: Container(
  125. color: Colors.white,
  126. padding: EdgeInsets.all(horizontalPadding),
  127. child: myTxt(
  128. text: specialDetailData.name ?? "",
  129. fontSize: 15.sp,
  130. color: color333333,
  131. maxLines: 1,
  132. ),
  133. ),
  134. ),
  135. SliverPadding(
  136. padding: EdgeInsets.all(horizontalPadding),
  137. sliver:
  138. specialListData.records?.isEmpty == true
  139. ? SliverToBoxAdapter(child: Empty1widget())
  140. : SliverList.separated(
  141. itemCount: specialListData.records?.length ?? 0,
  142. itemBuilder: (context, index) {
  143. final item = specialListData.records?[index];
  144. return GestureDetector(
  145. onTap: () {
  146. context.push(
  147. "/news/detail",
  148. extra: item?.contentId,
  149. );
  150. },
  151. child: Container(
  152. padding: EdgeInsets.all(10.w),
  153. decoration: BoxDecoration(
  154. color: Colors.white,
  155. borderRadius: BorderRadius.circular(4.r),
  156. ),
  157. child: Row(
  158. children: [
  159. Expanded(
  160. flex: 3,
  161. child: LoadImage(
  162. width: 93.w,
  163. height: 70.h,
  164. item?.imagesSrc?[0] ?? "",
  165. fit: BoxFit.cover,
  166. ),
  167. ),
  168. SizedBox(width: 8.w),
  169. Expanded(
  170. flex: 7,
  171. child: SizedBox(
  172. height: 72.h,
  173. child: Column(
  174. crossAxisAlignment:
  175. CrossAxisAlignment.start,
  176. mainAxisAlignment:
  177. MainAxisAlignment.spaceBetween,
  178. children: [
  179. myTxt(
  180. text: item?.title ?? "",
  181. fontSize: 15.sp,
  182. color: color333333,
  183. maxLines: 2,
  184. ),
  185. Row(
  186. spacing: 12.w,
  187. children: [
  188. myTxt(
  189. text: item?.author ?? "",
  190. fontSize: 12.sp,
  191. color: colorA7A6A6,
  192. ),
  193. myTxt(
  194. text: item?.publishDate ?? "",
  195. fontSize: 12.sp,
  196. color: colorA7A6A6,
  197. ),
  198. ],
  199. ),
  200. ],
  201. ),
  202. ),
  203. ),
  204. ],
  205. ),
  206. ),
  207. );
  208. },
  209. separatorBuilder: (context, index) {
  210. return SizedBox(height: 10.h);
  211. },
  212. ),
  213. ),
  214. ],
  215. );
  216. },
  217. ),
  218. );
  219. }
  220. }