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.

474 lines
20KB

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