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.

472 lines
17KB

  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/bottom_loader.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  5. import 'package:farm_tpf/presentation/screens/actions/crop_status/sc_edit_action_crop_status.dart';
  6. import 'package:farm_tpf/presentation/screens/actions/disease/sc_edit_action_disease.dart';
  7. import 'package:farm_tpf/presentation/screens/actions/dung/sc_edit_action_dung.dart';
  8. import 'package:farm_tpf/presentation/screens/actions/end/sc_edit_action_end.dart';
  9. import 'package:farm_tpf/presentation/screens/actions/environment_update/sc_edit_action_environment_update.dart';
  10. import 'package:farm_tpf/presentation/screens/actions/harvest/sc_edit_action_harvest.dart';
  11. import 'package:farm_tpf/presentation/screens/actions/harvest_process/sc_edit_action_harvest_process.dart';
  12. import 'package:farm_tpf/presentation/screens/actions/nursery/sc_edit_action_nursery.dart';
  13. import 'package:farm_tpf/presentation/screens/actions/other/sc_edit_action_other.dart';
  14. import 'package:farm_tpf/presentation/screens/actions/packing/sc_edit_action_packing.dart';
  15. import 'package:farm_tpf/presentation/screens/actions/plant/sc_edit_action_plant.dart';
  16. import 'package:farm_tpf/presentation/screens/actions/sell/sc_edit_action_sell.dart';
  17. import 'package:farm_tpf/presentation/screens/actions/spraying/sc_edit_action_spraying.dart';
  18. import 'package:farm_tpf/presentation/screens/actions/use_water/sc_edit_action_user_water.dart';
  19. import 'package:farm_tpf/presentation/screens/plot/sc_plot.dart';
  20. import 'package:farm_tpf/presentation/screens/plot_detail/bloc/plot_detail_bloc.dart';
  21. import 'package:farm_tpf/utils/const_color.dart';
  22. import 'package:farm_tpf/utils/const_string.dart';
  23. import 'package:flutter/material.dart';
  24. import 'package:flutter_bloc/flutter_bloc.dart';
  25. import 'package:farm_tpf/utils/formatter.dart';
  26. import 'package:get/get.dart';
  27. class PlotActionScreen extends StatefulWidget {
  28. int cropId;
  29. String cropCode;
  30. int cropType;
  31. PlotActionScreen({this.cropId, this.cropCode, this.cropType});
  32. @override
  33. _PlotActionScreenState createState() => _PlotActionScreenState();
  34. }
  35. class _PlotActionScreenState extends State<PlotActionScreen> {
  36. List<ActionType> actions = new List<ActionType>();
  37. ScrollController _scrollController;
  38. var blocPlotDetail = PlotDetailBloc(repository: Repository());
  39. @override
  40. void initState() {
  41. super.initState();
  42. _scrollController = ScrollController()..addListener(() => setState(() {}));
  43. _initActionButtons();
  44. }
  45. @override
  46. void dispose() {
  47. blocPlotDetail.close();
  48. super.dispose();
  49. }
  50. _initActionButtons() {
  51. //type: 0- Trồng, 1- ướm
  52. if (widget.cropType == 1) {
  53. actions.add(ActionType(
  54. plot_action_nursery,
  55. EditActionNurseryScreen(
  56. cropId: widget.cropId,
  57. )));
  58. } else if (widget.cropType == 0) {
  59. actions.add(ActionType(
  60. plot_action_plant,
  61. EditActionPlantScreen(
  62. cropId: widget.cropId,
  63. )));
  64. }
  65. actions.add(ActionType(
  66. plot_action_crop_status,
  67. EditActionCropStatusScreen(
  68. cropId: widget.cropId,
  69. )));
  70. actions.add(ActionType(
  71. plot_action_environment_update,
  72. EditActionEnvironmentUpdate(
  73. cropId: widget.cropId,
  74. )));
  75. actions.add(ActionType(
  76. plot_action_dung,
  77. EditActionDungScreen(
  78. cropId: widget.cropId,
  79. )));
  80. actions.add(ActionType(
  81. plot_action_spraying,
  82. EditActionSprayingScreen(
  83. cropId: widget.cropId,
  84. )));
  85. actions.add(ActionType(
  86. plot_action_disease,
  87. EditActionDiseaseScreen(
  88. cropId: widget.cropId,
  89. )));
  90. actions.add(ActionType(
  91. plot_action_use_water,
  92. EditActionUseWaterScreen(
  93. cropId: widget.cropId,
  94. )));
  95. actions.add(ActionType(
  96. plot_action_other,
  97. EditActionOtherScreen(
  98. cropId: widget.cropId,
  99. )));
  100. actions.add(ActionType(
  101. plot_action_harvest,
  102. EditActionHarvestScreen(
  103. cropId: widget.cropId,
  104. )));
  105. actions.add(ActionType(
  106. plot_action_finish,
  107. EditActionEndScreen(
  108. cropId: widget.cropId,
  109. )));
  110. }
  111. Widget _createActionButtons(ActionType actionType, BuildContext _context) {
  112. return BlocBuilder<PlotDetailBloc, PlotDetailState>(
  113. cubit: blocPlotDetail,
  114. builder: (context, state) {
  115. return GestureDetector(
  116. onTap: () {
  117. Navigator.of(context)
  118. .push(MaterialPageRoute(
  119. builder: (context) => actionType.listScreen))
  120. .then((value) {
  121. if (value != null) {
  122. try {
  123. blocPlotDetail.add(OnRefresh(cropId: widget.cropId));
  124. } catch (e) {
  125. print(e);
  126. }
  127. }
  128. });
  129. },
  130. child: Container(
  131. margin: EdgeInsets.all(2.5),
  132. decoration: BoxDecoration(
  133. color: COLOR_CONST.WHITE,
  134. borderRadius: BorderRadius.all(Radius.circular(4.0)),
  135. boxShadow: <BoxShadow>[
  136. BoxShadow(
  137. color: COLOR_CONST.GRAY1.withOpacity(0.2),
  138. offset: Offset(1.1, 1.1),
  139. blurRadius: 4.0),
  140. ],
  141. ),
  142. child: Align(
  143. alignment: Alignment.center,
  144. child: Text(
  145. actionType.actionName,
  146. textAlign: TextAlign.center,
  147. style: TextStyle(
  148. fontWeight: FontWeight.w400,
  149. fontSize: 11,
  150. color: COLOR_CONST.BLACK2,
  151. ),
  152. )),
  153. ));
  154. });
  155. }
  156. bool _showTitle(BuildContext context) {
  157. var kExpandedHeight = MediaQuery.of(context).size.width * 0.625 + 24;
  158. return _scrollController.hasClients &&
  159. _scrollController.offset > kExpandedHeight - kToolbarHeight;
  160. }
  161. @override
  162. Widget build(BuildContext context) {
  163. return BlocBuilder<PlotDetailBloc, PlotDetailState>(
  164. cubit: blocPlotDetail,
  165. builder: (context, state) {
  166. return NestedScrollView(
  167. controller: _scrollController,
  168. headerSliverBuilder: (context, innerBoxScrolled) => [
  169. SliverAppBar(
  170. floating: false,
  171. pinned: false,
  172. backgroundColor: Colors.white,
  173. leading: Container(),
  174. title: null,
  175. // title: _showTitle(context) ? Text(plot_detail_title) : null,
  176. //Height flexibleSpace : WidthScreen /2 * 4/16 * 5(row) + 8(space) *3
  177. expandedHeight:
  178. MediaQuery.of(context).size.width * 0.625 + 24,
  179. flexibleSpace: _showTitle(context)
  180. ? null
  181. : BlocBuilder<PlotDetailBloc, PlotDetailState>(
  182. cubit: blocPlotDetail,
  183. builder: (contextB, state) {
  184. return FlexibleSpaceBar(
  185. centerTitle: true,
  186. title: GridView.count(
  187. shrinkWrap: true,
  188. crossAxisCount: 2,
  189. childAspectRatio: 16 / 4,
  190. children: actions.map(
  191. (item) {
  192. return _createActionButtons(
  193. item, contextB);
  194. },
  195. ).toList()),
  196. );
  197. })),
  198. ],
  199. body: BlocBuilder<PlotDetailBloc, PlotDetailState>(
  200. cubit: blocPlotDetail,
  201. builder: (context, state) {
  202. return InfinityView(
  203. cropId: widget.cropId,
  204. parentScroll: _scrollController,
  205. plotDetailBloc: blocPlotDetail,
  206. );
  207. }),
  208. );
  209. });
  210. }
  211. }
  212. class InfinityView extends StatefulWidget {
  213. int cropId;
  214. String cropCode;
  215. ScrollController parentScroll;
  216. PlotDetailBloc plotDetailBloc;
  217. InfinityView(
  218. {this.cropId,
  219. this.cropCode,
  220. @required this.parentScroll,
  221. @required this.plotDetailBloc});
  222. @override
  223. _InfinityViewState createState() => _InfinityViewState();
  224. }
  225. class _InfinityViewState extends State<InfinityView> {
  226. final _scrollController = ScrollController();
  227. final _scrollThreshold = 250.0;
  228. var heightOfActionButton = Get.width * 0.625 + 24;
  229. @override
  230. void initState() {
  231. widget.plotDetailBloc
  232. .add(DataFetched(cropId: widget.cropId, cropCode: widget.cropCode));
  233. _scrollController.addListener(() {
  234. final maxScroll = _scrollController.position.maxScrollExtent;
  235. final currentScroll = _scrollController.position.pixels;
  236. if (maxScroll - currentScroll < _scrollThreshold) {
  237. widget.plotDetailBloc
  238. .add(DataFetched(cropId: widget.cropId, cropCode: widget.cropCode));
  239. }
  240. });
  241. super.initState();
  242. }
  243. @override
  244. Widget build(BuildContext context) {
  245. return BlocBuilder<PlotDetailBloc, PlotDetailState>(
  246. cubit: widget.plotDetailBloc,
  247. builder: (context, state) {
  248. if (state is PlotDetailFailure) {
  249. return Center(child: Text(state.errorString));
  250. }
  251. if (state is PlotDetailSuccess) {
  252. if (state.items.isEmpty) {
  253. return Center(child: Text(label_list_empty));
  254. }
  255. List<Activities> currentItems = List<Activities>.from(state.items);
  256. return RefreshIndicator(
  257. child: Column(
  258. children: [
  259. Container(
  260. height: 40,
  261. alignment: Alignment.center,
  262. decoration: BoxDecoration(
  263. color: COLOR_CONST.WHITE_50,
  264. border: Border(
  265. top: BorderSide(
  266. width: 0.5, color: COLOR_CONST.DEFAULT),
  267. bottom: BorderSide(
  268. width: 0.5, color: COLOR_CONST.DEFAULT)),
  269. ),
  270. child: Row(
  271. children: [
  272. Expanded(
  273. child: Text(
  274. plot_detail_title,
  275. textAlign: TextAlign.center,
  276. style: TextStyle(
  277. fontSize: 20, fontWeight: FontWeight.normal),
  278. )),
  279. IconButton(
  280. icon: (widget.parentScroll.position.pixels <
  281. heightOfActionButton)
  282. ? Icon(Icons.arrow_upward)
  283. : Icon(Icons.arrow_downward),
  284. onPressed: () {
  285. if (widget.parentScroll.position.pixels <
  286. heightOfActionButton) {
  287. widget.parentScroll.animateTo(
  288. heightOfActionButton,
  289. duration: Duration(milliseconds: 500),
  290. curve: Curves.easeOut);
  291. } else {
  292. widget.parentScroll.animateTo(0.0,
  293. duration: Duration(milliseconds: 500),
  294. curve: Curves.easeOut);
  295. }
  296. })
  297. ],
  298. )),
  299. Expanded(
  300. child: ListView.builder(
  301. physics: AlwaysScrollableScrollPhysics(),
  302. itemBuilder: (BuildContext context, int index) {
  303. return index >= state.items.length
  304. ? BottomLoader()
  305. : ItemInfinityWidget(
  306. currentItems: currentItems,
  307. item: state.items[index],
  308. currentPage: state.page,
  309. currentReachedMax: state.hasReachedMax);
  310. },
  311. itemCount: state.hasReachedMax
  312. ? state.items.length
  313. : state.items.length + 1,
  314. controller: _scrollController,
  315. ))
  316. ],
  317. ),
  318. onRefresh: () async {
  319. widget.plotDetailBloc.add(OnRefresh(
  320. cropId: widget.cropId, cropCode: widget.cropCode));
  321. });
  322. }
  323. return Center(
  324. child: LoadingListPage(),
  325. );
  326. },
  327. );
  328. }
  329. @override
  330. void dispose() {
  331. _scrollController.dispose();
  332. super.dispose();
  333. }
  334. }
  335. class ItemInfinityWidget extends StatelessWidget {
  336. final List<Activities> currentItems;
  337. final Activities item;
  338. final int currentPage;
  339. final bool currentReachedMax;
  340. const ItemInfinityWidget(
  341. {Key key,
  342. @required this.currentItems,
  343. @required this.item,
  344. @required this.currentPage,
  345. @required this.currentReachedMax})
  346. : super(key: key);
  347. @override
  348. Widget build(BuildContext context) {
  349. return GestureDetector(
  350. child: Card(
  351. child: ListTile(
  352. title: Text(item.activityTypeDescription ?? ''),
  353. subtitle: Text(item.executeDate.format_DDMMYY_HHmm()),
  354. ),
  355. ),
  356. onTap: () {
  357. if (item.activityTypeName == "ACTIVE_TYPE_NURSERY") {
  358. Get.to(EditActionNurseryScreen(
  359. cropId: item.cropId,
  360. activityId: item.id,
  361. isEdit: true,
  362. ));
  363. } else if (item.activityTypeName == "ACTIVE_TYPE_STATUS_CROP") {
  364. Get.to(EditActionCropStatusScreen(
  365. cropId: item.cropId,
  366. activityId: item.id,
  367. isEdit: true,
  368. ));
  369. } else if (item.activityTypeName == "ACTIVE_TYPE_UPDATE_ENV") {
  370. Get.to(EditActionEnvironmentUpdate(
  371. cropId: item.cropId,
  372. activityId: item.id,
  373. isEdit: true,
  374. ));
  375. } else if (item.activityTypeName ==
  376. "ACTIVE_TYPE_PESTS_INVESTIGATION") {
  377. Get.to(EditActionDiseaseScreen(
  378. cropId: item.cropId,
  379. activityId: item.id,
  380. isEdit: true,
  381. ));
  382. } else if (item.activityTypeName == "ACTIVE_TYPE_USE_WATER") {
  383. Get.to(EditActionUseWaterScreen(
  384. cropId: item.cropId,
  385. activityId: item.id,
  386. isEdit: true,
  387. ));
  388. } else if (item.activityTypeName == "ACTIVE_TYPE_HARVEST") {
  389. Get.to(EditActionHarvestScreen(
  390. cropId: item.cropId,
  391. activityId: item.id,
  392. isEdit: true,
  393. ));
  394. } else if (item.activityTypeName == "ACTIVE_TYPE_PACKING") {
  395. Get.to(EditActionPackingScreen(
  396. cropId: item.cropId,
  397. activityId: item.id,
  398. isEdit: true,
  399. ));
  400. } else if (item.activityTypeName == "ACTIVE_TYPE_SELL") {
  401. Get.to(EditActionSellScreen(
  402. cropId: item.cropId,
  403. activityId: item.id,
  404. isEdit: true,
  405. ));
  406. } else if (item.activityTypeName == "ACTIVE_TYPE_END") {
  407. Get.to(EditActionEndScreen(
  408. cropId: item.cropId,
  409. activityId: item.id,
  410. isEdit: true,
  411. ));
  412. } else if (item.activityTypeName == "ACTIVE_TYPE_STATUS_GROW") {
  413. Get.to(EditActionPlantScreen(
  414. cropId: item.cropId,
  415. activityId: item.id,
  416. isEdit: true,
  417. ));
  418. } else if (item.activityTypeName == "ACTIVE_TYPE_MANURING") {
  419. Get.to(EditActionDungScreen(
  420. cropId: item.cropId,
  421. activityId: item.id,
  422. isEdit: true,
  423. ));
  424. } else if (item.activityTypeName == "ACTIVE_TYPE_SPRAY") {
  425. Get.to(EditActionSprayingScreen(
  426. cropId: item.cropId,
  427. activityId: item.id,
  428. isEdit: true,
  429. ));
  430. } else if (item.activityTypeName == "ACTIVE_TYPE_PROCESS") {
  431. Get.to(EditActionHarvestProcessScreen(
  432. cropId: item.cropId,
  433. activityId: item.id,
  434. isEdit: true,
  435. ));
  436. } else {
  437. Get.to(EditActionOtherScreen(
  438. cropId: item.cropId,
  439. activityId: item.id,
  440. isEdit: true,
  441. ));
  442. }
  443. });
  444. }
  445. }
  446. class ActionType {
  447. Widget listScreen;
  448. String actionName;
  449. ActionType(String actionName, Widget listScreen) {
  450. this.actionName = actionName;
  451. this.listScreen = listScreen;
  452. }
  453. }