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.

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