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.

619 lines
27KB

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