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.

479 lines
18KB

  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. physics: NeverScrollableScrollPhysics(),
  188. shrinkWrap: true,
  189. crossAxisCount: 2,
  190. childAspectRatio: 16 / 4,
  191. children: actions.map(
  192. (item) {
  193. return _createActionButtons(
  194. item, contextB);
  195. },
  196. ).toList()),
  197. );
  198. })),
  199. ],
  200. body: BlocBuilder<PlotDetailBloc, PlotDetailState>(
  201. cubit: blocPlotDetail,
  202. builder: (context, state) {
  203. return InfinityView(
  204. cropId: widget.cropId,
  205. parentScroll: _scrollController,
  206. plotDetailBloc: blocPlotDetail,
  207. );
  208. }),
  209. );
  210. });
  211. }
  212. }
  213. class InfinityView extends StatefulWidget {
  214. int cropId;
  215. String cropCode;
  216. ScrollController parentScroll;
  217. PlotDetailBloc plotDetailBloc;
  218. InfinityView(
  219. {this.cropId,
  220. this.cropCode,
  221. @required this.parentScroll,
  222. @required this.plotDetailBloc});
  223. @override
  224. _InfinityViewState createState() => _InfinityViewState();
  225. }
  226. class _InfinityViewState extends State<InfinityView> {
  227. final _scrollController = ScrollController();
  228. final _scrollThreshold = 250.0;
  229. var heightOfActionButton = Get.width * 0.625 + 24;
  230. @override
  231. void initState() {
  232. widget.plotDetailBloc
  233. .add(DataFetched(cropId: widget.cropId, cropCode: widget.cropCode));
  234. _scrollController.addListener(() {
  235. final maxScroll = _scrollController.position.maxScrollExtent;
  236. final currentScroll = _scrollController.position.pixels;
  237. if (maxScroll - currentScroll < _scrollThreshold) {
  238. widget.plotDetailBloc
  239. .add(DataFetched(cropId: widget.cropId, cropCode: widget.cropCode));
  240. }
  241. });
  242. super.initState();
  243. }
  244. @override
  245. Widget build(BuildContext context) {
  246. return BlocBuilder<PlotDetailBloc, PlotDetailState>(
  247. cubit: widget.plotDetailBloc,
  248. builder: (context, state) {
  249. if (state is PlotDetailFailure) {
  250. return Center(child: Text(state.errorString));
  251. }
  252. if (state is PlotDetailSuccess) {
  253. if (state.items.isEmpty) {
  254. return Center(child: Text(label_list_empty));
  255. }
  256. List<Activities> currentItems = List<Activities>.from(state.items);
  257. return Scaffold(
  258. resizeToAvoidBottomInset: false,
  259. body: ListView(
  260. children: [
  261. Container(
  262. alignment: Alignment.center,
  263. decoration: BoxDecoration(
  264. color: COLOR_CONST.WHITE_50,
  265. border: Border(
  266. top: BorderSide(
  267. width: 0.5, color: COLOR_CONST.DEFAULT),
  268. bottom: BorderSide(
  269. width: 0.5, color: COLOR_CONST.DEFAULT)),
  270. ),
  271. child: Container(
  272. height: 40,
  273. child: Row(
  274. children: [
  275. Expanded(
  276. child: Text(
  277. plot_detail_title,
  278. textAlign: TextAlign.center,
  279. style: TextStyle(
  280. fontSize: 20, fontWeight: FontWeight.normal),
  281. )),
  282. GestureDetector(
  283. child: (widget.parentScroll.position.pixels <
  284. heightOfActionButton)
  285. ? Icon(Icons.arrow_upward)
  286. : Icon(Icons.arrow_downward_outlined),
  287. onTap: () {
  288. if (widget.parentScroll.position.pixels <
  289. heightOfActionButton) {
  290. widget.parentScroll.animateTo(
  291. heightOfActionButton,
  292. duration: Duration(milliseconds: 350),
  293. curve: Curves.easeOut);
  294. } else {
  295. widget.parentScroll.animateTo(0.0,
  296. duration: Duration(milliseconds: 350),
  297. curve: Curves.easeOut);
  298. }
  299. }),
  300. ],
  301. ),
  302. ),
  303. ),
  304. Expanded(
  305. child: RefreshIndicator(
  306. child: ListView.builder(
  307. physics: AlwaysScrollableScrollPhysics(),
  308. itemBuilder: (BuildContext context, int index) {
  309. return index >= state.items.length
  310. ? BottomLoader()
  311. : ItemInfinityWidget(
  312. currentItems: currentItems,
  313. item: state.items[index],
  314. currentPage: state.page,
  315. currentReachedMax: state.hasReachedMax);
  316. },
  317. itemCount: state.hasReachedMax
  318. ? state.items.length
  319. : state.items.length + 1,
  320. controller: _scrollController,
  321. ),
  322. onRefresh: () async {
  323. widget.plotDetailBloc.add(OnRefresh(
  324. cropId: widget.cropId,
  325. cropCode: widget.cropCode));
  326. }))
  327. ],
  328. ));
  329. }
  330. return Center(
  331. child: LoadingListPage(),
  332. );
  333. },
  334. );
  335. }
  336. @override
  337. void dispose() {
  338. _scrollController.dispose();
  339. super.dispose();
  340. }
  341. }
  342. class ItemInfinityWidget extends StatelessWidget {
  343. final List<Activities> currentItems;
  344. final Activities item;
  345. final int currentPage;
  346. final bool currentReachedMax;
  347. const ItemInfinityWidget(
  348. {Key key,
  349. @required this.currentItems,
  350. @required this.item,
  351. @required this.currentPage,
  352. @required this.currentReachedMax})
  353. : super(key: key);
  354. @override
  355. Widget build(BuildContext context) {
  356. return GestureDetector(
  357. child: Card(
  358. child: ListTile(
  359. title: Text(item.activityTypeDescription ?? ''),
  360. subtitle: Text(item.executeDate.format_DDMMYY_HHmm()),
  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. ));
  370. } else if (item.activityTypeName == "ACTIVE_TYPE_STATUS_CROP") {
  371. Get.to(EditActionCropStatusScreen(
  372. cropId: item.cropId,
  373. activityId: item.id,
  374. isEdit: true,
  375. ));
  376. } else if (item.activityTypeName == "ACTIVE_TYPE_UPDATE_ENV") {
  377. Get.to(EditActionEnvironmentUpdate(
  378. cropId: item.cropId,
  379. activityId: item.id,
  380. isEdit: true,
  381. ));
  382. } else if (item.activityTypeName ==
  383. "ACTIVE_TYPE_PESTS_INVESTIGATION") {
  384. Get.to(EditActionDiseaseScreen(
  385. cropId: item.cropId,
  386. activityId: item.id,
  387. isEdit: true,
  388. ));
  389. } else if (item.activityTypeName == "ACTIVE_TYPE_USE_WATER") {
  390. Get.to(EditActionUseWaterScreen(
  391. cropId: item.cropId,
  392. activityId: item.id,
  393. isEdit: true,
  394. ));
  395. } else if (item.activityTypeName == "ACTIVE_TYPE_HARVEST") {
  396. Get.to(EditActionHarvestScreen(
  397. cropId: item.cropId,
  398. activityId: item.id,
  399. isEdit: true,
  400. ));
  401. } else if (item.activityTypeName == "ACTIVE_TYPE_PACKING") {
  402. Get.to(EditActionPackingScreen(
  403. cropId: item.cropId,
  404. activityId: item.id,
  405. isEdit: true,
  406. ));
  407. } else if (item.activityTypeName == "ACTIVE_TYPE_SELL") {
  408. Get.to(EditActionSellScreen(
  409. cropId: item.cropId,
  410. activityId: item.id,
  411. isEdit: true,
  412. ));
  413. } else if (item.activityTypeName == "ACTIVE_TYPE_END") {
  414. Get.to(EditActionEndScreen(
  415. cropId: item.cropId,
  416. activityId: item.id,
  417. isEdit: true,
  418. ));
  419. } else if (item.activityTypeName == "ACTIVE_TYPE_STATUS_GROW") {
  420. Get.to(EditActionPlantScreen(
  421. cropId: item.cropId,
  422. activityId: item.id,
  423. isEdit: true,
  424. ));
  425. } else if (item.activityTypeName == "ACTIVE_TYPE_MANURING") {
  426. Get.to(EditActionDungScreen(
  427. cropId: item.cropId,
  428. activityId: item.id,
  429. isEdit: true,
  430. ));
  431. } else if (item.activityTypeName == "ACTIVE_TYPE_SPRAY") {
  432. Get.to(EditActionSprayingScreen(
  433. cropId: item.cropId,
  434. activityId: item.id,
  435. isEdit: true,
  436. ));
  437. } else if (item.activityTypeName == "ACTIVE_TYPE_PROCESS") {
  438. Get.to(EditActionHarvestProcessScreen(
  439. cropId: item.cropId,
  440. activityId: item.id,
  441. isEdit: true,
  442. ));
  443. } else {
  444. Get.to(EditActionOtherScreen(
  445. cropId: item.cropId,
  446. activityId: item.id,
  447. isEdit: true,
  448. ));
  449. }
  450. });
  451. }
  452. }
  453. class ActionType {
  454. Widget listScreen;
  455. String actionName;
  456. ActionType(String actionName, Widget listScreen) {
  457. this.actionName = actionName;
  458. this.listScreen = listScreen;
  459. }
  460. }