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.

201 lines
6.3KB

  1. import 'package:farm_tpf/custom_model/CropPlot.dart';
  2. import 'package:farm_tpf/data/repository/repository.dart';
  3. import 'package:farm_tpf/presentation/custom_widgets/bloc/widget_row_plot_info.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  6. import 'package:farm_tpf/presentation/screens/actions/environment_update/sc_edit_action_environment_update.dart';
  7. import 'package:farm_tpf/presentation/screens/actions/sc_action.dart';
  8. import 'package:farm_tpf/utils/const_color.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter_bloc/flutter_bloc.dart';
  11. import 'package:get/get.dart';
  12. import 'package:farm_tpf/utils/formatter.dart';
  13. import 'bloc/plot_detail_bloc.dart';
  14. class PlotHistoryScreen extends StatefulWidget {
  15. int cropId;
  16. String cropCode;
  17. int cropType;
  18. PlotHistoryScreen({this.cropId, this.cropCode, this.cropType});
  19. @override
  20. _PlotHistoryScreenState createState() => _PlotHistoryScreenState();
  21. }
  22. class _PlotHistoryScreenState extends State<PlotHistoryScreen>
  23. with AutomaticKeepAliveClientMixin {
  24. @override
  25. void initState() {
  26. super.initState();
  27. }
  28. @override
  29. void dispose() {
  30. super.dispose();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return InfinityView(
  35. cropId: widget.cropId,
  36. cropCode: widget.cropCode,
  37. );
  38. }
  39. @override
  40. bool get wantKeepAlive => true;
  41. }
  42. class InfinityView extends StatefulWidget {
  43. int cropId;
  44. String cropCode;
  45. InfinityView({this.cropId, this.cropCode});
  46. @override
  47. _InfinityViewState createState() => _InfinityViewState();
  48. }
  49. class _InfinityViewState extends State<InfinityView> {
  50. var plotDetailBloc = PlotDetailBloc(repository: Repository());
  51. final _scrollController = ScrollController();
  52. final _scrollThreshold = 250.0;
  53. @override
  54. void initState() {
  55. plotDetailBloc
  56. .add(DataFetched(cropId: widget.cropId, cropCode: widget.cropCode));
  57. _scrollController.addListener(() {
  58. final maxScroll = _scrollController.position.maxScrollExtent;
  59. final currentScroll = _scrollController.position.pixels;
  60. if (maxScroll - currentScroll < _scrollThreshold) {
  61. plotDetailBloc
  62. .add(DataFetched(cropId: widget.cropId, cropCode: widget.cropCode));
  63. }
  64. });
  65. super.initState();
  66. }
  67. @override
  68. Widget build(BuildContext context) {
  69. return BlocBuilder<PlotDetailBloc, PlotDetailState>(
  70. cubit: plotDetailBloc,
  71. builder: (context, state) {
  72. if (state is PlotDetailFailure) {
  73. return Center(child: Text(state.errorString));
  74. }
  75. if (state is PlotDetailSuccess) {
  76. if (state.items.isEmpty) {
  77. return Center(
  78. child: Column(
  79. mainAxisSize: MainAxisSize.max,
  80. crossAxisAlignment: CrossAxisAlignment.center,
  81. mainAxisAlignment: MainAxisAlignment.center,
  82. children: [
  83. Text("Không có dữ liệu lịch sử canh tác"),
  84. OutlineButton(
  85. onPressed: () {
  86. plotDetailBloc.add(OnRefresh(
  87. cropId: widget.cropId, cropCode: widget.cropCode));
  88. },
  89. child: Text('Tải lại'),
  90. )
  91. ],
  92. ));
  93. }
  94. List<Activities> currentItems = List<Activities>.from(state.items);
  95. return SafeArea(
  96. child: Scaffold(
  97. body: RefreshIndicator(
  98. child: ListView.builder(
  99. padding: EdgeInsets.all(8),
  100. physics: AlwaysScrollableScrollPhysics(),
  101. itemBuilder: (BuildContext context, int index) {
  102. return index >= state.items.length
  103. ? BottomLoader()
  104. : ItemInfinityWidget(
  105. currentItems: currentItems,
  106. item: state.items[index],
  107. index: index,
  108. currentPage: state.page,
  109. currentReachedMax: state.hasReachedMax);
  110. },
  111. itemCount: state.hasReachedMax
  112. ? state.items.length
  113. : state.items.length + 1,
  114. controller: _scrollController,
  115. ),
  116. onRefresh: () async {
  117. plotDetailBloc.add(OnRefresh(
  118. cropId: widget.cropId, cropCode: widget.cropCode));
  119. }),
  120. ),
  121. );
  122. }
  123. return Center(
  124. child: LoadingListPage(),
  125. );
  126. },
  127. );
  128. }
  129. @override
  130. void dispose() {
  131. _scrollController.dispose();
  132. super.dispose();
  133. }
  134. }
  135. class ItemInfinityWidget extends StatelessWidget {
  136. final List<Activities> currentItems;
  137. final Activities item;
  138. final int currentPage;
  139. final bool currentReachedMax;
  140. final int index;
  141. const ItemInfinityWidget(
  142. {Key key,
  143. @required this.currentItems,
  144. @required this.item,
  145. @required this.currentPage,
  146. @required this.currentReachedMax,
  147. @required this.index})
  148. : super(key: key);
  149. @override
  150. Widget build(BuildContext context) {
  151. return GestureDetector(
  152. child: WidgetRowPlotInfo(
  153. color: index % 2 == 0
  154. ? AppColors.DEFAULT.withOpacity(0.3)
  155. : AppColors.DEFAULT.withOpacity(0.1),
  156. name: '${item.activityTypeDescription ?? ''}',
  157. value: item.executeDate.format_DDMMYY_HHmm() ?? ''),
  158. onTap: () {
  159. if (item.activityTypeName == 'ACTIVE_TYPE_UPDATE_ENV') {
  160. Get.to(EditActionEnvironmentUpdate(
  161. cropId: item.cropId,
  162. activityId: item.id,
  163. isEdit: true,
  164. ));
  165. } else {
  166. Get.to(ActionScreen(
  167. isEdit: true,
  168. cropId: item.cropId,
  169. activityId: item.id,
  170. activityType: item.activityTypeName,
  171. title: 'ActionScreen',
  172. idAction: item.activityTypeId,
  173. ));
  174. }
  175. });
  176. }
  177. }
  178. class ActionType {
  179. Widget listScreen;
  180. String actionName;
  181. ActionType(String actionName, Widget listScreen) {
  182. this.actionName = actionName;
  183. this.listScreen = listScreen;
  184. }
  185. }