community_vm.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 'community_state.dart';
  7. import '../garagesale/garagesale_page.dart';
  8. import '../newsfeed/newsfeed_page.dart';
  9. part 'community_vm.g.dart';
  10. @riverpod
  11. class CommunityVm extends _$CommunityVm {
  12. get topSectionsData => state.topSectionsData;
  13. CommunityVmState initState() {
  14. return CommunityVmState();
  15. }
  16. @override
  17. CommunityVmState build(){
  18. final state = initState();
  19. Log.d("--------------------------build---------------------");
  20. // 初始时导航到子路由
  21. WidgetsBinding.instance.addPostFrameCallback((_) {
  22. switchPage(state.curIdx ?? 0, null, true);
  23. });
  24. return state;
  25. }
  26. // 页面切换
  27. switchPage(int index,BuildContext? context, [bool? isFirstInitSwitch] ){
  28. if(state.curIdx != index){
  29. state = state.copyWith(curIdx: index);
  30. final List<Map<String, dynamic>>? topSectionsData = state.topSectionsData;
  31. // Log.d("当前页面${topSectionsData?[index]['pageStartInstanceFn']}");
  32. final pageStartInstanceFn = topSectionsData?[index]['pageStartInstanceFn'] as Function({BuildContext? context});
  33. pageStartInstanceFn(context:context);
  34. }else {
  35. if(isFirstInitSwitch??false){
  36. final List<Map<String, dynamic>>? topSectionsData = state.topSectionsData;
  37. // Log.d("当前页面${topSectionsData?[index]['pageStartInstanceFn']}");
  38. final pageStartInstanceFn = topSectionsData?[index]['pageStartInstanceFn'] as Function({BuildContext? context});
  39. pageStartInstanceFn(context:context);
  40. }
  41. }
  42. }
  43. }