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.

487 lines
22KB

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