chooseHouseCleanContent_vm.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import 'package:domain/entity/paid_service_detail_entity.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:riverpod_annotation/riverpod_annotation.dart';
  5. import 'package:router/ext/auto_router_extensions.dart';
  6. import 'package:shared/utils/log_utils.dart';
  7. import 'package:widgets/dialog/dialog_content_wrap.dart';
  8. import '../../../respository/services_respository.dart';
  9. import 'chooseHouseCleanContent_state.dart';
  10. part 'chooseHouseCleanContent_vm.g.dart';
  11. @riverpod
  12. class ChooseHouseCleanContentVm extends _$ChooseHouseCleanContentVm {
  13. late ServicesRespository serviceRespositoryInstance;
  14. List<PaidServiceDetailProducts> _products = [];
  15. List<Map<String, dynamic>> houseCleanList = [];
  16. ChooseHouseCleanContentState initState(){
  17. Log.d("--------------------------initState---------------------");
  18. if(houseCleanList.isNotEmpty){
  19. List<HouseCleanContentItem> houseCleanListNew = [];
  20. houseCleanList.forEach((item) {
  21. HouseCleanContentItem newItem = HouseCleanContentItem()
  22. ..id = item['id']
  23. ..price = item['price'] as double
  24. ..name = item['name']
  25. ..num = 1
  26. ..areaSizeRange = item['areaSizeRange']
  27. ..isChecked = item['isChecked']
  28. ..isDisable = item['isDisable'];
  29. houseCleanListNew.add(newItem);
  30. });
  31. return ChooseHouseCleanContentState(
  32. houseCleanList: houseCleanListNew,
  33. );
  34. }else {
  35. return ChooseHouseCleanContentState(
  36. houseCleanList: null,
  37. );
  38. }
  39. }
  40. @override
  41. ChooseHouseCleanContentState build(){
  42. // 引入数据仓库
  43. serviceRespositoryInstance = ref.read(servicesRespositoryProvider);
  44. ChooseHouseCleanContentState state = initState();
  45. Log.d("--------------------------build---------------------");
  46. return state;
  47. }
  48. setInitPageData(BuildContext context, Map<String, dynamic>? params){
  49. Log.d("--------------------------setInitPageData----------$params-----------");
  50. _products = params?['products']??[];
  51. // 渲染一帧后
  52. WidgetsBinding.instance.addPostFrameCallback((_){
  53. // 修改 state 中 HouseCleanList
  54. if(_products != null){
  55. if(_products != null && _products.isNotEmpty){
  56. List<PaidServiceDetailProducts> products = _products;
  57. List<HouseCleanContentItem> houseCleanListNew = [];
  58. products.forEach((item) {
  59. HouseCleanContentItem newItem = HouseCleanContentItem()
  60. ..id = item.id
  61. ..price = (item.price ?? 0.0).toDouble()
  62. ..name = item.name
  63. ..num = 1
  64. ..areaSizeRange = item.shortDescription
  65. ..isChecked = false
  66. ..isDisable = false;
  67. houseCleanListNew.add(newItem);
  68. });
  69. state = state.copyWith(
  70. houseCleanList: houseCleanListNew,
  71. );
  72. }
  73. }
  74. });
  75. }
  76. // 修改 item 的选中状态
  77. handlerChangeItemChecked(BuildContext context, bool isChecked, int index){
  78. List<HouseCleanContentItem> newHouseCleanList = state.houseCleanList!;
  79. newHouseCleanList?[index].isChecked = isChecked;
  80. state = state.copyWith(
  81. houseCleanList: newHouseCleanList,
  82. );
  83. }
  84. }