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.

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