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.

593 lines
26KB

  1. import 'package:farm_tpf/custom_model/Device.dart';
  2. import 'package:farm_tpf/custom_model/SuppliesUsing.dart';
  3. import 'package:farm_tpf/custom_model/Supply.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/widget_text_form_field.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
  6. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeDevice.dart';
  7. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeFieldInForm.dart';
  8. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeFormButton.dart';
  9. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeSupplyUsing.dart';
  10. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeUnit.dart';
  11. import 'package:farm_tpf/presentation/screens/actions/resource_device_activity/sc_device_activity.dart';
  12. import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_supply.dart';
  13. import 'package:farm_tpf/presentation/screens/resources/sc_resource_helper.dart';
  14. import 'package:farm_tpf/utils/const_color.dart';
  15. import 'package:farm_tpf/utils/const_common.dart';
  16. import 'package:farm_tpf/utils/const_style.dart';
  17. import 'package:farm_tpf/utils/validators.dart';
  18. import 'package:flutter/material.dart';
  19. import 'package:get/get.dart';
  20. import 'package:farm_tpf/utils/formatter.dart';
  21. import '../util_action.dart';
  22. class WidgetSprayingSupply extends StatefulWidget {
  23. final List<SuppliesUsing> currentItems;
  24. final Function(List<SuppliesUsing> supplyChanges) onChangeSupplies;
  25. WidgetSprayingSupply({this.currentItems, @required this.onChangeSupplies});
  26. @override
  27. _WidgetSprayingSupplyState createState() => _WidgetSprayingSupplyState();
  28. }
  29. class _WidgetSprayingSupplyState extends State<WidgetSprayingSupply> {
  30. final _dosageController = TextEditingController();
  31. final _quantityController = TextEditingController();
  32. final _howToUseController = TextEditingController();
  33. final changeSelectedSupply = Get.put(ChangeSupply());
  34. final changeSupplyUsing = Get.put(ChangeSupplyUsing());
  35. final changeUnit = Get.put(ChangeUnit());
  36. final changeButton = Get.put(ChangeButtonInForm());
  37. final changeSelectedDevice = Get.put(ChangeDevice());
  38. final changeFormField = Get.put(ChangeFieldFormSupply());
  39. GlobalKey<FormState> _formSupplyKey = GlobalKey();
  40. @override
  41. void initState() {
  42. super.initState();
  43. changeSelectedSupply.initValue();
  44. changeSelectedDevice.initValue();
  45. changeSupplyUsing.init(widget.currentItems);
  46. changeUnit.initValue();
  47. changeButton.resetValue();
  48. changeFormField.init();
  49. }
  50. Widget _buildListSupply() {
  51. return GetBuilder<ChangeSupplyUsing>(builder: (value) {
  52. widget.onChangeSupplies(value.currentItems);
  53. if (value.currentItems.isEmpty) {
  54. return Container();
  55. } else {
  56. return Container(
  57. height: 120,
  58. child: ListView.builder(
  59. physics: ClampingScrollPhysics(),
  60. scrollDirection: Axis.horizontal,
  61. shrinkWrap: true,
  62. itemCount: value.currentItems.length,
  63. itemBuilder: (context, index) {
  64. return GestureDetector(
  65. onTap: () {
  66. print("edit");
  67. changeSupplyUsing.changeIndexEdit(index);
  68. changeButton.updateToEdit(true);
  69. var editedSupplyUsing = value.currentItems[index];
  70. var editedSupply = Supply()
  71. ..id = editedSupplyUsing.tbSuppliesInWarehouseId
  72. ..tbSuppliesName = editedSupplyUsing.supplyName
  73. ..unit = editedSupplyUsing.supplyUnit;
  74. changeSelectedSupply.change(editedSupply);
  75. var editedDevice = Device()
  76. ..id = editedSupplyUsing.tbEquipmentOfCustomerId
  77. ..name = editedSupplyUsing.equipmentName;
  78. changeSelectedDevice.change(editedDevice);
  79. changeUnit
  80. .updateListByUnitName(editedSupplyUsing.supplyUnit);
  81. changeUnit.updateSelected(editedSupplyUsing.supplyUnit);
  82. _dosageController.text = editedSupplyUsing.dosage;
  83. _howToUseController.text = editedSupplyUsing.howToUse;
  84. _quantityController.text = editedSupplyUsing.quantity
  85. .formatNumtoStringDecimal();
  86. },
  87. child: Card(
  88. child: Stack(
  89. alignment: Alignment.bottomCenter,
  90. overflow: Overflow.visible,
  91. children: <Widget>[
  92. Positioned(
  93. child: ClipRRect(
  94. borderRadius: BorderRadius.circular(8),
  95. child: Container(
  96. padding: EdgeInsets.all(4),
  97. width: 150,
  98. child: Column(
  99. mainAxisAlignment: MainAxisAlignment.center,
  100. children: [
  101. SizedBox(
  102. height: 12.0,
  103. ),
  104. Flexible(
  105. child: Text(
  106. value.currentItems[index].supplyName ??
  107. "",
  108. overflow: TextOverflow.ellipsis,
  109. maxLines: 1),
  110. ),
  111. Validators.stringNotNullOrEmpty(
  112. value.currentItems[index].dosage)
  113. ? Flexible(
  114. child: Text(
  115. "${value.currentItems[index].dosage ?? ""}"))
  116. : SizedBox(),
  117. Validators.stringNotNullOrEmpty(value
  118. .currentItems[index].quantity
  119. .formatNumtoStringDecimal())
  120. ? Flexible(
  121. child: Text(
  122. "${value.currentItems[index].quantity.formatNumtoStringDecimal() ?? ""} ${value.currentItems[index].supplyUnit ?? ""}"))
  123. : SizedBox(),
  124. Validators.stringNotNullOrEmpty(value
  125. .currentItems[index].equipmentName)
  126. ? Flexible(
  127. child: Text(
  128. "${value.currentItems[index].equipmentName ?? ""}"))
  129. : SizedBox(),
  130. Validators.stringNotNullOrEmpty(
  131. value.currentItems[index].howToUse)
  132. ? Flexible(
  133. child: Text(
  134. "${value.currentItems[index].howToUse ?? ""}"))
  135. : SizedBox(),
  136. ],
  137. ),
  138. ),
  139. )),
  140. Positioned(
  141. top: -10,
  142. right: -10,
  143. child: IconButton(
  144. icon: Icon(
  145. Icons.cancel,
  146. color: Colors.redAccent,
  147. ),
  148. onPressed: () {
  149. changeSupplyUsing.deleteSupply(index);
  150. }),
  151. )
  152. ],
  153. )));
  154. }));
  155. }
  156. });
  157. }
  158. Widget _btnSelectSubstrates() {
  159. return GetBuilder<ChangeSupply>(builder: (data) {
  160. return FlatButton(
  161. padding:
  162. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  163. onPressed: () {
  164. var currentIndexEdit = changeSupplyUsing.currentIndex;
  165. Navigator.of(context)
  166. .push(MaterialPageRoute(
  167. builder: (_) => ResourceHelperScreen(
  168. titleName: "Thuốc BVTV",
  169. type: ConstCommon.supplyTypeProtectPlant,
  170. selectedId: changeSelectedSupply.selectedSupplyId,
  171. currentItems: changeSupplyUsing.currentItems,
  172. currentEditId: (currentIndexEdit >= 0)
  173. ? changeSupplyUsing.currentItems[currentIndexEdit]
  174. .tbSuppliesInWarehouseId
  175. : -1,
  176. ),
  177. fullscreenDialog: false))
  178. .then((value) {
  179. if (value != null) {
  180. var result = value as Supply;
  181. changeSelectedSupply.change(result);
  182. changeUnit.updateListByUnitName(result.unit);
  183. changeFormField.change(true);
  184. }
  185. });
  186. },
  187. child: GetBuilder<ChangeSupply>(builder: (_) {
  188. var isValid = changeSelectedSupply.isValid;
  189. return Container(
  190. padding: EdgeInsets.only(
  191. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  192. decoration: BoxDecoration(
  193. border: Border(
  194. bottom: BorderSide(
  195. width: 1,
  196. color: isValid ? Colors.grey : Colors.red[900])),
  197. ),
  198. child: Column(
  199. crossAxisAlignment: CrossAxisAlignment.start,
  200. children: [
  201. Text(
  202. 'Tên thương mại *',
  203. style: TextStyle(
  204. fontSize: 13,
  205. fontWeight: FontWeight.normal,
  206. color: isValid ? Colors.black54 : Colors.red[600]),
  207. ),
  208. Row(
  209. children: [
  210. Expanded(
  211. child: Text(
  212. changeSelectedSupply.selectedSupplyName ??
  213. "Tên thương mại",
  214. style: TextStyle(
  215. fontSize: 14.0, color: Colors.black87))),
  216. Icon(
  217. Icons.arrow_drop_down,
  218. color: Colors.grey,
  219. ),
  220. ],
  221. ),
  222. ],
  223. ));
  224. }));
  225. });
  226. }
  227. Widget _btnSelectDevice() {
  228. return GetBuilder<ChangeDevice>(builder: (data) {
  229. return FlatButton(
  230. padding:
  231. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  232. onPressed: () {
  233. Navigator.of(context)
  234. .push(MaterialPageRoute(
  235. builder: (_) => ListDeviceActivity(
  236. selectedId: changeSelectedDevice.selectedDeviceId),
  237. fullscreenDialog: false))
  238. .then((value) {
  239. if (value != null) {
  240. var result = value as Device;
  241. changeSelectedDevice.change(result);
  242. changeFormField.change(true);
  243. }
  244. });
  245. },
  246. child: Container(
  247. padding: EdgeInsets.only(
  248. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  249. decoration: BoxDecoration(
  250. border: kBorderTextField,
  251. ),
  252. child: Row(
  253. children: [
  254. GetBuilder<ChangeSupply>(
  255. builder: (_) => Expanded(
  256. child: Text(
  257. changeSelectedDevice.selectedDeviceName ??
  258. "Thiết bị",
  259. style: TextStyle(
  260. fontSize: 14.0, color: Colors.black87)))),
  261. Icon(
  262. Icons.arrow_drop_down,
  263. color: Colors.grey,
  264. ),
  265. ],
  266. )));
  267. });
  268. }
  269. Widget _dropdownUnitTypes() {
  270. return GetBuilder<ChangeUnit>(builder: (data) {
  271. return DropdownButtonFormField<String>(
  272. itemHeight: 100,
  273. value: data.selectedUnit.isEmpty ? null : data.selectedUnit,
  274. items: data.currentUnits
  275. .map((label) => DropdownMenuItem(
  276. child: Text(label),
  277. value: label,
  278. ))
  279. .toList(),
  280. onChanged: (value) {
  281. var currentQuantity = _quantityController.text;
  282. num assignValue = currentQuantity.parseDoubleThousand();
  283. if (assignValue != null) {
  284. var oldSelected = data.selectedUnit;
  285. if (oldSelected == value) {
  286. } else {
  287. assignValue = UtilAction.convertUnit(
  288. inputValue: assignValue,
  289. oldUnit: oldSelected,
  290. newUnit: value);
  291. }
  292. _quantityController.text = assignValue.formatNumtoStringDecimal();
  293. }
  294. changeUnit.updateSelected(value);
  295. },
  296. );
  297. });
  298. }
  299. _quantityField() {
  300. return WidgetTextFormFieldNumber(
  301. hintValue: "Tổng lượng sử dụng *",
  302. textController: _quantityController,
  303. validator: (String value) {
  304. return Validators.validateNotNullOrEmpty(value, "");
  305. },
  306. onChanged: (value) {
  307. if (!Validators.stringNotNullOrEmpty(value) &&
  308. !Validators.stringNotNullOrEmpty(_howToUseController.text) &&
  309. !Validators.stringNotNullOrEmpty(_dosageController.text) &&
  310. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  311. changeSelectedDevice.selectedDeviceId <= 0) {
  312. changeFormField.change(false);
  313. } else {
  314. changeFormField.change(true);
  315. }
  316. },
  317. );
  318. }
  319. _buttonInForm() {
  320. return GetBuilder<ChangeButtonInForm>(builder: (_) {
  321. return Align(
  322. alignment: Alignment.centerRight,
  323. child: Row(
  324. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  325. children: [
  326. _.isEdit
  327. ? OutlineButton(
  328. shape: RoundedRectangleBorder(
  329. borderRadius: new BorderRadius.circular(8.0)),
  330. child: Text("Huỷ"),
  331. onPressed: () {
  332. changeButton.resetValue();
  333. _resetForm();
  334. _hidenKeyboard(context);
  335. })
  336. : SizedBox(),
  337. _.isEdit
  338. ? FlatButton(
  339. color: AppColors.DEFAULT,
  340. shape: RoundedRectangleBorder(
  341. borderRadius: new BorderRadius.circular(8.0)),
  342. onPressed: () {
  343. if (_formSupplyKey.currentState.validate()) {
  344. _formSupplyKey.currentState.save();
  345. if (changeSelectedSupply.selectedSupplyId <= 0) {
  346. changeSelectedSupply.changeValid(false);
  347. } else {
  348. changeSelectedSupply.changeValid(true);
  349. }
  350. var currentSupply = changeSelectedSupply.currentSupply;
  351. var currentDevice = changeSelectedDevice.currentDevice;
  352. var currentQuantity =
  353. _quantityController.text.parseDoubleThousand();
  354. if (currentSupply.id != null &&
  355. (currentQuantity ?? 0) > 0) {
  356. var quantityWithCurrentSupplyUnit =
  357. UtilAction.convertUnit(
  358. inputValue: currentQuantity,
  359. oldUnit: changeUnit.selectedUnit,
  360. newUnit:
  361. changeSelectedSupply.currentSupply.unit);
  362. SuppliesUsing newSup = SuppliesUsing()
  363. ..dosage = _dosageController.text
  364. ..howToUse = _howToUseController.text
  365. ..quantity = quantityWithCurrentSupplyUnit
  366. ..tbSuppliesInWarehouseId = currentSupply.id
  367. ..suppliesInWarehouseId = currentSupply.id
  368. ..supplyName = currentSupply.tbSuppliesName
  369. ..tbEquipmentOfCustomerId = currentDevice.id
  370. ..equipmentOfCustomerId = currentDevice.id
  371. ..equipmentName = currentDevice.name
  372. ..supplyUnit = currentSupply.unit
  373. ..unit = currentSupply.unit;
  374. changeSupplyUsing.editSupply(
  375. changeSupplyUsing.currentIndex, newSup);
  376. _resetForm();
  377. _hidenKeyboard(context);
  378. } else if (currentSupply.id == null ||
  379. ((currentQuantity ?? 0) <= 0)) {
  380. Utils.showSnackBarWarning(
  381. message: "Vui lòng nhập vật tư và số lượng");
  382. }
  383. } else {
  384. Utils.showSnackBarWarning(
  385. message: "Vui lòng nhập vật tư và số lượng");
  386. if (changeSelectedSupply.selectedSupplyId <= 0) {
  387. changeSelectedSupply.changeValid(false);
  388. } else {
  389. changeSelectedSupply.changeValid(true);
  390. }
  391. }
  392. },
  393. child: Text(
  394. "Sửa",
  395. style: TextStyle(color: Colors.white),
  396. ))
  397. : FlatButton(
  398. color: AppColors.DEFAULT,
  399. shape: RoundedRectangleBorder(
  400. borderRadius: new BorderRadius.circular(8.0)),
  401. onPressed: () {
  402. if (_formSupplyKey.currentState.validate()) {
  403. _formSupplyKey.currentState.save();
  404. if (changeSelectedSupply.selectedSupplyId <= 0) {
  405. changeSelectedSupply.changeValid(false);
  406. } else {
  407. changeSelectedSupply.changeValid(true);
  408. }
  409. var currentSupply = changeSelectedSupply.currentSupply;
  410. var currentDevice = changeSelectedDevice.currentDevice;
  411. var currentQuantity =
  412. _quantityController.text.parseDoubleThousand();
  413. if (currentSupply.id != null &&
  414. (currentQuantity ?? 0) > 0) {
  415. var quantityWithCurrentSupplyUnit =
  416. UtilAction.convertUnit(
  417. inputValue: currentQuantity,
  418. oldUnit: changeUnit.selectedUnit,
  419. newUnit:
  420. changeSelectedSupply.currentSupply.unit);
  421. SuppliesUsing newSup = SuppliesUsing()
  422. ..dosage = _dosageController.text
  423. ..howToUse = _howToUseController.text
  424. ..quantity = quantityWithCurrentSupplyUnit
  425. ..tbSuppliesInWarehouseId = currentSupply.id
  426. ..suppliesInWarehouseId = currentSupply.id
  427. ..supplyName = currentSupply.tbSuppliesName
  428. ..supplyUnit = currentSupply.unit
  429. ..tbEquipmentOfCustomerId = currentDevice.id
  430. ..equipmentOfCustomerId = currentDevice.id
  431. ..equipmentName = currentDevice.name
  432. ..unit = currentSupply.unit;
  433. changeSupplyUsing.addSupply(newSup);
  434. _resetForm();
  435. _hidenKeyboard(context);
  436. } else if (currentSupply.id == null ||
  437. ((currentQuantity ?? 0) <= 0)) {
  438. Utils.showSnackBarWarning(
  439. message: "Vui lòng nhập vật tư và số lượng");
  440. }
  441. } else {
  442. Utils.showSnackBarWarning(
  443. message: "Vui lòng nhập vật tư và số lượng");
  444. if (changeSelectedSupply.selectedSupplyId <= 0) {
  445. changeSelectedSupply.changeValid(false);
  446. } else {
  447. changeSelectedSupply.changeValid(true);
  448. }
  449. }
  450. },
  451. child: Text(
  452. "Thêm",
  453. style: TextStyle(color: Colors.white),
  454. ))
  455. ],
  456. ),
  457. );
  458. });
  459. }
  460. Widget _formEdit() {
  461. return Form(
  462. key: _formSupplyKey,
  463. child: Container(
  464. padding: EdgeInsets.all(8.0),
  465. decoration: BoxDecoration(
  466. shape: BoxShape.rectangle,
  467. borderRadius: BorderRadius.circular(10),
  468. color: Colors.white,
  469. border: Border.all(color: AppColors.DEFAULT)),
  470. child: Column(
  471. children: [
  472. _btnSelectSubstrates(),
  473. TextFormField(
  474. keyboardType: TextInputType.text,
  475. controller: _dosageController,
  476. decoration: InputDecoration(labelText: "Liều lượng sử dụng"),
  477. onSaved: (newValue) {},
  478. onChanged: (value) {
  479. if (!Validators.stringNotNullOrEmpty(
  480. _quantityController.text) &&
  481. !Validators.stringNotNullOrEmpty(
  482. _howToUseController.text) &&
  483. !Validators.stringNotNullOrEmpty(value) &&
  484. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  485. changeSelectedDevice.selectedDeviceId <= 0) {
  486. changeFormField.change(false);
  487. } else {
  488. changeFormField.change(true);
  489. }
  490. },
  491. ),
  492. Row(
  493. mainAxisSize: MainAxisSize.min,
  494. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  495. crossAxisAlignment: CrossAxisAlignment.center,
  496. children: [
  497. Expanded(
  498. flex: 2,
  499. child: Container(
  500. height: 70,
  501. child: _quantityField(),
  502. ),
  503. ),
  504. SizedBox(
  505. width: 16.0,
  506. ),
  507. Expanded(
  508. flex: 1,
  509. child: Align(
  510. alignment: Alignment.bottomCenter,
  511. child: _dropdownUnitTypes(),
  512. )),
  513. ]),
  514. Container(
  515. width: double.infinity,
  516. child: Text(
  517. "Thiết bị",
  518. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  519. ),
  520. ),
  521. _btnSelectDevice(),
  522. TextFormField(
  523. keyboardType: TextInputType.text,
  524. controller: _howToUseController,
  525. decoration: InputDecoration(labelText: "Phương pháp sử dụng"),
  526. onSaved: (newValue) {},
  527. onChanged: (value) {
  528. if (!Validators.stringNotNullOrEmpty(
  529. _quantityController.text) &&
  530. !Validators.stringNotNullOrEmpty(value) &&
  531. !Validators.stringNotNullOrEmpty(
  532. _dosageController.text) &&
  533. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  534. changeSelectedDevice.selectedDeviceId <= 0) {
  535. changeFormField.change(false);
  536. } else {
  537. changeFormField.change(true);
  538. }
  539. },
  540. ),
  541. _buttonInForm()
  542. ],
  543. )),
  544. );
  545. }
  546. _resetForm() {
  547. changeSupplyUsing.changeIndexEdit(-1);
  548. changeButton.resetValue();
  549. _dosageController.text = "";
  550. _howToUseController.text = "";
  551. _quantityController.text = "";
  552. changeUnit.initValue();
  553. changeSelectedSupply.initValue();
  554. changeSelectedDevice.initValue();
  555. changeFormField.change(false);
  556. }
  557. _hidenKeyboard(BuildContext context) {
  558. FocusScopeNode currentFocus = FocusScope.of(context);
  559. if (!currentFocus.hasPrimaryFocus) {
  560. currentFocus.unfocus();
  561. }
  562. }
  563. @override
  564. Widget build(BuildContext context) {
  565. return Column(
  566. children: [
  567. _buildListSupply(),
  568. SizedBox(
  569. height: 8.0,
  570. ),
  571. _formEdit()
  572. ],
  573. );
  574. }
  575. }