property_vm.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 '../../ioan/property_ioan_page.dart';
  7. import '../../news/page/property_news_page.dart';
  8. import '../../rent/page/property_rent_page.dart';
  9. import '../../sale/page/property_sale_page.dart';
  10. part 'property_vm.g.dart';
  11. class PropertyVmState {
  12. List<Map<String, dynamic>>? topSectionsData;
  13. int? curIdx;
  14. PropertyVmState({
  15. List<Map<String, dynamic>>? topSectionsData,
  16. this.curIdx = 0,
  17. }) : topSectionsData = topSectionsData?? [
  18. {
  19. "title": "Ioan",
  20. "icon": Assets.propertyIoan,
  21. "pageStartInstanceFn": PropertyIoanPage.startInstance,
  22. "page": const PropertyIoanPage(),
  23. },
  24. {
  25. "title": "News",
  26. "icon": Assets.propertyNews,
  27. "pageStartInstanceFn": PropertyNewsPage.startInstance,
  28. "page": const PropertyNewsPage(),
  29. },
  30. {
  31. "title": "Sale",
  32. "icon": Assets.propertySale,
  33. "pageStartInstanceFn": PropertySalePage.startInstance,
  34. "page": const PropertySalePage(),
  35. },
  36. {
  37. "title": "Rent",
  38. "icon": Assets.propertyRent,
  39. "pageStartInstanceFn": PropertyRentPage.startInstance,
  40. "page": const PropertyRentPage(),
  41. },
  42. ];
  43. PropertyVmState copyWith({
  44. List<Map<String, dynamic>>? topSectionsData,
  45. int? curIdx = 0,
  46. }) {
  47. return PropertyVmState(
  48. topSectionsData: topSectionsData ?? this.topSectionsData,
  49. curIdx: curIdx ?? 0,
  50. );
  51. }
  52. }
  53. @riverpod
  54. class PropertyVm extends _$PropertyVm {
  55. get topSectionsData => state.topSectionsData;
  56. PropertyVmState initState() {
  57. return PropertyVmState();
  58. }
  59. @override
  60. PropertyVmState build(){
  61. final state = initState();
  62. Log.d("--------------------------build---------------------");
  63. // 初始时导航到子路由
  64. WidgetsBinding.instance.addPostFrameCallback((_) {
  65. switchPage(state.curIdx ?? 0, null, true);
  66. });
  67. return state;
  68. }
  69. // 页面切换
  70. switchPage(int index,BuildContext? context, [bool? isFirstInitSwitch] ){
  71. if(state.curIdx != index){
  72. state = state.copyWith(curIdx: index);
  73. final List<Map<String, dynamic>>? topSectionsData = state.topSectionsData;
  74. // Log.d("当前页面${topSectionsData?[index]['pageStartInstanceFn']}");
  75. final pageStartInstanceFn = topSectionsData?[index]['pageStartInstanceFn'] as Function({BuildContext? context});
  76. pageStartInstanceFn(context:context);
  77. }else {
  78. if(isFirstInitSwitch??false){
  79. final List<Map<String, dynamic>>? topSectionsData = state.topSectionsData;
  80. // Log.d("当前页面${topSectionsData?[index]['pageStartInstanceFn']}");
  81. final pageStartInstanceFn = topSectionsData?[index]['pageStartInstanceFn'] as Function({BuildContext? context});
  82. pageStartInstanceFn(context:context);
  83. }
  84. }
  85. }
  86. }