rewards_my_vm.dart 2.8 KB

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