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.

659 lines
29KB

  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. Validators.stringNotNullOrEmpty(
  206. changeSelectedSupply.selectedSupplyName)
  207. ? Text(
  208. 'Tên thương mại *',
  209. style: TextStyle(
  210. fontSize: 13,
  211. fontWeight: FontWeight.normal,
  212. color: isValid
  213. ? Colors.black54
  214. : Colors.red[600]),
  215. )
  216. : Text(
  217. '',
  218. style: TextStyle(
  219. fontSize: 13,
  220. fontWeight: FontWeight.normal,
  221. color: isValid
  222. ? Colors.black54
  223. : Colors.red[600]),
  224. ),
  225. Row(
  226. children: [
  227. Expanded(
  228. child: Validators.stringNotNullOrEmpty(
  229. changeSelectedSupply.selectedSupplyName)
  230. ? Text(
  231. changeSelectedSupply.selectedSupplyName,
  232. style: TextStyle(
  233. fontSize: 14.0,
  234. color: Colors.black87))
  235. : Text("Tên thương mại *",
  236. style: TextStyle(
  237. fontSize: 14.0,
  238. color: Colors.black54)),
  239. ),
  240. Icon(
  241. Icons.arrow_drop_down,
  242. color: Colors.grey,
  243. ),
  244. ],
  245. ),
  246. ],
  247. )),
  248. isValid ? SizedBox() : WidgetErrorTextField()
  249. ],
  250. );
  251. }));
  252. });
  253. }
  254. Widget _btnSelectDevice() {
  255. return GetBuilder<ChangeDevice>(builder: (data) {
  256. return FlatButton(
  257. padding:
  258. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  259. onPressed: () {
  260. Navigator.of(context)
  261. .push(MaterialPageRoute(
  262. builder: (_) => ListDeviceActivity(
  263. selectedId: changeSelectedDevice.selectedDeviceId),
  264. fullscreenDialog: false))
  265. .then((value) {
  266. if (value != null) {
  267. var result = value as Device;
  268. changeSelectedDevice.change(result);
  269. changeFormField.change(true);
  270. }
  271. });
  272. },
  273. child: Container(
  274. padding: EdgeInsets.only(
  275. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  276. decoration: BoxDecoration(
  277. border: kBorderTextField,
  278. ),
  279. child: Column(
  280. crossAxisAlignment: CrossAxisAlignment.start,
  281. children: [
  282. Validators.stringNotNullOrEmpty(
  283. changeSelectedDevice.selectedDeviceName)
  284. ? Text(
  285. 'Thiết bị',
  286. style: TextStyle(
  287. fontSize: 13,
  288. fontWeight: FontWeight.normal,
  289. color: Colors.black45),
  290. )
  291. : Text(
  292. '',
  293. style: TextStyle(
  294. fontSize: 13,
  295. fontWeight: FontWeight.normal,
  296. color: Colors.black54),
  297. ),
  298. Row(
  299. children: [
  300. GetBuilder<ChangeSupply>(
  301. builder: (_) => Expanded(
  302. child: Validators.stringNotNullOrEmpty(
  303. changeSelectedDevice.selectedDeviceName)
  304. ? Text(
  305. changeSelectedDevice.selectedDeviceName,
  306. style: TextStyle(
  307. fontSize: 14.0,
  308. color: Colors.black87))
  309. : Text("Thiết bị",
  310. style: TextStyle(
  311. fontSize: 14.0,
  312. color: Colors.black54)),
  313. )),
  314. Icon(
  315. Icons.arrow_drop_down,
  316. color: Colors.grey,
  317. ),
  318. ],
  319. ),
  320. ],
  321. )));
  322. });
  323. }
  324. Widget _dropdownUnitTypes() {
  325. return GetBuilder<ChangeUnit>(builder: (data) {
  326. return DropdownButtonFormField<String>(
  327. itemHeight: 100,
  328. value: data.selectedUnit.isEmpty ? null : data.selectedUnit,
  329. items: data.currentUnits
  330. .map((label) => DropdownMenuItem(
  331. child: Text(label),
  332. value: label,
  333. ))
  334. .toList(),
  335. onChanged: (value) {
  336. var currentQuantity = _quantityController.text;
  337. num assignValue = currentQuantity.parseDoubleThousand();
  338. if (assignValue != null) {
  339. var oldSelected = data.selectedUnit;
  340. if (oldSelected == value) {
  341. } else {
  342. assignValue = UtilAction.convertUnit(
  343. inputValue: assignValue,
  344. oldUnit: oldSelected,
  345. newUnit: value);
  346. }
  347. _quantityController.text = assignValue.formatNumtoStringDecimal();
  348. }
  349. changeUnit.updateSelected(value);
  350. },
  351. );
  352. });
  353. }
  354. _quantityField() {
  355. return WidgetTextFormFieldNumber(
  356. hintValue: "Tổng lượng sử dụng *",
  357. labelText: "Tổng lượng sử dụng *",
  358. textController: _quantityController,
  359. validator: (String value) {
  360. return Validators.validateNotNullOrEmpty(
  361. value, label_validate_input_empty);
  362. },
  363. onChanged: (value) {
  364. if (!Validators.stringNotNullOrEmpty(value) &&
  365. !Validators.stringNotNullOrEmpty(_howToUseController.text) &&
  366. !Validators.stringNotNullOrEmpty(_dosageController.text) &&
  367. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  368. changeSelectedDevice.selectedDeviceId <= 0) {
  369. changeFormField.change(false);
  370. } else {
  371. changeFormField.change(true);
  372. }
  373. },
  374. );
  375. }
  376. _buttonInForm() {
  377. return GetBuilder<ChangeButtonInForm>(builder: (_) {
  378. return Row(
  379. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  380. children: [
  381. _.isEdit
  382. ? OutlineButton(
  383. shape: RoundedRectangleBorder(
  384. borderRadius: new BorderRadius.circular(8.0)),
  385. child: Text("Huỷ"),
  386. onPressed: () {
  387. changeButton.resetValue();
  388. _resetForm();
  389. _hidenKeyboard(context);
  390. })
  391. : SizedBox(),
  392. _.isEdit
  393. ? Expanded(
  394. child: FlatButton(
  395. onPressed: () {
  396. if (_formSupplyKey.currentState.validate()) {
  397. _formSupplyKey.currentState.save();
  398. if (changeSelectedSupply.selectedSupplyId <= 0) {
  399. changeSelectedSupply.changeValid(false);
  400. } else {
  401. changeSelectedSupply.changeValid(true);
  402. }
  403. var currentSupply =
  404. changeSelectedSupply.currentSupply;
  405. var currentDevice =
  406. changeSelectedDevice.currentDevice;
  407. var currentQuantity =
  408. _quantityController.text.parseDoubleThousand();
  409. if (currentSupply.id != null &&
  410. (currentQuantity ?? 0) > 0) {
  411. var quantityWithCurrentSupplyUnit =
  412. UtilAction.convertUnit(
  413. inputValue: currentQuantity,
  414. oldUnit: changeUnit.selectedUnit,
  415. newUnit: changeSelectedSupply
  416. .currentSupply.unit);
  417. SuppliesUsing newSup = SuppliesUsing()
  418. ..dosage = _dosageController.text
  419. ..howToUse = _howToUseController.text
  420. ..quantity = quantityWithCurrentSupplyUnit
  421. ..tbSuppliesInWarehouseId = currentSupply.id
  422. ..suppliesInWarehouseId = currentSupply.id
  423. ..supplyName = currentSupply.tbSuppliesName
  424. ..tbEquipmentOfCustomerId = currentDevice.id
  425. ..equipmentOfCustomerId = currentDevice.id
  426. ..equipmentName = currentDevice.name
  427. ..supplyUnit = currentSupply.unit
  428. ..unit = currentSupply.unit;
  429. changeSupplyUsing.editSupply(
  430. changeSupplyUsing.currentIndex, newSup);
  431. _resetForm();
  432. _hidenKeyboard(context);
  433. } else if (currentSupply.id == null ||
  434. ((currentQuantity ?? 0) <= 0)) {
  435. Utils.showSnackBarWarning(
  436. message: "Vui lòng nhập vật tư và số lượng");
  437. }
  438. } else {
  439. Utils.showSnackBarWarning(
  440. message: "Vui lòng nhập vật tư và số lượng");
  441. if (changeSelectedSupply.selectedSupplyId <= 0) {
  442. changeSelectedSupply.changeValid(false);
  443. } else {
  444. changeSelectedSupply.changeValid(true);
  445. }
  446. }
  447. },
  448. child: Text(
  449. "Sửa thuốc BVTV",
  450. style: TextStyle(color: Colors.blue),
  451. )),
  452. )
  453. : Expanded(
  454. child: FlatButton(
  455. onPressed: () {
  456. if (_formSupplyKey.currentState.validate()) {
  457. _formSupplyKey.currentState.save();
  458. if (changeSelectedSupply.selectedSupplyId <= 0) {
  459. changeSelectedSupply.changeValid(false);
  460. } else {
  461. changeSelectedSupply.changeValid(true);
  462. }
  463. var currentSupply =
  464. changeSelectedSupply.currentSupply;
  465. var currentDevice =
  466. changeSelectedDevice.currentDevice;
  467. var currentQuantity =
  468. _quantityController.text.parseDoubleThousand();
  469. if (currentSupply.id != null &&
  470. (currentQuantity ?? 0) > 0) {
  471. var quantityWithCurrentSupplyUnit =
  472. UtilAction.convertUnit(
  473. inputValue: currentQuantity,
  474. oldUnit: changeUnit.selectedUnit,
  475. newUnit: changeSelectedSupply
  476. .currentSupply.unit);
  477. SuppliesUsing newSup = SuppliesUsing()
  478. ..dosage = _dosageController.text
  479. ..howToUse = _howToUseController.text
  480. ..quantity = quantityWithCurrentSupplyUnit
  481. ..tbSuppliesInWarehouseId = currentSupply.id
  482. ..suppliesInWarehouseId = currentSupply.id
  483. ..supplyName = currentSupply.tbSuppliesName
  484. ..supplyUnit = currentSupply.unit
  485. ..tbEquipmentOfCustomerId = currentDevice.id
  486. ..equipmentOfCustomerId = currentDevice.id
  487. ..equipmentName = currentDevice.name
  488. ..unit = currentSupply.unit;
  489. changeSupplyUsing.addSupply(newSup);
  490. _resetForm();
  491. _hidenKeyboard(context);
  492. } else if (currentSupply.id == null ||
  493. ((currentQuantity ?? 0) <= 0)) {
  494. Utils.showSnackBarWarning(
  495. message: "Vui lòng nhập vật tư và số lượng");
  496. }
  497. } else {
  498. Utils.showSnackBarWarning(
  499. message: "Vui lòng nhập vật tư và số lượng");
  500. if (changeSelectedSupply.selectedSupplyId <= 0) {
  501. changeSelectedSupply.changeValid(false);
  502. } else {
  503. changeSelectedSupply.changeValid(true);
  504. }
  505. }
  506. },
  507. child: Text(
  508. "+ Thêm thuốc BVTV",
  509. style: TextStyle(color: Colors.blue),
  510. )),
  511. )
  512. ],
  513. );
  514. });
  515. }
  516. Widget _formEdit() {
  517. return Form(
  518. key: _formSupplyKey,
  519. child: Column(
  520. children: [
  521. Container(
  522. padding: EdgeInsets.all(8.0),
  523. margin: EdgeInsets.all(8.0),
  524. decoration: BoxDecoration(
  525. shape: BoxShape.rectangle,
  526. borderRadius: BorderRadius.circular(10),
  527. color: Colors.white,
  528. border: Border.all(color: Colors.grey[300])),
  529. child: Column(
  530. children: [
  531. _btnSelectSubstrates(),
  532. TextFormField(
  533. keyboardType: TextInputType.text,
  534. controller: _dosageController,
  535. decoration: InputDecoration(labelText: "Liều lượng sử dụng"),
  536. onSaved: (newValue) {},
  537. onChanged: (value) {
  538. if (!Validators.stringNotNullOrEmpty(
  539. _quantityController.text) &&
  540. !Validators.stringNotNullOrEmpty(
  541. _howToUseController.text) &&
  542. !Validators.stringNotNullOrEmpty(value) &&
  543. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  544. changeSelectedDevice.selectedDeviceId <= 0) {
  545. changeFormField.change(false);
  546. } else {
  547. changeFormField.change(true);
  548. }
  549. },
  550. ),
  551. Row(
  552. mainAxisSize: MainAxisSize.min,
  553. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  554. crossAxisAlignment: CrossAxisAlignment.center,
  555. children: [
  556. Expanded(
  557. flex: 2,
  558. child: Container(
  559. height: 82,
  560. child: _quantityField(),
  561. ),
  562. ),
  563. SizedBox(
  564. width: 16.0,
  565. ),
  566. Expanded(
  567. flex: 1,
  568. child: Align(
  569. alignment: Alignment.bottomCenter,
  570. child: _dropdownUnitTypes(),
  571. )),
  572. ]),
  573. _btnSelectDevice(),
  574. TextFormField(
  575. keyboardType: TextInputType.text,
  576. controller: _howToUseController,
  577. decoration: InputDecoration(labelText: "Phương pháp sử dụng"),
  578. onSaved: (newValue) {},
  579. onChanged: (value) {
  580. if (!Validators.stringNotNullOrEmpty(
  581. _quantityController.text) &&
  582. !Validators.stringNotNullOrEmpty(value) &&
  583. !Validators.stringNotNullOrEmpty(
  584. _dosageController.text) &&
  585. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  586. changeSelectedDevice.selectedDeviceId <= 0) {
  587. changeFormField.change(false);
  588. } else {
  589. changeFormField.change(true);
  590. }
  591. },
  592. ),
  593. ],
  594. ),
  595. ),
  596. _buttonInForm()
  597. ],
  598. ),
  599. );
  600. }
  601. _resetForm() {
  602. changeSupplyUsing.changeIndexEdit(-1);
  603. changeButton.resetValue();
  604. _dosageController.text = "";
  605. _howToUseController.text = "";
  606. _quantityController.text = "";
  607. changeUnit.initValue();
  608. changeSelectedSupply.initValue();
  609. changeSelectedDevice.initValue();
  610. changeFormField.change(false);
  611. }
  612. _hidenKeyboard(BuildContext context) {
  613. FocusScopeNode currentFocus = FocusScope.of(context);
  614. if (!currentFocus.hasPrimaryFocus) {
  615. currentFocus.unfocus();
  616. }
  617. }
  618. @override
  619. Widget build(BuildContext context) {
  620. return Column(
  621. crossAxisAlignment: CrossAxisAlignment.start,
  622. children: [
  623. Padding(
  624. padding: const EdgeInsets.all(8.0),
  625. child: Align(
  626. alignment: Alignment.centerLeft,
  627. child: Text(
  628. 'Thuốc BVTV',
  629. style: TextStyle(color: Colors.black54, fontSize: 14),
  630. ),
  631. ),
  632. ),
  633. _buildListSupply(),
  634. SizedBox(
  635. height: 8.0,
  636. ),
  637. _formEdit()
  638. ],
  639. );
  640. }
  641. }