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.

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