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.

527 lines
19KB

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