chooseVisitTimeTitle_vm.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import 'package:cpt_services/components/chooseVisitTimeContent_vm.dart';
  2. import 'package:cpt_services/modules/services/service_order_confirm/service_order_confirm_vm.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:riverpod_annotation/riverpod_annotation.dart';
  6. import 'package:router/ext/auto_router_extensions.dart';
  7. import 'package:shared/utils/date_time_utils.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:widgets/dialog/dialog_content_wrap.dart';
  10. import '../../../respository/services_respository.dart';
  11. import 'chooseVisitTimeTitle_state.dart';
  12. part 'chooseVisitTimeTitle_vm.g.dart';
  13. List<Map<String, dynamic>> dayInfoList = [
  14. {
  15. "id": 1,
  16. "day": 'Tody',
  17. "date": '2022-01-01',
  18. "isSelected": true,
  19. "isIntrady": true,
  20. },
  21. {
  22. "id": 2,
  23. "day": 'Tommorow',
  24. "date": '2022-01-01',
  25. "isSelected": false,
  26. "isIntrady": false,
  27. },
  28. {
  29. "id": 3,
  30. "day": '周三',
  31. "date": '2022-01-01',
  32. "isSelected": false,
  33. "isIntrady": false,
  34. },
  35. {
  36. "id": 4,
  37. "day": '周四',
  38. "date": '2022-01-01',
  39. "isSelected": false,
  40. "isIntrady": false,
  41. },
  42. {
  43. "id": 5,
  44. "day": '周五',
  45. "date": '2022-01-01',
  46. "isSelected": false,
  47. "isIntrady": false,
  48. },
  49. {
  50. "id": 6,
  51. "day": '周六',
  52. "date": '2022-01-01',
  53. "isSelected": false,
  54. "isIntrady": false,
  55. },
  56. {
  57. "id": 7,
  58. "day": '周天',
  59. "date": '2022-01-01',
  60. "isSelected": false,
  61. "isIntrady": false,
  62. },
  63. ];
  64. @riverpod
  65. class ChooseVisitTimeTitleVm extends _$ChooseVisitTimeTitleVm {
  66. late ServicesRespository serviceRespositoryInstance;
  67. int _rencentDays = 7;
  68. formatWeekday(int index, String day){
  69. switch (index) {
  70. case 0:
  71. return 'Today';
  72. case 1:
  73. return 'Tomorrow';
  74. default:
  75. return day;
  76. }
  77. }
  78. ChooseVisitTimeTitleState initState(){
  79. Log.d("--------------------------initState---------------------");
  80. List<DayInfoItem> dayInfoListNew = [];
  81. // 从当前天开始 生成一份最近一个星期的 DayInfoItem
  82. DateTime now = DateTime.now();
  83. for(int i = 0; i < _rencentDays; i++){
  84. DateTime date = now.add(Duration(days: i));
  85. String day = DateTimeUtils.getWeekday(date, languageCode: 'en');
  86. String dateStr = date.toString().substring(0, 10);
  87. DayInfoItem newItem = DayInfoItem()
  88. ..id = i
  89. ..day = day = formatWeekday(i, day)
  90. ..date = dateStr
  91. ..isSelected = i == 0
  92. ..isIntrady = i == 0
  93. ..holiday = false
  94. ..enable = true;
  95. dayInfoListNew.add(newItem);
  96. }
  97. return ChooseVisitTimeTitleState(
  98. dayInfoList: dayInfoListNew,
  99. );
  100. }
  101. @override
  102. ChooseVisitTimeTitleState build(){
  103. // 引入数据仓库
  104. serviceRespositoryInstance = ref.read(servicesRespositoryProvider);
  105. ChooseVisitTimeTitleState state = initState();
  106. Log.d("--------------------------build---------------------");
  107. return state;
  108. }
  109. handlerChangeNum(BuildContext context, int num, int index){
  110. // state.dayInfoList[index].tu = num;
  111. // state = state.copyWith(
  112. // dayInfoList: state.dayInfoList,
  113. // );
  114. }
  115. // 点击某一天
  116. handlerClicKSomeDay(BuildContext context, DayInfoItem dayInfoItem, int index){
  117. state.currentSelectIndex = index;
  118. // 循环 state.dayInfoList 需要索引值为index 的 设置isSelected 为true 其他设置为false
  119. state.dayInfoList?.asMap().forEach((idx, value) {
  120. if(idx == index){
  121. value.isSelected = true;
  122. }else{
  123. value.isSelected = false;
  124. }
  125. });
  126. state = state.copyWith(
  127. currentSelectIndex: state.currentSelectIndex,
  128. dayInfoList: state.dayInfoList,
  129. );
  130. // 触发 chooseVisitTimeContent 的刷新
  131. final chooseVisitTimeContentVm = ref.read(chooseVisitTimeContentVmProvider.notifier);
  132. chooseVisitTimeContentVm?.getVisitTimeList(context, dayInfoItem.date!);
  133. }
  134. // 点击某一时间段
  135. handlerTimeItemClick(BuildContext context, int index, bool isSelected){
  136. // 修改 状态
  137. // state.dayInfoList[index].isChecked = isSelected;
  138. // state = state.copyWith(
  139. // dayInfoList: state.dayInfoList,
  140. // );
  141. //
  142. // ref.read(serviceOrderConfirmVmProvider.notifier).setConfirmOrderTotalPrice(context);
  143. }
  144. }