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.

536 lines
22KB

  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. Navigator.of(context)
  164. .push(MaterialPageRoute(
  165. builder: (_) => ResourceHelperScreen(
  166. titleName: "Thuốc BVTV",
  167. type: ConstCommon.supplyTypeProtectPlant,
  168. selectedId: changeSelectedSupply.selectedSupplyId),
  169. fullscreenDialog: false))
  170. .then((value) {
  171. if (value != null) {
  172. var result = value as Supply;
  173. changeSelectedSupply.change(result);
  174. changeUnit.updateListByUnitName(result.unit);
  175. changeFormField.change(true);
  176. }
  177. });
  178. },
  179. child: Container(
  180. padding: EdgeInsets.only(
  181. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  182. decoration: BoxDecoration(
  183. border: kBorderTextField,
  184. ),
  185. child: Row(
  186. children: [
  187. GetBuilder<ChangeSupply>(
  188. builder: (_) => Expanded(
  189. child: Text(
  190. changeSelectedSupply.selectedSupplyName ??
  191. "Tên thương mại",
  192. style: TextStyle(
  193. fontSize: 14.0, color: Colors.black87)))),
  194. Icon(
  195. Icons.arrow_drop_down,
  196. color: Colors.grey,
  197. ),
  198. ],
  199. )));
  200. });
  201. }
  202. Widget _btnSelectDevice() {
  203. return GetBuilder<ChangeDevice>(builder: (data) {
  204. return FlatButton(
  205. padding:
  206. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  207. onPressed: () {
  208. Navigator.of(context)
  209. .push(MaterialPageRoute(
  210. builder: (_) => ListDeviceActivity(
  211. selectedId: changeSelectedDevice.selectedDeviceId),
  212. fullscreenDialog: false))
  213. .then((value) {
  214. if (value != null) {
  215. var result = value as Device;
  216. changeSelectedDevice.change(result);
  217. changeFormField.change(true);
  218. }
  219. });
  220. },
  221. child: Container(
  222. padding: EdgeInsets.only(
  223. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  224. decoration: BoxDecoration(
  225. border: kBorderTextField,
  226. ),
  227. child: Row(
  228. children: [
  229. GetBuilder<ChangeSupply>(
  230. builder: (_) => Expanded(
  231. child: Text(
  232. changeSelectedDevice.selectedDeviceName ??
  233. "Thiết bị",
  234. style: TextStyle(
  235. fontSize: 14.0, color: Colors.black87)))),
  236. Icon(
  237. Icons.arrow_drop_down,
  238. color: Colors.grey,
  239. ),
  240. ],
  241. )));
  242. });
  243. }
  244. Widget _dropdownUnitTypes() {
  245. return GetBuilder<ChangeUnit>(builder: (data) {
  246. return DropdownButtonFormField<String>(
  247. itemHeight: 100,
  248. value: data.selectedUnit.isEmpty ? null : data.selectedUnit,
  249. items: data.currentUnits
  250. .map((label) => DropdownMenuItem(
  251. child: Text(label),
  252. value: label,
  253. ))
  254. .toList(),
  255. onChanged: (value) {
  256. var currentQuantity = _quantityController.text;
  257. num assignValue = currentQuantity.parseDoubleThousand();
  258. if (assignValue != null) {
  259. var oldSelected = data.selectedUnit;
  260. if (oldSelected == value) {
  261. } else {
  262. assignValue = UtilAction.convertUnit(
  263. inputValue: assignValue,
  264. oldUnit: oldSelected,
  265. newUnit: value);
  266. }
  267. _quantityController.text = assignValue.formatNumtoStringDecimal();
  268. }
  269. changeUnit.updateSelected(value);
  270. },
  271. );
  272. });
  273. }
  274. _quantityField() {
  275. return WidgetTextFormFieldNumber(
  276. hintValue: "Tổng lượng sử dụng *",
  277. textController: _quantityController,
  278. onChanged: (value) {
  279. if (!Validators.stringNotNullOrEmpty(value) &&
  280. !Validators.stringNotNullOrEmpty(_howToUseController.text) &&
  281. !Validators.stringNotNullOrEmpty(_dosageController.text) &&
  282. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  283. changeSelectedDevice.selectedDeviceId <= 0) {
  284. changeFormField.change(false);
  285. } else {
  286. changeFormField.change(true);
  287. }
  288. },
  289. );
  290. }
  291. _buttonInForm() {
  292. return GetBuilder<ChangeButtonInForm>(builder: (_) {
  293. return Align(
  294. alignment: Alignment.centerRight,
  295. child: Row(
  296. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  297. children: [
  298. _.isEdit
  299. ? OutlineButton(
  300. shape: RoundedRectangleBorder(
  301. borderRadius: new BorderRadius.circular(8.0)),
  302. child: Text("Huỷ"),
  303. onPressed: () {
  304. changeButton.resetValue();
  305. _resetForm();
  306. _hidenKeyboard(context);
  307. })
  308. : SizedBox(),
  309. _.isEdit
  310. ? FlatButton(
  311. color: COLOR_CONST.DEFAULT,
  312. shape: RoundedRectangleBorder(
  313. borderRadius: new BorderRadius.circular(8.0)),
  314. onPressed: () {
  315. var currentSupply = changeSelectedSupply.currentSupply;
  316. var currentDevice = changeSelectedDevice.currentDevice;
  317. var currentQuantity =
  318. _quantityController.text.parseDoubleThousand();
  319. if (currentSupply.id != null &&
  320. (currentQuantity ?? 0) > 0) {
  321. var quantityWithCurrentSupplyUnit =
  322. UtilAction.convertUnit(
  323. inputValue: currentQuantity,
  324. oldUnit: changeUnit.selectedUnit,
  325. newUnit:
  326. changeSelectedSupply.currentSupply.unit);
  327. SuppliesUsing newSup = SuppliesUsing()
  328. ..dosage = _dosageController.text
  329. ..howToUse = _howToUseController.text
  330. ..quantity = quantityWithCurrentSupplyUnit
  331. ..tbSuppliesInWarehouseId = currentSupply.id
  332. ..suppliesInWarehouseId = currentSupply.id
  333. ..supplyName = currentSupply.tbSuppliesName
  334. ..tbEquipmentOfCustomerId = currentDevice.id
  335. ..equipmentOfCustomerId = currentDevice.id
  336. ..equipmentName = currentDevice.name
  337. ..supplyUnit = currentSupply.unit
  338. ..unit = currentSupply.unit;
  339. changeSupplyUsing.editSupply(
  340. changeSupplyUsing.currentIndex, newSup);
  341. _resetForm();
  342. _hidenKeyboard(context);
  343. } else if (currentSupply.id == null ||
  344. ((currentQuantity ?? 0) <= 0)) {
  345. Utils.showSnackBarWarning(
  346. message: "Vui lòng nhập vật tư và số lượng");
  347. }
  348. },
  349. child: Text(
  350. "Sửa",
  351. style: TextStyle(color: Colors.white),
  352. ))
  353. : FlatButton(
  354. color: COLOR_CONST.DEFAULT,
  355. shape: RoundedRectangleBorder(
  356. borderRadius: new BorderRadius.circular(8.0)),
  357. onPressed: () {
  358. var currentSupply = changeSelectedSupply.currentSupply;
  359. var currentDevice = changeSelectedDevice.currentDevice;
  360. var currentQuantity =
  361. _quantityController.text.parseDoubleThousand();
  362. if (currentSupply.id != null &&
  363. (currentQuantity ?? 0) > 0) {
  364. var quantityWithCurrentSupplyUnit =
  365. UtilAction.convertUnit(
  366. inputValue: currentQuantity,
  367. oldUnit: changeUnit.selectedUnit,
  368. newUnit:
  369. changeSelectedSupply.currentSupply.unit);
  370. SuppliesUsing newSup = SuppliesUsing()
  371. ..dosage = _dosageController.text
  372. ..howToUse = _howToUseController.text
  373. ..quantity = quantityWithCurrentSupplyUnit
  374. ..tbSuppliesInWarehouseId = currentSupply.id
  375. ..suppliesInWarehouseId = currentSupply.id
  376. ..supplyName = currentSupply.tbSuppliesName
  377. ..supplyUnit = currentSupply.unit
  378. ..tbEquipmentOfCustomerId = currentDevice.id
  379. ..equipmentOfCustomerId = currentDevice.id
  380. ..equipmentName = currentDevice.name
  381. ..unit = currentSupply.unit;
  382. changeSupplyUsing.addSupply(newSup);
  383. _resetForm();
  384. _hidenKeyboard(context);
  385. } else if (currentSupply.id == null ||
  386. ((currentQuantity ?? 0) <= 0)) {
  387. Utils.showSnackBarWarning(
  388. message: "Vui lòng nhập vật tư và số lượng");
  389. }
  390. },
  391. child: Text(
  392. "Thêm",
  393. style: TextStyle(color: Colors.white),
  394. ))
  395. ],
  396. ),
  397. );
  398. });
  399. }
  400. Widget _formEdit() {
  401. return Container(
  402. padding: EdgeInsets.all(8.0),
  403. decoration: BoxDecoration(
  404. shape: BoxShape.rectangle,
  405. borderRadius: BorderRadius.circular(10),
  406. color: Colors.white,
  407. border: Border.all(color: COLOR_CONST.DEFAULT)),
  408. child: Column(
  409. children: [
  410. Container(
  411. width: double.infinity,
  412. child: Text(
  413. "Tên thương mại *",
  414. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  415. ),
  416. ),
  417. _btnSelectSubstrates(),
  418. TextFormField(
  419. keyboardType: TextInputType.text,
  420. controller: _dosageController,
  421. decoration: InputDecoration(labelText: "Liều lượng sử dụng"),
  422. onSaved: (newValue) {},
  423. onChanged: (value) {
  424. if (!Validators.stringNotNullOrEmpty(
  425. _quantityController.text) &&
  426. !Validators.stringNotNullOrEmpty(
  427. _howToUseController.text) &&
  428. !Validators.stringNotNullOrEmpty(value) &&
  429. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  430. changeSelectedDevice.selectedDeviceId <= 0) {
  431. changeFormField.change(false);
  432. } else {
  433. changeFormField.change(true);
  434. }
  435. },
  436. ),
  437. Row(
  438. mainAxisSize: MainAxisSize.min,
  439. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  440. crossAxisAlignment: CrossAxisAlignment.center,
  441. children: [
  442. Expanded(
  443. flex: 2,
  444. child: Container(
  445. height: 70,
  446. child: _quantityField(),
  447. ),
  448. ),
  449. SizedBox(
  450. width: 16.0,
  451. ),
  452. Expanded(
  453. flex: 1,
  454. child: Align(
  455. alignment: Alignment.bottomCenter,
  456. child: _dropdownUnitTypes(),
  457. )),
  458. ]),
  459. Container(
  460. width: double.infinity,
  461. child: Text(
  462. "Thiết bị",
  463. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  464. ),
  465. ),
  466. _btnSelectDevice(),
  467. TextFormField(
  468. keyboardType: TextInputType.text,
  469. controller: _howToUseController,
  470. decoration: InputDecoration(labelText: "Phương pháp sử dụng"),
  471. onSaved: (newValue) {},
  472. onChanged: (value) {
  473. if (!Validators.stringNotNullOrEmpty(
  474. _quantityController.text) &&
  475. !Validators.stringNotNullOrEmpty(value) &&
  476. !Validators.stringNotNullOrEmpty(_dosageController.text) &&
  477. Get.find<ChangeSupply>().selectedSupplyId <= 0 &&
  478. changeSelectedDevice.selectedDeviceId <= 0) {
  479. changeFormField.change(false);
  480. } else {
  481. changeFormField.change(true);
  482. }
  483. },
  484. ),
  485. _buttonInForm()
  486. ],
  487. ));
  488. }
  489. _resetForm() {
  490. changeSupplyUsing.changeIndexEdit(-1);
  491. changeButton.resetValue();
  492. _dosageController.text = "";
  493. _howToUseController.text = "";
  494. _quantityController.text = "";
  495. changeUnit.initValue();
  496. changeSelectedSupply.initValue();
  497. changeSelectedDevice.initValue();
  498. changeFormField.change(false);
  499. }
  500. _hidenKeyboard(BuildContext context) {
  501. FocusScopeNode currentFocus = FocusScope.of(context);
  502. if (!currentFocus.hasPrimaryFocus) {
  503. currentFocus.unfocus();
  504. }
  505. }
  506. @override
  507. Widget build(BuildContext context) {
  508. return Column(
  509. children: [
  510. _buildListSupply(),
  511. SizedBox(
  512. height: 8.0,
  513. ),
  514. _formEdit()
  515. ],
  516. );
  517. }
  518. }