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.

543 lines
23KB

  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. @override
  40. void initState() {
  41. super.initState();
  42. changeSelectedSupply.initValue();
  43. changeSelectedDevice.initValue();
  44. changeSupplyUsing.init(widget.currentItems);
  45. changeUnit.initValue();
  46. changeButton.resetValue();
  47. changeFormField.init();
  48. }
  49. Widget _buildListSupply() {
  50. return GetBuilder<ChangeSupplyUsing>(builder: (value) {
  51. widget.onChangeSupplies(value.currentItems);
  52. if (value.currentItems.isEmpty) {
  53. return Container();
  54. } else {
  55. return Container(
  56. height: 120,
  57. child: ListView.builder(
  58. physics: ClampingScrollPhysics(),
  59. scrollDirection: Axis.horizontal,
  60. shrinkWrap: true,
  61. itemCount: value.currentItems.length,
  62. itemBuilder: (context, index) {
  63. return GestureDetector(
  64. onTap: () {
  65. print("edit");
  66. changeSupplyUsing.changeIndexEdit(index);
  67. changeButton.updateToEdit(true);
  68. var editedSupplyUsing = value.currentItems[index];
  69. var editedSupply = Supply()
  70. ..id = editedSupplyUsing.tbSuppliesInWarehouseId
  71. ..tbSuppliesName = editedSupplyUsing.supplyName
  72. ..unit = editedSupplyUsing.supplyUnit;
  73. changeSelectedSupply.change(editedSupply);
  74. var editedDevice = Device()
  75. ..id = editedSupplyUsing.tbEquipmentOfCustomerId
  76. ..name = editedSupplyUsing.equipmentName;
  77. changeSelectedDevice.change(editedDevice);
  78. changeUnit
  79. .updateListByUnitName(editedSupplyUsing.supplyUnit);
  80. changeUnit.updateSelected(editedSupplyUsing.supplyUnit);
  81. _dosageController.text = editedSupplyUsing.dosage;
  82. _howToUseController.text = editedSupplyUsing.howToUse;
  83. _quantityController.text = editedSupplyUsing.quantity
  84. .formatNumtoStringDecimal();
  85. },
  86. child: Card(
  87. child: Stack(
  88. alignment: Alignment.bottomCenter,
  89. overflow: Overflow.visible,
  90. children: <Widget>[
  91. Positioned(
  92. child: ClipRRect(
  93. borderRadius: BorderRadius.circular(8),
  94. child: Container(
  95. padding: EdgeInsets.all(4),
  96. width: 150,
  97. child: Column(
  98. mainAxisAlignment: MainAxisAlignment.center,
  99. children: [
  100. SizedBox(
  101. height: 12.0,
  102. ),
  103. Flexible(
  104. child: Text(
  105. value.currentItems[index].supplyName ??
  106. "",
  107. overflow: TextOverflow.ellipsis,
  108. maxLines: 1),
  109. ),
  110. Validators.stringNotNullOrEmpty(
  111. value.currentItems[index].dosage)
  112. ? Flexible(
  113. child: Text(
  114. "${value.currentItems[index].dosage ?? ""}"))
  115. : SizedBox(),
  116. Validators.stringNotNullOrEmpty(value
  117. .currentItems[index].quantity
  118. .formatNumtoStringDecimal())
  119. ? Flexible(
  120. child: Text(
  121. "${value.currentItems[index].quantity.formatNumtoStringDecimal() ?? ""} ${value.currentItems[index].supplyUnit ?? ""}"))
  122. : SizedBox(),
  123. Validators.stringNotNullOrEmpty(value
  124. .currentItems[index].equipmentName)
  125. ? Flexible(
  126. child: Text(
  127. "${value.currentItems[index].equipmentName ?? ""}"))
  128. : SizedBox(),
  129. Validators.stringNotNullOrEmpty(
  130. value.currentItems[index].howToUse)
  131. ? Flexible(
  132. child: Text(
  133. "${value.currentItems[index].howToUse ?? ""}"))
  134. : SizedBox(),
  135. ],
  136. ),
  137. ),
  138. )),
  139. Positioned(
  140. top: -10,
  141. right: -10,
  142. child: IconButton(
  143. icon: Icon(
  144. Icons.cancel,
  145. color: Colors.redAccent,
  146. ),
  147. onPressed: () {
  148. changeSupplyUsing.deleteSupply(index);
  149. }),
  150. )
  151. ],
  152. )));
  153. }));
  154. }
  155. });
  156. }
  157. Widget _btnSelectSubstrates() {
  158. return GetBuilder<ChangeSupply>(builder: (data) {
  159. return FlatButton(
  160. padding:
  161. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  162. onPressed: () {
  163. var currentIndexEdit = changeSupplyUsing.currentIndex;
  164. Navigator.of(context)
  165. .push(MaterialPageRoute(
  166. builder: (_) => ResourceHelperScreen(
  167. titleName: "Thuốc BVTV",
  168. type: ConstCommon.supplyTypeProtectPlant,
  169. selectedId: changeSelectedSupply.selectedSupplyId,
  170. currentItems: changeSupplyUsing.currentItems,
  171. currentEditId: (currentIndexEdit >= 0)
  172. ? changeSupplyUsing.currentItems[currentIndexEdit]
  173. .tbSuppliesInWarehouseId
  174. : -1,
  175. ),
  176. fullscreenDialog: false))
  177. .then((value) {
  178. if (value != null) {
  179. var result = value as Supply;
  180. changeSelectedSupply.change(result);
  181. changeUnit.updateListByUnitName(result.unit);
  182. changeFormField.change(true);
  183. }
  184. });
  185. },
  186. child: Container(
  187. padding: EdgeInsets.only(
  188. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  189. decoration: BoxDecoration(
  190. border: kBorderTextField,
  191. ),
  192. child: Row(
  193. children: [
  194. GetBuilder<ChangeSupply>(
  195. builder: (_) => Expanded(
  196. child: Text(
  197. changeSelectedSupply.selectedSupplyName ??
  198. "Tên thương mại",
  199. style: TextStyle(
  200. fontSize: 14.0, color: Colors.black87)))),
  201. Icon(
  202. Icons.arrow_drop_down,
  203. color: Colors.grey,
  204. ),
  205. ],
  206. )));
  207. });
  208. }
  209. Widget _btnSelectDevice() {
  210. return GetBuilder<ChangeDevice>(builder: (data) {
  211. return FlatButton(
  212. padding:
  213. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  214. onPressed: () {
  215. Navigator.of(context)
  216. .push(MaterialPageRoute(
  217. builder: (_) => ListDeviceActivity(
  218. selectedId: changeSelectedDevice.selectedDeviceId),
  219. fullscreenDialog: false))
  220. .then((value) {
  221. if (value != null) {
  222. var result = value as Device;
  223. changeSelectedDevice.change(result);
  224. changeFormField.change(true);
  225. }
  226. });
  227. },
  228. child: Container(
  229. padding: EdgeInsets.only(
  230. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  231. decoration: BoxDecoration(
  232. border: kBorderTextField,
  233. ),
  234. child: Row(
  235. children: [
  236. GetBuilder<ChangeSupply>(
  237. builder: (_) => Expanded(
  238. child: Text(
  239. changeSelectedDevice.selectedDeviceName ??
  240. "Thiết bị",
  241. style: TextStyle(
  242. fontSize: 14.0, color: Colors.black87)))),
  243. Icon(
  244. Icons.arrow_drop_down,
  245. color: Colors.grey,
  246. ),
  247. ],
  248. )));
  249. });
  250. }
  251. Widget _dropdownUnitTypes() {
  252. return GetBuilder<ChangeUnit>(builder: (data) {
  253. return DropdownButtonFormField<String>(
  254. itemHeight: 100,
  255. value: data.selectedUnit.isEmpty ? null : data.selectedUnit,
  256. items: data.currentUnits
  257. .map((label) => DropdownMenuItem(
  258. child: Text(label),
  259. value: label,
  260. ))
  261. .toList(),
  262. onChanged: (value) {
  263. var currentQuantity = _quantityController.text;
  264. num assignValue = currentQuantity.parseDoubleThousand();
  265. if (assignValue != null) {
  266. var oldSelected = data.selectedUnit;
  267. if (oldSelected == value) {
  268. } else {
  269. assignValue = UtilAction.convertUnit(
  270. inputValue: assignValue,
  271. oldUnit: oldSelected,
  272. newUnit: value);
  273. }
  274. _quantityController.text = assignValue.formatNumtoStringDecimal();
  275. }
  276. changeUnit.updateSelected(value);
  277. },
  278. );
  279. });
  280. }
  281. _quantityField() {
  282. return WidgetTextFormFieldNumber(
  283. hintValue: "Tổng lượng sử dụng *",
  284. textController: _quantityController,
  285. onChanged: (value) {
  286. if (!Validators.stringNotNullOrEmpty(value) &&
  287. !Validators.stringNotNullOrEmpty(_howToUseController.text) &&
  288. !Validators.stringNotNullOrEmpty(_dosageController.text) &&
  289. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  290. changeSelectedDevice.selectedDeviceId <= 0) {
  291. changeFormField.change(false);
  292. } else {
  293. changeFormField.change(true);
  294. }
  295. },
  296. );
  297. }
  298. _buttonInForm() {
  299. return GetBuilder<ChangeButtonInForm>(builder: (_) {
  300. return Align(
  301. alignment: Alignment.centerRight,
  302. child: Row(
  303. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  304. children: [
  305. _.isEdit
  306. ? OutlineButton(
  307. shape: RoundedRectangleBorder(
  308. borderRadius: new BorderRadius.circular(8.0)),
  309. child: Text("Huỷ"),
  310. onPressed: () {
  311. changeButton.resetValue();
  312. _resetForm();
  313. _hidenKeyboard(context);
  314. })
  315. : SizedBox(),
  316. _.isEdit
  317. ? FlatButton(
  318. color: COLOR_CONST.DEFAULT,
  319. shape: RoundedRectangleBorder(
  320. borderRadius: new BorderRadius.circular(8.0)),
  321. onPressed: () {
  322. var currentSupply = changeSelectedSupply.currentSupply;
  323. var currentDevice = changeSelectedDevice.currentDevice;
  324. var currentQuantity =
  325. _quantityController.text.parseDoubleThousand();
  326. if (currentSupply.id != null &&
  327. (currentQuantity ?? 0) > 0) {
  328. var quantityWithCurrentSupplyUnit =
  329. UtilAction.convertUnit(
  330. inputValue: currentQuantity,
  331. oldUnit: changeUnit.selectedUnit,
  332. newUnit:
  333. changeSelectedSupply.currentSupply.unit);
  334. SuppliesUsing newSup = SuppliesUsing()
  335. ..dosage = _dosageController.text
  336. ..howToUse = _howToUseController.text
  337. ..quantity = quantityWithCurrentSupplyUnit
  338. ..tbSuppliesInWarehouseId = currentSupply.id
  339. ..suppliesInWarehouseId = currentSupply.id
  340. ..supplyName = currentSupply.tbSuppliesName
  341. ..tbEquipmentOfCustomerId = currentDevice.id
  342. ..equipmentOfCustomerId = currentDevice.id
  343. ..equipmentName = currentDevice.name
  344. ..supplyUnit = currentSupply.unit
  345. ..unit = currentSupply.unit;
  346. changeSupplyUsing.editSupply(
  347. changeSupplyUsing.currentIndex, newSup);
  348. _resetForm();
  349. _hidenKeyboard(context);
  350. } else if (currentSupply.id == null ||
  351. ((currentQuantity ?? 0) <= 0)) {
  352. Utils.showSnackBarWarning(
  353. message: "Vui lòng nhập vật tư và số lượng");
  354. }
  355. },
  356. child: Text(
  357. "Sửa",
  358. style: TextStyle(color: Colors.white),
  359. ))
  360. : FlatButton(
  361. color: COLOR_CONST.DEFAULT,
  362. shape: RoundedRectangleBorder(
  363. borderRadius: new BorderRadius.circular(8.0)),
  364. onPressed: () {
  365. var currentSupply = changeSelectedSupply.currentSupply;
  366. var currentDevice = changeSelectedDevice.currentDevice;
  367. var currentQuantity =
  368. _quantityController.text.parseDoubleThousand();
  369. if (currentSupply.id != null &&
  370. (currentQuantity ?? 0) > 0) {
  371. var quantityWithCurrentSupplyUnit =
  372. UtilAction.convertUnit(
  373. inputValue: currentQuantity,
  374. oldUnit: changeUnit.selectedUnit,
  375. newUnit:
  376. changeSelectedSupply.currentSupply.unit);
  377. SuppliesUsing newSup = SuppliesUsing()
  378. ..dosage = _dosageController.text
  379. ..howToUse = _howToUseController.text
  380. ..quantity = quantityWithCurrentSupplyUnit
  381. ..tbSuppliesInWarehouseId = currentSupply.id
  382. ..suppliesInWarehouseId = currentSupply.id
  383. ..supplyName = currentSupply.tbSuppliesName
  384. ..supplyUnit = currentSupply.unit
  385. ..tbEquipmentOfCustomerId = currentDevice.id
  386. ..equipmentOfCustomerId = currentDevice.id
  387. ..equipmentName = currentDevice.name
  388. ..unit = currentSupply.unit;
  389. changeSupplyUsing.addSupply(newSup);
  390. _resetForm();
  391. _hidenKeyboard(context);
  392. } else if (currentSupply.id == null ||
  393. ((currentQuantity ?? 0) <= 0)) {
  394. Utils.showSnackBarWarning(
  395. message: "Vui lòng nhập vật tư và số lượng");
  396. }
  397. },
  398. child: Text(
  399. "Thêm",
  400. style: TextStyle(color: Colors.white),
  401. ))
  402. ],
  403. ),
  404. );
  405. });
  406. }
  407. Widget _formEdit() {
  408. return Container(
  409. padding: EdgeInsets.all(8.0),
  410. decoration: BoxDecoration(
  411. shape: BoxShape.rectangle,
  412. borderRadius: BorderRadius.circular(10),
  413. color: Colors.white,
  414. border: Border.all(color: COLOR_CONST.DEFAULT)),
  415. child: Column(
  416. children: [
  417. Container(
  418. width: double.infinity,
  419. child: Text(
  420. "Tên thương mại *",
  421. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  422. ),
  423. ),
  424. _btnSelectSubstrates(),
  425. TextFormField(
  426. keyboardType: TextInputType.text,
  427. controller: _dosageController,
  428. decoration: InputDecoration(labelText: "Liều lượng sử dụng"),
  429. onSaved: (newValue) {},
  430. onChanged: (value) {
  431. if (!Validators.stringNotNullOrEmpty(
  432. _quantityController.text) &&
  433. !Validators.stringNotNullOrEmpty(
  434. _howToUseController.text) &&
  435. !Validators.stringNotNullOrEmpty(value) &&
  436. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  437. changeSelectedDevice.selectedDeviceId <= 0) {
  438. changeFormField.change(false);
  439. } else {
  440. changeFormField.change(true);
  441. }
  442. },
  443. ),
  444. Row(
  445. mainAxisSize: MainAxisSize.min,
  446. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  447. crossAxisAlignment: CrossAxisAlignment.center,
  448. children: [
  449. Expanded(
  450. flex: 2,
  451. child: Container(
  452. height: 70,
  453. child: _quantityField(),
  454. ),
  455. ),
  456. SizedBox(
  457. width: 16.0,
  458. ),
  459. Expanded(
  460. flex: 1,
  461. child: Align(
  462. alignment: Alignment.bottomCenter,
  463. child: _dropdownUnitTypes(),
  464. )),
  465. ]),
  466. Container(
  467. width: double.infinity,
  468. child: Text(
  469. "Thiết bị",
  470. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  471. ),
  472. ),
  473. _btnSelectDevice(),
  474. TextFormField(
  475. keyboardType: TextInputType.text,
  476. controller: _howToUseController,
  477. decoration: InputDecoration(labelText: "Phương pháp sử dụng"),
  478. onSaved: (newValue) {},
  479. onChanged: (value) {
  480. if (!Validators.stringNotNullOrEmpty(
  481. _quantityController.text) &&
  482. !Validators.stringNotNullOrEmpty(value) &&
  483. !Validators.stringNotNullOrEmpty(_dosageController.text) &&
  484. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  485. changeSelectedDevice.selectedDeviceId <= 0) {
  486. changeFormField.change(false);
  487. } else {
  488. changeFormField.change(true);
  489. }
  490. },
  491. ),
  492. _buttonInForm()
  493. ],
  494. ));
  495. }
  496. _resetForm() {
  497. changeSupplyUsing.changeIndexEdit(-1);
  498. changeButton.resetValue();
  499. _dosageController.text = "";
  500. _howToUseController.text = "";
  501. _quantityController.text = "";
  502. changeUnit.initValue();
  503. changeSelectedSupply.initValue();
  504. changeSelectedDevice.initValue();
  505. changeFormField.change(false);
  506. }
  507. _hidenKeyboard(BuildContext context) {
  508. FocusScopeNode currentFocus = FocusScope.of(context);
  509. if (!currentFocus.hasPrimaryFocus) {
  510. currentFocus.unfocus();
  511. }
  512. }
  513. @override
  514. Widget build(BuildContext context) {
  515. return Column(
  516. children: [
  517. _buildListSupply(),
  518. SizedBox(
  519. height: 8.0,
  520. ),
  521. _formEdit()
  522. ],
  523. );
  524. }
  525. }