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.

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