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.

444 lines
19KB

  1. import 'dart:convert';
  2. import 'package:farm_tpf/custom_model/Harvest.dart';
  3. import 'package:farm_tpf/data/api/app_exception.dart';
  4. import 'package:farm_tpf/data/repository/repository.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/bloc/media_helper_bloc.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/widget_field_time_picker.dart';
  7. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  8. import 'package:farm_tpf/presentation/custom_widgets/widget_media_picker.dart';
  9. import 'package:farm_tpf/presentation/custom_widgets/widget_text_form_field.dart';
  10. import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
  11. import 'package:farm_tpf/presentation/screens/actions/bloc/action_detail_bloc.dart';
  12. import 'package:farm_tpf/presentation/screens/actions/harvest_process/sc_edit_action_harvest_process.dart';
  13. import 'package:farm_tpf/presentation/screens/actions/packing/sc_edit_action_packing.dart';
  14. import 'package:farm_tpf/presentation/screens/actions/sell/sc_edit_action_sell.dart';
  15. import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_file_controller.dart';
  16. import 'package:farm_tpf/presentation/screens/actions/util_action.dart';
  17. import 'package:farm_tpf/utils/const_common.dart';
  18. import 'package:farm_tpf/utils/const_string.dart';
  19. import 'package:farm_tpf/utils/const_style.dart';
  20. import 'package:farm_tpf/utils/pref.dart';
  21. import 'package:farm_tpf/utils/validators.dart';
  22. import 'package:flutter/material.dart';
  23. import 'package:flutter_bloc/flutter_bloc.dart';
  24. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  25. import 'package:get/get.dart';
  26. import 'package:intl/intl.dart';
  27. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  28. import 'package:pattern_formatter/pattern_formatter.dart';
  29. import 'package:farm_tpf/utils/formatter.dart';
  30. class EditActionHarvestScreen extends StatefulWidget {
  31. final int cropId;
  32. final bool isEdit;
  33. final int activityId;
  34. EditActionHarvestScreen(
  35. {@required this.cropId, this.isEdit = false, this.activityId});
  36. @override
  37. _EditActionHarvestScreenState createState() =>
  38. _EditActionHarvestScreenState();
  39. }
  40. class _EditActionHarvestScreenState extends State<EditActionHarvestScreen> {
  41. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  42. final _repository = Repository();
  43. GlobalKey<FormState> _formKey = GlobalKey();
  44. bool _autoValidate = false;
  45. Harvest _harvest = Harvest();
  46. TextEditingController _l1Controller = TextEditingController();
  47. TextEditingController _l2Controller = TextEditingController();
  48. TextEditingController _l3Controller = TextEditingController();
  49. TextEditingController _removedQuantityController = TextEditingController();
  50. TextEditingController _descriptionController = TextEditingController();
  51. final _executeByController = TextEditingController();
  52. var pref = LocalPref();
  53. DateTime executeTime = DateTime.now();
  54. List<String> filePaths = List<String>();
  55. var changeFileController = Get.put(ChangeFileController());
  56. Future<Null> getSharedPrefs() async {
  57. var currentFullName = await pref.getString(DATA_CONST.CURRENT_FULL_NAME);
  58. _executeByController.text = currentFullName ?? "";
  59. }
  60. @override
  61. void initState() {
  62. super.initState();
  63. getSharedPrefs();
  64. changeFileController.initValue();
  65. _harvest.cropId = widget.cropId;
  66. }
  67. _validateInputs() async {
  68. if (_formKey.currentState.validate()) {
  69. _formKey.currentState.save();
  70. LoadingDialog.showLoadingDialog(context);
  71. filePaths = Get.find<ChangeFileController>().newFiles;
  72. try {
  73. _harvest.mediaDel = Get.find<ChangeFileController>().deleteFiles;
  74. var activityHarvest = jsonEncode(_harvest.toJson()).toString();
  75. //ADD NEW
  76. if (_harvest.activityId == null) {
  77. _repository.createAction((value) {
  78. LoadingDialog.hideLoadingDialog(context);
  79. Get.back(result: value);
  80. Utils.showSnackBarSuccess(message: label_add_success);
  81. }, (error) {
  82. LoadingDialog.hideLoadingDialog(context);
  83. Utils.showSnackBarError(message: AppException.handleError(error));
  84. },
  85. apiAddAction: ConstCommon.apiAddHarvest,
  86. paramActivity: ConstCommon.paramsActionHarvest,
  87. activityAction: activityHarvest,
  88. filePaths: filePaths);
  89. } else {
  90. //UPDATE
  91. _repository.updateAction((value) {
  92. LoadingDialog.hideLoadingDialog(context);
  93. Get.back(result: value);
  94. Utils.showSnackBarSuccess(message: label_update_success);
  95. }, (error) {
  96. LoadingDialog.hideLoadingDialog(context);
  97. Utils.showSnackBarError(message: AppException.handleError(error));
  98. },
  99. apiUpdateAction: ConstCommon.apiUpdateHarvest,
  100. paramActivity: ConstCommon.paramsActionHarvest,
  101. activityAction: activityHarvest,
  102. filePaths: filePaths);
  103. }
  104. } catch (e) {
  105. LoadingDialog.hideLoadingDialog(context);
  106. print(e.toString());
  107. }
  108. } else {
  109. _autoValidate = true;
  110. }
  111. }
  112. Widget _btnExecuteTimePicker() {
  113. return WidgetFieldDateTimePicker(
  114. initDateTime: executeTime,
  115. onUpdateDateTime: (selectedDate) {
  116. _harvest.executeDate =
  117. selectedDate.convertLocalDateTimeToStringUtcDateTime();
  118. });
  119. }
  120. Widget _l1Field() {
  121. return WidgetTextFormFieldNumber(
  122. hintValue: "Số lượng/khối lượng loại 1",
  123. textController: _l1Controller,
  124. onSaved: (newValue) {
  125. _harvest.collectedQuantityLv1 = newValue.parseDoubleThousand();
  126. },
  127. );
  128. }
  129. Widget _l2Field() {
  130. return WidgetTextFormFieldNumber(
  131. hintValue: "Số lượng/khối lượng loại 2",
  132. textController: _l2Controller,
  133. onSaved: (newValue) {
  134. _harvest.collectedQuantityLv2 = newValue.parseDoubleThousand();
  135. },
  136. );
  137. }
  138. Widget _l3Field() {
  139. return WidgetTextFormFieldNumber(
  140. hintValue: "Số lượng/khối lượng loại 3",
  141. textController: _l3Controller,
  142. onSaved: (newValue) {
  143. _harvest.collectedQuantityLv3 = newValue.parseDoubleThousand();
  144. },
  145. );
  146. }
  147. Widget _removedQuantityField() {
  148. return WidgetTextFormFieldNumber(
  149. hintValue: "Số lượng/khối lượng loại bỏ",
  150. textController: _removedQuantityController,
  151. onSaved: (newValue) {
  152. _harvest.removedQuantity = newValue.parseDoubleThousand();
  153. },
  154. );
  155. }
  156. Widget _descriptionField() {
  157. return TextFormField(
  158. keyboardType: TextInputType.text,
  159. decoration: InputDecoration(labelText: "Ghi chú"),
  160. controller: _descriptionController,
  161. onSaved: (newValue) {
  162. _harvest.description = newValue;
  163. },
  164. );
  165. }
  166. Widget _executeByField() {
  167. return TextFormField(
  168. keyboardType: TextInputType.text,
  169. decoration: InputDecoration(labelText: "Người thực hiện"),
  170. enabled: false,
  171. controller: _executeByController,
  172. onSaved: (newValue) {},
  173. );
  174. }
  175. _actionAppBar() {
  176. IconButton iconButton;
  177. if (1 == 1) {
  178. iconButton = IconButton(
  179. icon: Icon(
  180. Icons.done,
  181. color: Colors.black,
  182. ),
  183. onPressed: () {
  184. FocusScopeNode currentFocus = FocusScope.of(context);
  185. if (!currentFocus.hasPrimaryFocus) {
  186. currentFocus.unfocus();
  187. }
  188. _validateInputs();
  189. },
  190. );
  191. return <Widget>[iconButton];
  192. }
  193. return <Widget>[Container()];
  194. }
  195. @override
  196. Widget build(BuildContext context) => KeyboardDismisser(
  197. gestures: [
  198. GestureType.onTap,
  199. GestureType.onPanUpdateDownDirection,
  200. ],
  201. child: Scaffold(
  202. key: _scaffoldKey,
  203. appBar: AppBar(
  204. centerTitle: true,
  205. title: Text(plot_action_harvest),
  206. actions: _actionAppBar()),
  207. body: KeyboardDismisser(
  208. child: MultiBlocProvider(
  209. providers: [
  210. BlocProvider<ActionDetailBloc>(
  211. create: (context) =>
  212. ActionDetailBloc(repository: Repository())
  213. ..add(FetchData(
  214. isNeedFetchData: widget.isEdit,
  215. apiActivity: ConstCommon.apiDetailHarvest,
  216. activityId: widget.activityId))),
  217. BlocProvider<MediaHelperBloc>(
  218. create: (context) =>
  219. MediaHelperBloc()..add(ChangeListMedia(items: [])),
  220. )
  221. ],
  222. child: Form(
  223. key: _formKey,
  224. autovalidate: _autoValidate,
  225. child: SingleChildScrollView(
  226. padding: EdgeInsets.all(8.0),
  227. child: BlocConsumer<ActionDetailBloc, ActionDetailState>(
  228. listener: (context, state) async {
  229. if (state is ActionDetailFailure) {
  230. LoadingDialog.hideLoadingDialog(context);
  231. } else if (state is ActionDetailSuccess) {
  232. LoadingDialog.hideLoadingDialog(context);
  233. print(state.item);
  234. _harvest = Harvest.fromJson(state.item);
  235. _harvest.activityId = widget.activityId;
  236. _l1Controller.text = _harvest.collectedQuantityLv1
  237. .formatNumtoStringDecimal();
  238. _l2Controller.text = _harvest.collectedQuantityLv2
  239. .formatNumtoStringDecimal();
  240. _l3Controller.text = _harvest.collectedQuantityLv3
  241. .formatNumtoStringDecimal();
  242. _removedQuantityController.text = _harvest
  243. .removedQuantity
  244. .formatNumtoStringDecimal();
  245. _descriptionController.text =
  246. _harvest.description ?? "";
  247. _executeByController.text = _harvest.executeBy;
  248. Get.find<ChangeDateTimePicker>().change(_harvest
  249. .executeDate
  250. .convertStringServerDateTimeToLocalDateTime());
  251. //Show media
  252. if (Validators.stringNotNullOrEmpty(
  253. _harvest.media)) {
  254. BlocProvider.of<MediaHelperBloc>(context).add(
  255. ChangeListMedia(
  256. items: UtilAction.convertFilePathToMedia(
  257. _harvest.media)));
  258. }
  259. } else if (state is ActionDetailInitial) {
  260. } else if (state is ActionDetailLoading) {
  261. LoadingDialog.showLoadingDialog(context);
  262. }
  263. },
  264. builder: (context, state) {
  265. return Column(
  266. children: <Widget>[
  267. Container(
  268. width: double.infinity,
  269. child: Text(
  270. "Ngày thực hiện *",
  271. style: TextStyle(
  272. color: Colors.black54, fontSize: 13.0),
  273. ),
  274. ),
  275. _btnExecuteTimePicker(),
  276. SizedBox(
  277. height: 8.0,
  278. ),
  279. _l1Field(),
  280. SizedBox(
  281. height: 8.0,
  282. ),
  283. _l2Field(),
  284. SizedBox(
  285. height: 8.0,
  286. ),
  287. _l3Field(),
  288. SizedBox(
  289. height: 8.0,
  290. ),
  291. _removedQuantityField(),
  292. SizedBox(
  293. height: 8.0,
  294. ),
  295. _descriptionField(),
  296. SizedBox(
  297. height: 8.0,
  298. ),
  299. _executeByField(),
  300. SizedBox(
  301. height: 8.0,
  302. ),
  303. BlocBuilder<MediaHelperBloc, MediaHelperState>(
  304. builder: (context, state) {
  305. if (state is MediaHelperSuccess) {
  306. return WidgetMediaPicker(
  307. currentItems: state.items,
  308. onChangeFiles: (newPathFiles,
  309. deletePathFiles) async {
  310. Get.find<ChangeFileController>().change(
  311. newPathFiles, deletePathFiles);
  312. });
  313. } else {
  314. return Center(
  315. child: CircularProgressIndicator());
  316. }
  317. }),
  318. Row(
  319. mainAxisAlignment:
  320. MainAxisAlignment.spaceBetween,
  321. children: [
  322. SizedBox(
  323. width: Get.width / 2 - 16,
  324. child: FlatButton(
  325. padding: EdgeInsets.all(14),
  326. shape: RoundedRectangleBorder(
  327. borderRadius:
  328. new BorderRadius.circular(8.0)),
  329. color: Colors.lightGreen,
  330. onPressed: () {
  331. if (_harvest.id != null) {
  332. Get.to(EditActionHarvestProcessScreen(
  333. cropId: widget.cropId,
  334. harvestId: _harvest.id,
  335. ));
  336. } else {
  337. Get.to(EditActionHarvestProcessScreen(
  338. cropId: widget.cropId));
  339. }
  340. },
  341. child: Text(
  342. plot_action_harvest_process,
  343. style: TextStyle(
  344. color: Colors.white, fontSize: 14),
  345. ),
  346. ),
  347. ),
  348. SizedBox(
  349. width: Get.width / 2 - 16,
  350. child: FlatButton(
  351. padding: EdgeInsets.all(14),
  352. shape: RoundedRectangleBorder(
  353. borderRadius:
  354. new BorderRadius.circular(8.0)),
  355. color: Colors.lightGreen,
  356. onPressed: () {
  357. if (_harvest.id != null) {
  358. Get.to(EditActionPackingScreen(
  359. cropId: widget.cropId,
  360. harvestId: _harvest.id,
  361. ));
  362. } else {
  363. Get.to(EditActionPackingScreen(
  364. cropId: widget.cropId));
  365. }
  366. },
  367. child: Text(
  368. plot_action_packing,
  369. style: TextStyle(
  370. color: Colors.white, fontSize: 14),
  371. ),
  372. ),
  373. )
  374. ],
  375. ),
  376. SizedBox(
  377. height: 8,
  378. ),
  379. Row(
  380. mainAxisAlignment: MainAxisAlignment.start,
  381. children: [
  382. SizedBox(
  383. width: Get.width / 2 - 16,
  384. child: FlatButton(
  385. padding: EdgeInsets.all(14),
  386. shape: RoundedRectangleBorder(
  387. borderRadius:
  388. new BorderRadius.circular(8.0)),
  389. color: Colors.lightGreen,
  390. onPressed: () {
  391. if (_harvest.id != null) {
  392. Get.to(EditActionSellScreen(
  393. cropId: widget.cropId,
  394. harvestId: _harvest.id,
  395. ));
  396. } else {
  397. Get.to(EditActionSellScreen(
  398. cropId: widget.cropId));
  399. }
  400. },
  401. child: Text(
  402. plot_action_sell,
  403. style: TextStyle(
  404. color: Colors.white, fontSize: 14),
  405. ),
  406. ),
  407. )
  408. ],
  409. ),
  410. ],
  411. );
  412. },
  413. ),
  414. )),
  415. ))));
  416. @override
  417. void dispose() {
  418. _l1Controller.dispose();
  419. _l2Controller.dispose();
  420. _l3Controller.dispose();
  421. _removedQuantityController.dispose();
  422. _descriptionController.dispose();
  423. _executeByController.dispose();
  424. super.dispose();
  425. }
  426. }