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.

496 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 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. // print("height: $kExpandedHeight");
  154. // print('offset ${_scrollController.offset}');
  155. return _scrollController.hasClients &&
  156. _scrollController.offset > kExpandedHeight - kToolbarHeight;
  157. }
  158. @override
  159. Widget build(BuildContext context) {
  160. return NestedScrollView(
  161. controller: _scrollController,
  162. headerSliverBuilder: (context, innerBoxScrolled) => [
  163. SliverAppBar(
  164. floating: false,
  165. pinned: false,
  166. backgroundColor: Colors.white,
  167. leading: Container(),
  168. title: null,
  169. // title: _showTitle(context) ? Text(plot_detail_title) : null,
  170. //Height flexibleSpace : WidthScreen /2 * 4/16 * 5(row) + 8(space) *3
  171. expandedHeight: MediaQuery.of(context).size.width * 0.625 + 24,
  172. flexibleSpace: _showTitle(context)
  173. ? null
  174. : BlocProvider<PlotDetailBloc>(
  175. create: (context) => PlotDetailBloc(repository: Repository()),
  176. child: BlocBuilder<PlotDetailBloc, PlotDetailState>(
  177. builder: (contextB, state) {
  178. return FlexibleSpaceBar(
  179. centerTitle: true,
  180. title: GridView.count(
  181. shrinkWrap: true,
  182. crossAxisCount: 2,
  183. childAspectRatio: 16 / 4,
  184. children: actions.map(
  185. (item) {
  186. return _createActionButtons(item, contextB);
  187. },
  188. ).toList()),
  189. );
  190. }),
  191. ),
  192. ),
  193. ],
  194. body: BlocProvider(
  195. create: (context) => PlotDetailBloc(repository: Repository())
  196. ..add(DataFetched(cropId: widget.cropId, cropCode: widget.cropCode)),
  197. child: HoldInfinityWidget(
  198. cropId: widget.cropId,
  199. cropCode: widget.cropCode,
  200. ),
  201. ),
  202. );
  203. }
  204. }
  205. class HoldInfinityWidget extends StatelessWidget {
  206. int cropId;
  207. String cropCode;
  208. HoldInfinityWidget({this.cropId, this.cropCode});
  209. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  210. @override
  211. Widget build(BuildContext context) {
  212. return Scaffold(key: _scaffoldKey, body: InfinityView(cropId: cropId));
  213. }
  214. }
  215. class InfinityView extends StatefulWidget {
  216. int cropId;
  217. String cropCode;
  218. InfinityView({this.cropId, this.cropCode});
  219. @override
  220. _InfinityViewState createState() => _InfinityViewState();
  221. }
  222. class _InfinityViewState extends State<InfinityView> {
  223. final _scrollController = ScrollController();
  224. final _scrollThreshold = 250.0;
  225. PlotDetailBloc _plotDetailBloc;
  226. var a = Get.put(ChangeToRefreshLists());
  227. @override
  228. void initState() {
  229. _scrollController.addListener(() {
  230. final maxScroll = _scrollController.position.maxScrollExtent;
  231. final currentScroll = _scrollController.position.pixels;
  232. if (maxScroll - currentScroll < _scrollThreshold) {
  233. _plotDetailBloc
  234. .add(DataFetched(cropId: widget.cropId, cropCode: widget.cropCode));
  235. }
  236. });
  237. _plotDetailBloc = BlocProvider.of<PlotDetailBloc>(context);
  238. super.initState();
  239. }
  240. @override
  241. Widget build(BuildContext context) {
  242. return BlocBuilder<PlotDetailBloc, PlotDetailState>(
  243. builder: (context, state) {
  244. if (state is PlotDetailFailure) {
  245. return Center(child: Text(state.errorString));
  246. }
  247. if (state is PlotDetailSuccess) {
  248. if (state.items.isEmpty) {
  249. return Center(child: Text(label_list_empty));
  250. }
  251. List<Activities> currentItems = List<Activities>.from(state.items);
  252. Get.find<ChangeToRefreshLists>()
  253. .updateValue(currentItems, state.page, state.hasReachedMax);
  254. return RefreshIndicator(
  255. child: Column(
  256. children: [
  257. Container(
  258. height: 40,
  259. alignment: Alignment.center,
  260. decoration: BoxDecoration(
  261. color: COLOR_CONST.WHITE_50,
  262. border: Border(
  263. top: BorderSide(
  264. width: 0.5, color: COLOR_CONST.DEFAULT),
  265. bottom: BorderSide(
  266. width: 0.5, color: COLOR_CONST.DEFAULT)),
  267. ),
  268. child: Text(
  269. plot_detail_title,
  270. style: TextStyle(
  271. fontSize: 20, fontWeight: FontWeight.normal),
  272. )),
  273. Expanded(
  274. child: ListView.builder(
  275. physics: AlwaysScrollableScrollPhysics(),
  276. itemBuilder: (BuildContext context, int index) {
  277. return index >= state.items.length
  278. ? BottomLoader()
  279. : ItemInfinityWidget(
  280. currentItems: currentItems,
  281. item: state.items[index],
  282. currentPage: state.page,
  283. currentReachedMax: state.hasReachedMax);
  284. },
  285. itemCount: state.hasReachedMax
  286. ? state.items.length
  287. : state.items.length + 1,
  288. controller: _scrollController,
  289. ))
  290. ],
  291. ),
  292. onRefresh: () async {
  293. _plotDetailBloc.add(OnRefresh(
  294. cropId: widget.cropId, cropCode: widget.cropCode));
  295. });
  296. }
  297. return Center(
  298. child: LoadingListPage(),
  299. );
  300. },
  301. );
  302. }
  303. @override
  304. void dispose() {
  305. _scrollController.dispose();
  306. super.dispose();
  307. }
  308. }
  309. class ItemInfinityWidget extends StatelessWidget {
  310. final List<Activities> currentItems;
  311. final Activities item;
  312. final int currentPage;
  313. final bool currentReachedMax;
  314. const ItemInfinityWidget(
  315. {Key key,
  316. @required this.currentItems,
  317. @required this.item,
  318. @required this.currentPage,
  319. @required this.currentReachedMax})
  320. : super(key: key);
  321. @override
  322. Widget build(BuildContext context) {
  323. return GestureDetector(
  324. child: Card(
  325. child: ListTile(
  326. title: Text(item.activityTypeDescription ?? ''),
  327. subtitle: Text(item.executeDate.format_DDMMYY_HHmm()),
  328. //TODO: remove when release
  329. // trailing: Text(item.id.toString()),
  330. ),
  331. ),
  332. onTap: () {
  333. if (item.activityTypeName == "ACTIVE_TYPE_NURSERY") {
  334. Get.to(EditActionNurseryScreen(
  335. cropId: item.cropId,
  336. activityId: item.id,
  337. isEdit: true,
  338. )).then((value) {
  339. if (value != null) {
  340. try {
  341. //TODO: refresh when edit activity
  342. // BlocProvider.of<PlotDetailBloc>(context)
  343. // .add(OnRefresh(cropId: item.cropId));
  344. // var updatedItem = Activities.fromJson(value);
  345. // List<Activities> updatedItems = new List<Activities>();
  346. // currentItems.forEach((e) {
  347. // if (e.id == updatedItem.id) {
  348. // e.executeDate = updatedItem.executeDate;
  349. // } else {
  350. // //
  351. // }
  352. // updatedItems.add(Activities.clone(e));
  353. // });
  354. // BlocProvider.of<PlotDetailBloc>(context).add(
  355. // OnUpdate<Activities>(
  356. // currentItems: updatedItems,
  357. // currentPage: currentPage,
  358. // hasReachedMax: currentReachedMax));
  359. } catch (e) {
  360. print(e.toString());
  361. }
  362. }
  363. });
  364. } else if (item.activityTypeName == "ACTIVE_TYPE_STATUS_CROP") {
  365. Get.to(EditActionCropStatusScreen(
  366. cropId: item.cropId,
  367. activityId: item.id,
  368. isEdit: true,
  369. ));
  370. } else if (item.activityTypeName == "ACTIVE_TYPE_UPDATE_ENV") {
  371. Get.to(EditActionEnvironmentUpdate(
  372. cropId: item.cropId,
  373. activityId: item.id,
  374. isEdit: true,
  375. ));
  376. } else if (item.activityTypeName ==
  377. "ACTIVE_TYPE_PESTS_INVESTIGATION") {
  378. Get.to(EditActionDiseaseScreen(
  379. cropId: item.cropId,
  380. activityId: item.id,
  381. isEdit: true,
  382. ));
  383. } else if (item.activityTypeName == "ACTIVE_TYPE_USE_WATER") {
  384. Get.to(EditActionUseWaterScreen(
  385. cropId: item.cropId,
  386. activityId: item.id,
  387. isEdit: true,
  388. ));
  389. } else if (item.activityTypeName == "ACTIVE_TYPE_HARVEST") {
  390. Get.to(EditActionHarvestScreen(
  391. cropId: item.cropId,
  392. activityId: item.id,
  393. isEdit: true,
  394. ));
  395. } else if (item.activityTypeName == "ACTIVE_TYPE_PACKING") {
  396. Get.to(EditActionPackingScreen(
  397. cropId: item.cropId,
  398. activityId: item.id,
  399. isEdit: true,
  400. ));
  401. } else if (item.activityTypeName == "ACTIVE_TYPE_SELL") {
  402. Get.to(EditActionSellScreen(
  403. cropId: item.cropId,
  404. activityId: item.id,
  405. isEdit: true,
  406. ));
  407. } else if (item.activityTypeName == "ACTIVE_TYPE_END") {
  408. Get.to(EditActionEndScreen(
  409. cropId: item.cropId,
  410. activityId: item.id,
  411. isEdit: true,
  412. ));
  413. } else if (item.activityTypeName == "ACTIVE_TYPE_STATUS_GROW") {
  414. Get.to(EditActionPlantScreen(
  415. cropId: item.cropId,
  416. activityId: item.id,
  417. isEdit: true,
  418. ));
  419. } else if (item.activityTypeName == "ACTIVE_TYPE_MANURING") {
  420. Get.to(EditActionDungScreen(
  421. cropId: item.cropId,
  422. activityId: item.id,
  423. isEdit: true,
  424. ));
  425. } else if (item.activityTypeName == "ACTIVE_TYPE_SPRAY") {
  426. Get.to(EditActionSprayingScreen(
  427. cropId: item.cropId,
  428. activityId: item.id,
  429. isEdit: true,
  430. ));
  431. } else if (item.activityTypeName == "ACTIVE_TYPE_PROCESS") {
  432. Get.to(EditActionHarvestProcessScreen(
  433. cropId: item.cropId,
  434. activityId: item.id,
  435. isEdit: true,
  436. ));
  437. } else {
  438. //TODO: Check other types
  439. Get.to(EditActionOtherScreen(
  440. cropId: item.cropId,
  441. activityId: item.id,
  442. isEdit: true,
  443. ));
  444. }
  445. });
  446. }
  447. }
  448. class ActionType {
  449. Widget listScreen;
  450. String actionName;
  451. ActionType(String actionName, Widget listScreen) {
  452. this.actionName = actionName;
  453. this.listScreen = listScreen;
  454. }
  455. }
  456. class ChangeToRefreshLists extends GetxController {
  457. List<Activities> currentItems;
  458. int currentPage;
  459. bool currentReachedMax;
  460. void initValue() {
  461. currentItems = List<Activities>();
  462. currentPage = 0;
  463. currentReachedMax = true;
  464. update();
  465. }
  466. void updateValue(List<Activities> updateItems, int page, bool reachedMax) {
  467. currentItems = updateItems;
  468. currentPage = page;
  469. currentReachedMax = reachedMax;
  470. update();
  471. }
  472. }