user_read_history_page.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/widget/my_txt.dart';
  8. import '../../model/news_data_model.dart';
  9. import '../../provider/user_favorite_provider.dart';
  10. import '../../provider/user_read_history_provider.dart';
  11. import '../../widget/empty_1_widget.dart';
  12. import '../news/news_child_main_page.dart';
  13. /// @author: bo.zeng
  14. /// @email: cnhbwds@gmail.com
  15. /// @date: 2025 2025/4/9 16:00
  16. /// @description:
  17. class UserReadHistoryPage extends ConsumerStatefulWidget {
  18. const UserReadHistoryPage({super.key});
  19. @override
  20. ConsumerState<UserReadHistoryPage> createState() =>
  21. _UserReadHistoryPageState();
  22. }
  23. final userReadHistoryProvider =
  24. NotifierProvider<UserReadHistoryProvider, UserNews>(() {
  25. return UserReadHistoryProvider();
  26. });
  27. class _UserReadHistoryPageState extends ConsumerState<UserReadHistoryPage> {
  28. int pageNum = 0;
  29. int totalNews = 0;
  30. @override
  31. void initState() {
  32. super.initState();
  33. ref.read(userReadHistoryProvider.notifier).fetchUserReadHistory(pageNum);
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. final news = ref.watch(userReadHistoryProvider);
  38. ref.listen(userReadHistoryProvider, (pre, next) {
  39. totalNews = next.rows?.length ?? 0;
  40. });
  41. return Scaffold(
  42. appBar: AppBar(
  43. // 移除阴影
  44. scrolledUnderElevation: 0,
  45. // 禁用滚动时的阴影变化
  46. backgroundColor: Colors.white,
  47. title: myTxt(
  48. text: "阅读历史",
  49. fontSize: 18.sp,
  50. fontWeight: FontWeight.bold,
  51. ),
  52. centerTitle: true,
  53. ),
  54. body:
  55. news.rows?.isEmpty == true
  56. ? Empty1widget()
  57. : EasyRefresh.builder(
  58. onRefresh: () async {
  59. pageNum = 0;
  60. await ref
  61. .read(userReadHistoryProvider.notifier)
  62. .fetchUserReadHistory(pageNum);
  63. return IndicatorResult.success;
  64. },
  65. onLoad: () async {
  66. if (totalNews >= news.total.safeValue) {
  67. return IndicatorResult.noMore;
  68. } else {
  69. pageNum++;
  70. await ref
  71. .read(userReadHistoryProvider.notifier)
  72. .fetchUserReadHistory(pageNum);
  73. return IndicatorResult.success;
  74. }
  75. },
  76. childBuilder: (context, py) {
  77. return ListView.separated(
  78. physics: py,
  79. padding: EdgeInsets.symmetric(
  80. horizontal: horizontalPadding,
  81. ),
  82. itemBuilder: (BuildContext context, int index) {
  83. return buildNewsNewItem(
  84. news.rows?[index] ?? NewsRecord(),
  85. context,
  86. );
  87. },
  88. separatorBuilder: (BuildContext context, int index) {
  89. return SizedBox(height: 5.h,);
  90. },
  91. itemCount: news.rows?.length ?? 0,
  92. );
  93. },
  94. ),
  95. );
  96. }
  97. }