You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
10.0KB

  1. // ignore_for_file: public_member_api_docs, sort_constructors_first
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_bloc/flutter_bloc.dart';
  4. import 'package:get/get.dart';
  5. import 'package:farm_tpf/custom_model/CropPlot.dart';
  6. import 'package:farm_tpf/data/repository/repository.dart';
  7. import 'package:farm_tpf/presentation/custom_widgets/bloc/widget_row_plot_info.dart';
  8. import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
  9. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  10. import 'package:farm_tpf/presentation/screens/actions/crop_status/sc_edit_action_crop_status.dart';
  11. import 'package:farm_tpf/presentation/screens/actions/disease/sc_edit_action_disease.dart';
  12. import 'package:farm_tpf/presentation/screens/actions/dung/sc_edit_action_dung.dart';
  13. import 'package:farm_tpf/presentation/screens/actions/end/sc_edit_action_end.dart';
  14. import 'package:farm_tpf/presentation/screens/actions/environment_update/sc_edit_action_environment_update.dart';
  15. import 'package:farm_tpf/presentation/screens/actions/harvest/sc_edit_action_harvest.dart';
  16. import 'package:farm_tpf/presentation/screens/actions/harvest_process/sc_edit_action_harvest_process.dart';
  17. import 'package:farm_tpf/presentation/screens/actions/nursery/sc_edit_action_nursery.dart';
  18. import 'package:farm_tpf/presentation/screens/actions/other/sc_edit_action_other.dart';
  19. import 'package:farm_tpf/presentation/screens/actions/packing/sc_edit_action_packing.dart';
  20. import 'package:farm_tpf/presentation/screens/actions/plant/sc_edit_action_plant.dart';
  21. import 'package:farm_tpf/presentation/screens/actions/sell/sc_edit_action_sell.dart';
  22. import 'package:farm_tpf/presentation/screens/actions/spraying/sc_edit_action_spraying.dart';
  23. import 'package:farm_tpf/presentation/screens/actions/use_water/sc_edit_action_user_water.dart';
  24. import 'package:farm_tpf/utils/const_color.dart';
  25. import 'package:farm_tpf/utils/formatter.dart';
  26. import 'bloc/plot_detail_bloc.dart';
  27. class PlotHistoryScreen extends StatefulWidget {
  28. int? cropId;
  29. String? cropCode;
  30. int? cropType;
  31. PlotHistoryScreen({this.cropId, this.cropCode, this.cropType});
  32. @override
  33. _PlotHistoryScreenState createState() => _PlotHistoryScreenState();
  34. }
  35. class _PlotHistoryScreenState extends State<PlotHistoryScreen> with AutomaticKeepAliveClientMixin {
  36. @override
  37. void initState() {
  38. super.initState();
  39. }
  40. @override
  41. void dispose() {
  42. super.dispose();
  43. }
  44. @override
  45. Widget build(BuildContext context) {
  46. return InfinityView(
  47. cropId: widget.cropId ?? -1,
  48. cropCode: widget.cropCode ?? '',
  49. );
  50. }
  51. @override
  52. bool get wantKeepAlive => true;
  53. }
  54. class InfinityView extends StatefulWidget {
  55. int? cropId;
  56. String? cropCode;
  57. InfinityView({this.cropId, this.cropCode});
  58. @override
  59. _InfinityViewState createState() => _InfinityViewState();
  60. }
  61. class _InfinityViewState extends State<InfinityView> {
  62. var plotDetailBloc = PlotDetailBloc(repository: Repository());
  63. final _scrollController = ScrollController();
  64. final _scrollThreshold = 250.0;
  65. @override
  66. void initState() {
  67. plotDetailBloc.add(DataFetched(cropId: widget.cropId ?? -1, cropCode: widget.cropCode ?? ''));
  68. _scrollController.addListener(() {
  69. final maxScroll = _scrollController.position.maxScrollExtent;
  70. final currentScroll = _scrollController.position.pixels;
  71. if (maxScroll - currentScroll < _scrollThreshold) {
  72. plotDetailBloc.add(DataFetched(cropId: widget.cropId ?? -1, cropCode: widget.cropCode ?? ''));
  73. }
  74. });
  75. super.initState();
  76. }
  77. @override
  78. Widget build(BuildContext context) {
  79. return BlocBuilder<PlotDetailBloc, PlotDetailState>(
  80. cubit: plotDetailBloc,
  81. builder: (context, state) {
  82. if (state is PlotDetailFailure) {
  83. return Center(child: Text(state.errorString));
  84. }
  85. if (state is PlotDetailSuccess) {
  86. if (state.items.isEmpty) {
  87. return Center(
  88. child: Column(
  89. mainAxisSize: MainAxisSize.max,
  90. crossAxisAlignment: CrossAxisAlignment.center,
  91. mainAxisAlignment: MainAxisAlignment.center,
  92. children: [
  93. const Text("Không có dữ liệu lịch sử canh tác"),
  94. InkWell(
  95. onTap: () {
  96. plotDetailBloc.add(OnRefresh(cropId: widget.cropId ?? -1, cropCode: widget.cropCode ?? ''));
  97. },
  98. child: const Text('Tải lại'),
  99. ),
  100. ],
  101. ));
  102. }
  103. var currentItems = List<Activities>.from(state.items);
  104. return SafeArea(
  105. child: Scaffold(
  106. body: RefreshIndicator(
  107. child: ListView.builder(
  108. padding: const EdgeInsets.all(8),
  109. physics: const AlwaysScrollableScrollPhysics(),
  110. itemBuilder: (BuildContext context, int index) {
  111. return index >= state.items.length
  112. ? BottomLoader()
  113. : ItemInfinityWidget(
  114. currentItems: currentItems,
  115. item: state.items[index],
  116. index: index,
  117. currentPage: state.page,
  118. currentReachedMax: state.hasReachedMax);
  119. },
  120. itemCount: state.hasReachedMax ? state.items.length : state.items.length + 1,
  121. controller: _scrollController,
  122. ),
  123. onRefresh: () async {
  124. plotDetailBloc.add(OnRefresh(cropId: widget.cropId ?? -1, cropCode: widget.cropCode ?? ''));
  125. }),
  126. ),
  127. );
  128. }
  129. return Center(
  130. child: LoadingListPage(),
  131. );
  132. },
  133. );
  134. }
  135. @override
  136. void dispose() {
  137. _scrollController.dispose();
  138. super.dispose();
  139. }
  140. }
  141. class ItemInfinityWidget extends StatelessWidget {
  142. final List<Activities> currentItems;
  143. final Activities item;
  144. final int currentPage;
  145. final bool currentReachedMax;
  146. final int index;
  147. const ItemInfinityWidget(
  148. {Key? key, required this.currentItems, required this.item, required this.currentPage, required this.currentReachedMax, required this.index})
  149. : super(key: key);
  150. @override
  151. Widget build(BuildContext context) {
  152. return GestureDetector(
  153. child: WidgetRowPlotInfo(
  154. color: index % 2 == 0 ? AppColors.DEFAULT.withOpacity(0.3) : AppColors.DEFAULT.withOpacity(0.1),
  155. name: '${item.activityTypeDescription ?? ''}',
  156. value: item.executeDate?.format_DDMMYY_HHmm() ?? ''),
  157. onTap: () {
  158. if (item.activityTypeName == "ACTIVE_TYPE_NURSERY") {
  159. Get.to(EditActionNurseryScreen(
  160. cropId: item.cropId ?? -1,
  161. activityId: item.id,
  162. isEdit: true,
  163. ));
  164. } else if (item.activityTypeName == "ACTIVE_TYPE_STATUS_CROP") {
  165. Get.to(EditActionCropStatusScreen(
  166. cropId: item.cropId ?? -1,
  167. activityId: item.id,
  168. isEdit: true,
  169. ));
  170. } else if (item.activityTypeName == "ACTIVE_TYPE_UPDATE_ENV") {
  171. Get.to(EditActionEnvironmentUpdate(
  172. cropId: item.cropId ?? -1,
  173. activityId: item.id,
  174. isEdit: true,
  175. ));
  176. } else if (item.activityTypeName == "ACTIVE_TYPE_PESTS_INVESTIGATION") {
  177. Get.to(EditActionDiseaseScreen(
  178. cropId: item.cropId ?? -1,
  179. activityId: item.id,
  180. isEdit: true,
  181. ));
  182. } else if (item.activityTypeName == "ACTIVE_TYPE_USE_WATER") {
  183. Get.to(EditActionUseWaterScreen(
  184. cropId: item.cropId ?? -1,
  185. activityId: item.id,
  186. isEdit: true,
  187. ));
  188. } else if (item.activityTypeName == "ACTIVE_TYPE_HARVEST") {
  189. Get.to(EditActionHarvestScreen(
  190. cropId: item.cropId ?? -1,
  191. activityId: item.id,
  192. isEdit: true,
  193. ));
  194. } else if (item.activityTypeName == "ACTIVE_TYPE_PACKING") {
  195. Get.to(EditActionPackingScreen(
  196. cropId: item.cropId ?? -1,
  197. activityId: item.id,
  198. isEdit: true,
  199. ));
  200. } else if (item.activityTypeName == "ACTIVE_TYPE_SELL") {
  201. Get.to(EditActionSellScreen(
  202. cropId: item.cropId ?? -1,
  203. activityId: item.id,
  204. isEdit: true,
  205. ));
  206. } else if (item.activityTypeName == "ACTIVE_TYPE_END") {
  207. Get.to(EditActionEndScreen(
  208. cropId: item.cropId ?? -1,
  209. activityId: item.id,
  210. isEdit: true,
  211. ));
  212. } else if (item.activityTypeName == "ACTIVE_TYPE_STATUS_GROW") {
  213. Get.to(EditActionPlantScreen(
  214. cropId: item.cropId ?? -1,
  215. activityId: item.id,
  216. isEdit: true,
  217. ));
  218. } else if (item.activityTypeName == "ACTIVE_TYPE_MANURING") {
  219. Get.to(EditActionDungScreen(
  220. cropId: item.cropId ?? -1,
  221. activityId: item.id,
  222. isEdit: true,
  223. ));
  224. } else if (item.activityTypeName == "ACTIVE_TYPE_SPRAY") {
  225. Get.to(EditActionSprayingScreen(
  226. cropId: item.cropId ?? -1,
  227. activityId: item.id,
  228. isEdit: true,
  229. ));
  230. } else if (item.activityTypeName == "ACTIVE_TYPE_PROCESS") {
  231. Get.to(EditActionHarvestProcessScreen(
  232. cropId: item.cropId ?? -1,
  233. activityId: item.id,
  234. isEdit: true,
  235. ));
  236. } else {
  237. Get.to(EditActionOtherScreen(
  238. cropId: item.cropId ?? -1,
  239. activityId: item.id,
  240. isEdit: true,
  241. ));
  242. }
  243. });
  244. }
  245. }
  246. class ActionType {
  247. Widget listScreen;
  248. String actionName;
  249. ActionType(
  250. this.listScreen,
  251. this.actionName,
  252. ) {
  253. this.actionName = actionName;
  254. this.listScreen = listScreen;
  255. }
  256. }