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