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.

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