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.

594 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 Align(
  323. alignment: Alignment.centerRight,
  324. child: Row(
  325. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  326. children: [
  327. _.isEdit
  328. ? OutlineButton(
  329. shape: RoundedRectangleBorder(
  330. borderRadius: new BorderRadius.circular(8.0)),
  331. child: Text("Huỷ"),
  332. onPressed: () {
  333. changeButton.resetValue();
  334. _resetForm();
  335. _hidenKeyboard(context);
  336. })
  337. : SizedBox(),
  338. _.isEdit
  339. ? FlatButton(
  340. color: AppColors.DEFAULT,
  341. shape: RoundedRectangleBorder(
  342. borderRadius: new BorderRadius.circular(8.0)),
  343. onPressed: () {
  344. if (_formSupplyKey.currentState.validate()) {
  345. _formSupplyKey.currentState.save();
  346. if (changeSelectedSupply.selectedSupplyId <= 0) {
  347. changeSelectedSupply.changeValid(false);
  348. } else {
  349. changeSelectedSupply.changeValid(true);
  350. }
  351. var currentSupply = changeSelectedSupply.currentSupply;
  352. var currentDevice = changeSelectedDevice.currentDevice;
  353. var currentQuantity =
  354. _quantityController.text.parseDoubleThousand();
  355. if (currentSupply.id != null &&
  356. (currentQuantity ?? 0) > 0) {
  357. var quantityWithCurrentSupplyUnit =
  358. UtilAction.convertUnit(
  359. inputValue: currentQuantity,
  360. oldUnit: changeUnit.selectedUnit,
  361. newUnit:
  362. changeSelectedSupply.currentSupply.unit);
  363. SuppliesUsing newSup = SuppliesUsing()
  364. ..dosage = _dosageController.text
  365. ..howToUse = _howToUseController.text
  366. ..quantity = quantityWithCurrentSupplyUnit
  367. ..tbSuppliesInWarehouseId = currentSupply.id
  368. ..suppliesInWarehouseId = currentSupply.id
  369. ..supplyName = currentSupply.tbSuppliesName
  370. ..tbEquipmentOfCustomerId = currentDevice.id
  371. ..equipmentOfCustomerId = currentDevice.id
  372. ..equipmentName = currentDevice.name
  373. ..supplyUnit = currentSupply.unit
  374. ..unit = currentSupply.unit;
  375. changeSupplyUsing.editSupply(
  376. changeSupplyUsing.currentIndex, newSup);
  377. _resetForm();
  378. _hidenKeyboard(context);
  379. } else if (currentSupply.id == null ||
  380. ((currentQuantity ?? 0) <= 0)) {
  381. Utils.showSnackBarWarning(
  382. message: "Vui lòng nhập vật tư và số lượng");
  383. }
  384. } else {
  385. Utils.showSnackBarWarning(
  386. message: "Vui lòng nhập vật tư và số lượng");
  387. if (changeSelectedSupply.selectedSupplyId <= 0) {
  388. changeSelectedSupply.changeValid(false);
  389. } else {
  390. changeSelectedSupply.changeValid(true);
  391. }
  392. }
  393. },
  394. child: Text(
  395. "Sửa",
  396. style: TextStyle(color: Colors.white),
  397. ))
  398. : FlatButton(
  399. color: AppColors.DEFAULT,
  400. shape: RoundedRectangleBorder(
  401. borderRadius: new BorderRadius.circular(8.0)),
  402. onPressed: () {
  403. if (_formSupplyKey.currentState.validate()) {
  404. _formSupplyKey.currentState.save();
  405. if (changeSelectedSupply.selectedSupplyId <= 0) {
  406. changeSelectedSupply.changeValid(false);
  407. } else {
  408. changeSelectedSupply.changeValid(true);
  409. }
  410. var currentSupply = changeSelectedSupply.currentSupply;
  411. var currentDevice = changeSelectedDevice.currentDevice;
  412. var currentQuantity =
  413. _quantityController.text.parseDoubleThousand();
  414. if (currentSupply.id != null &&
  415. (currentQuantity ?? 0) > 0) {
  416. var quantityWithCurrentSupplyUnit =
  417. UtilAction.convertUnit(
  418. inputValue: currentQuantity,
  419. oldUnit: changeUnit.selectedUnit,
  420. newUnit:
  421. changeSelectedSupply.currentSupply.unit);
  422. SuppliesUsing newSup = SuppliesUsing()
  423. ..dosage = _dosageController.text
  424. ..howToUse = _howToUseController.text
  425. ..quantity = quantityWithCurrentSupplyUnit
  426. ..tbSuppliesInWarehouseId = currentSupply.id
  427. ..suppliesInWarehouseId = currentSupply.id
  428. ..supplyName = currentSupply.tbSuppliesName
  429. ..supplyUnit = currentSupply.unit
  430. ..tbEquipmentOfCustomerId = currentDevice.id
  431. ..equipmentOfCustomerId = currentDevice.id
  432. ..equipmentName = currentDevice.name
  433. ..unit = currentSupply.unit;
  434. changeSupplyUsing.addSupply(newSup);
  435. _resetForm();
  436. _hidenKeyboard(context);
  437. } else if (currentSupply.id == null ||
  438. ((currentQuantity ?? 0) <= 0)) {
  439. Utils.showSnackBarWarning(
  440. message: "Vui lòng nhập vật tư và số lượng");
  441. }
  442. } else {
  443. Utils.showSnackBarWarning(
  444. message: "Vui lòng nhập vật tư và số lượng");
  445. if (changeSelectedSupply.selectedSupplyId <= 0) {
  446. changeSelectedSupply.changeValid(false);
  447. } else {
  448. changeSelectedSupply.changeValid(true);
  449. }
  450. //
  451. }
  452. },
  453. child: Text(
  454. "Thêm",
  455. style: TextStyle(color: Colors.white),
  456. ))
  457. ],
  458. ),
  459. );
  460. });
  461. }
  462. Widget _formEdit() {
  463. return Container(
  464. padding: EdgeInsets.all(8.0),
  465. decoration: BoxDecoration(
  466. shape: BoxShape.rectangle,
  467. borderRadius: BorderRadius.circular(10),
  468. color: Colors.white,
  469. border: Border.all(color: AppColors.DEFAULT)),
  470. child: Form(
  471. key: _formSupplyKey,
  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: 70,
  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. _buttonInForm()
  544. ],
  545. ),
  546. ));
  547. }
  548. _resetForm() {
  549. changeSupplyUsing.changeIndexEdit(-1);
  550. changeButton.resetValue();
  551. _dosageController.text = "";
  552. _howToUseController.text = "";
  553. _quantityController.text = "";
  554. changeUnit.initValue();
  555. changeSelectedSupply.initValue();
  556. changeSelectedDevice.initValue();
  557. changeFormField.change(false);
  558. }
  559. _hidenKeyboard(BuildContext context) {
  560. FocusScopeNode currentFocus = FocusScope.of(context);
  561. if (!currentFocus.hasPrimaryFocus) {
  562. currentFocus.unfocus();
  563. }
  564. }
  565. @override
  566. Widget build(BuildContext context) {
  567. return Column(
  568. children: [
  569. _buildListSupply(),
  570. SizedBox(
  571. height: 8.0,
  572. ),
  573. _formEdit()
  574. ],
  575. );
  576. }
  577. }