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.

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