rewards_my_vm.dart 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  4. import 'package:riverpod_annotation/riverpod_annotation.dart';
  5. import 'package:shared/utils/log_utils.dart';
  6. import './rewards_my_active/rewards_my_active_page.dart';
  7. import './rewards_my_expired/rewards_my_expired_page.dart';
  8. import './rewards_my_used/rewards_my_used_page.dart';
  9. part 'rewards_my_vm.g.dart';
  10. class RewardsMyVmState {
  11. List<Map<String, dynamic>>? topSectionsData;
  12. int? curIdx;
  13. RewardsMyVmState({
  14. List<Map<String, dynamic>>? topSectionsData,
  15. this.curIdx = 0,
  16. }) : topSectionsData = topSectionsData?? [
  17. {
  18. "title": "Active",
  19. "icon": Assets.propertyIoan,
  20. "pageStartInstanceFn": RewardsMyActivePage.startInstance,
  21. "page": const RewardsMyActivePage(),
  22. },
  23. {
  24. "title": "Expired",
  25. "icon": Assets.propertyNews,
  26. "pageStartInstanceFn": RewardsMyExpiredPage.startInstance,
  27. "page": const RewardsMyExpiredPage(),
  28. },
  29. {
  30. "title": "Used",
  31. "icon": Assets.propertySale,
  32. "pageStartInstanceFn": RewardsMyUsedPage.startInstance,
  33. "page": const RewardsMyUsedPage(),
  34. }
  35. ];
  36. RewardsMyVmState copyWith({
  37. List<Map<String, dynamic>>? topSectionsData,
  38. int? curIdx = 0,
  39. }) {
  40. return RewardsMyVmState(
  41. topSectionsData: topSectionsData ?? this.topSectionsData,
  42. curIdx: curIdx ?? 0,
  43. );
  44. }
  45. }
  46. @riverpod
  47. class RewardsMyVm extends _$RewardsMyVm {
  48. get topSectionsData => state.topSectionsData;
  49. RewardsMyVmState initState() {
  50. return RewardsMyVmState();
  51. }
  52. @override
  53. RewardsMyVmState build(){
  54. final state = initState();
  55. Log.d("--------------------------build---------------------");
  56. // 初始时导航到子路由
  57. WidgetsBinding.instance.addPostFrameCallback((_) {
  58. });
  59. return state;
  60. }
  61. // 页面切换
  62. switchPage(int index,BuildContext? context, [bool? isFirstInitSwitch] ){
  63. if(state.curIdx != index){
  64. state = state.copyWith(curIdx: index);
  65. final List<Map<String, dynamic>>? topSectionsData = state.topSectionsData;
  66. // Log.d("当前页面${topSectionsData?[index]['pageStartInstanceFn']}");
  67. final pageStartInstanceFn = topSectionsData?[index]['pageStartInstanceFn'] as Function({BuildContext? context});
  68. pageStartInstanceFn(context:context);
  69. }else {
  70. if(isFirstInitSwitch??false){
  71. final List<Map<String, dynamic>>? topSectionsData = state.topSectionsData;
  72. // Log.d("当前页面${topSectionsData?[index]['pageStartInstanceFn']}");
  73. final pageStartInstanceFn = topSectionsData?[index]['pageStartInstanceFn'] as Function({BuildContext? context});
  74. pageStartInstanceFn(context:context);
  75. }
  76. }
  77. }
  78. }