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.

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