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.

609 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 Row(
  322. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  323. children: [
  324. _.isEdit
  325. ? OutlineButton(
  326. shape: RoundedRectangleBorder(
  327. borderRadius: new BorderRadius.circular(8.0)),
  328. child: Text("Huỷ"),
  329. onPressed: () {
  330. changeButton.resetValue();
  331. _resetForm();
  332. _hidenKeyboard(context);
  333. })
  334. : SizedBox(),
  335. _.isEdit
  336. ? Expanded(
  337. child: FlatButton(
  338. onPressed: () {
  339. if (_formSupplyKey.currentState.validate()) {
  340. _formSupplyKey.currentState.save();
  341. if (changeSelectedSupply.selectedSupplyId <= 0) {
  342. changeSelectedSupply.changeValid(false);
  343. } else {
  344. changeSelectedSupply.changeValid(true);
  345. }
  346. var currentSupply =
  347. changeSelectedSupply.currentSupply;
  348. var currentDevice =
  349. changeSelectedDevice.currentDevice;
  350. var currentQuantity =
  351. _quantityController.text.parseDoubleThousand();
  352. if (currentSupply.id != null &&
  353. (currentQuantity ?? 0) > 0) {
  354. var quantityWithCurrentSupplyUnit =
  355. UtilAction.convertUnit(
  356. inputValue: currentQuantity,
  357. oldUnit: changeUnit.selectedUnit,
  358. newUnit: changeSelectedSupply
  359. .currentSupply.unit);
  360. SuppliesUsing newSup = SuppliesUsing()
  361. ..dosage = _dosageController.text
  362. ..howToUse = _howToUseController.text
  363. ..quantity = quantityWithCurrentSupplyUnit
  364. ..tbSuppliesInWarehouseId = currentSupply.id
  365. ..suppliesInWarehouseId = currentSupply.id
  366. ..supplyName = currentSupply.tbSuppliesName
  367. ..tbEquipmentOfCustomerId = currentDevice.id
  368. ..equipmentOfCustomerId = currentDevice.id
  369. ..equipmentName = currentDevice.name
  370. ..supplyUnit = currentSupply.unit
  371. ..unit = currentSupply.unit;
  372. changeSupplyUsing.editSupply(
  373. changeSupplyUsing.currentIndex, newSup);
  374. _resetForm();
  375. _hidenKeyboard(context);
  376. } else if (currentSupply.id == null ||
  377. ((currentQuantity ?? 0) <= 0)) {
  378. Utils.showSnackBarWarning(
  379. message: "Vui lòng nhập vật tư và số lượng");
  380. }
  381. } else {
  382. Utils.showSnackBarWarning(
  383. message: "Vui lòng nhập vật tư và số lượng");
  384. if (changeSelectedSupply.selectedSupplyId <= 0) {
  385. changeSelectedSupply.changeValid(false);
  386. } else {
  387. changeSelectedSupply.changeValid(true);
  388. }
  389. }
  390. },
  391. child: Text(
  392. "Sửa thuốc BVTV",
  393. style: TextStyle(color: Colors.blue),
  394. )),
  395. )
  396. : Expanded(
  397. child: FlatButton(
  398. onPressed: () {
  399. if (_formSupplyKey.currentState.validate()) {
  400. _formSupplyKey.currentState.save();
  401. if (changeSelectedSupply.selectedSupplyId <= 0) {
  402. changeSelectedSupply.changeValid(false);
  403. } else {
  404. changeSelectedSupply.changeValid(true);
  405. }
  406. var currentSupply =
  407. changeSelectedSupply.currentSupply;
  408. var currentDevice =
  409. changeSelectedDevice.currentDevice;
  410. var currentQuantity =
  411. _quantityController.text.parseDoubleThousand();
  412. if (currentSupply.id != null &&
  413. (currentQuantity ?? 0) > 0) {
  414. var quantityWithCurrentSupplyUnit =
  415. UtilAction.convertUnit(
  416. inputValue: currentQuantity,
  417. oldUnit: changeUnit.selectedUnit,
  418. newUnit: changeSelectedSupply
  419. .currentSupply.unit);
  420. SuppliesUsing newSup = SuppliesUsing()
  421. ..dosage = _dosageController.text
  422. ..howToUse = _howToUseController.text
  423. ..quantity = quantityWithCurrentSupplyUnit
  424. ..tbSuppliesInWarehouseId = currentSupply.id
  425. ..suppliesInWarehouseId = currentSupply.id
  426. ..supplyName = currentSupply.tbSuppliesName
  427. ..supplyUnit = currentSupply.unit
  428. ..tbEquipmentOfCustomerId = currentDevice.id
  429. ..equipmentOfCustomerId = currentDevice.id
  430. ..equipmentName = currentDevice.name
  431. ..unit = currentSupply.unit;
  432. changeSupplyUsing.addSupply(newSup);
  433. _resetForm();
  434. _hidenKeyboard(context);
  435. } else if (currentSupply.id == null ||
  436. ((currentQuantity ?? 0) <= 0)) {
  437. Utils.showSnackBarWarning(
  438. message: "Vui lòng nhập vật tư và số lượng");
  439. }
  440. } else {
  441. Utils.showSnackBarWarning(
  442. message: "Vui lòng nhập vật tư và số lượng");
  443. if (changeSelectedSupply.selectedSupplyId <= 0) {
  444. changeSelectedSupply.changeValid(false);
  445. } else {
  446. changeSelectedSupply.changeValid(true);
  447. }
  448. }
  449. },
  450. child: Text(
  451. "+ Thêm thuốc BVTV",
  452. style: TextStyle(color: Colors.blue),
  453. )),
  454. )
  455. ],
  456. );
  457. });
  458. }
  459. Widget _formEdit() {
  460. return Form(
  461. key: _formSupplyKey,
  462. child: Column(
  463. children: [
  464. Container(
  465. padding: EdgeInsets.all(8.0),
  466. margin: EdgeInsets.all(8.0),
  467. decoration: BoxDecoration(
  468. shape: BoxShape.rectangle,
  469. borderRadius: BorderRadius.circular(10),
  470. color: Colors.white,
  471. border: Border.all(color: Colors.grey[300])),
  472. child: Column(
  473. children: [
  474. _btnSelectSubstrates(),
  475. TextFormField(
  476. keyboardType: TextInputType.text,
  477. controller: _dosageController,
  478. decoration: InputDecoration(labelText: "Liều lượng sử dụng"),
  479. onSaved: (newValue) {},
  480. onChanged: (value) {
  481. if (!Validators.stringNotNullOrEmpty(
  482. _quantityController.text) &&
  483. !Validators.stringNotNullOrEmpty(
  484. _howToUseController.text) &&
  485. !Validators.stringNotNullOrEmpty(value) &&
  486. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  487. changeSelectedDevice.selectedDeviceId <= 0) {
  488. changeFormField.change(false);
  489. } else {
  490. changeFormField.change(true);
  491. }
  492. },
  493. ),
  494. Row(
  495. mainAxisSize: MainAxisSize.min,
  496. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  497. crossAxisAlignment: CrossAxisAlignment.center,
  498. children: [
  499. Expanded(
  500. flex: 2,
  501. child: Container(
  502. height: 82,
  503. child: _quantityField(),
  504. ),
  505. ),
  506. SizedBox(
  507. width: 16.0,
  508. ),
  509. Expanded(
  510. flex: 1,
  511. child: Align(
  512. alignment: Alignment.bottomCenter,
  513. child: _dropdownUnitTypes(),
  514. )),
  515. ]),
  516. Container(
  517. width: double.infinity,
  518. child: Text(
  519. "Thiết bị",
  520. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  521. ),
  522. ),
  523. _btnSelectDevice(),
  524. TextFormField(
  525. keyboardType: TextInputType.text,
  526. controller: _howToUseController,
  527. decoration: InputDecoration(labelText: "Phương pháp sử dụng"),
  528. onSaved: (newValue) {},
  529. onChanged: (value) {
  530. if (!Validators.stringNotNullOrEmpty(
  531. _quantityController.text) &&
  532. !Validators.stringNotNullOrEmpty(value) &&
  533. !Validators.stringNotNullOrEmpty(
  534. _dosageController.text) &&
  535. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  536. changeSelectedDevice.selectedDeviceId <= 0) {
  537. changeFormField.change(false);
  538. } else {
  539. changeFormField.change(true);
  540. }
  541. },
  542. ),
  543. ],
  544. ),
  545. ),
  546. _buttonInForm()
  547. ],
  548. ),
  549. );
  550. }
  551. _resetForm() {
  552. changeSupplyUsing.changeIndexEdit(-1);
  553. changeButton.resetValue();
  554. _dosageController.text = "";
  555. _howToUseController.text = "";
  556. _quantityController.text = "";
  557. changeUnit.initValue();
  558. changeSelectedSupply.initValue();
  559. changeSelectedDevice.initValue();
  560. changeFormField.change(false);
  561. }
  562. _hidenKeyboard(BuildContext context) {
  563. FocusScopeNode currentFocus = FocusScope.of(context);
  564. if (!currentFocus.hasPrimaryFocus) {
  565. currentFocus.unfocus();
  566. }
  567. }
  568. @override
  569. Widget build(BuildContext context) {
  570. return Column(
  571. crossAxisAlignment: CrossAxisAlignment.start,
  572. children: [
  573. Padding(
  574. padding: const EdgeInsets.all(8.0),
  575. child: Align(
  576. alignment: Alignment.centerLeft,
  577. child: Text(
  578. 'Thuốc BVTV',
  579. style: TextStyle(color: Colors.black54, fontSize: 14),
  580. ),
  581. ),
  582. ),
  583. _buildListSupply(),
  584. SizedBox(
  585. height: 8.0,
  586. ),
  587. _formEdit()
  588. ],
  589. );
  590. }
  591. }