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.

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