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.

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