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.

504 lines
21KB

  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
  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. Validators.stringNotNullOrEmpty(
  185. changeSelectedSupply.selectedSupplyName)
  186. ? Text(
  187. 'Hoá chất xử lý *',
  188. style: TextStyle(
  189. fontSize: 13,
  190. fontWeight: FontWeight.normal,
  191. color: isValid
  192. ? Colors.black54
  193. : Colors.red[600]),
  194. )
  195. : Text(
  196. '',
  197. style: TextStyle(
  198. fontSize: 13,
  199. fontWeight: FontWeight.normal,
  200. color: isValid
  201. ? Colors.black54
  202. : Colors.red[600]),
  203. ),
  204. Row(
  205. children: [
  206. Expanded(
  207. child: Validators.stringNotNullOrEmpty(
  208. changeSelectedSupply.selectedSupplyName)
  209. ? Text(
  210. changeSelectedSupply.selectedSupplyName,
  211. style: TextStyle(
  212. fontSize: 14.0,
  213. color: Colors.black87))
  214. : Text("Hoá chất xử lý *",
  215. style: TextStyle(
  216. fontSize: 14.0,
  217. color: Colors.black54)),
  218. ),
  219. Icon(
  220. Icons.arrow_drop_down,
  221. color: Colors.grey,
  222. ),
  223. ],
  224. )
  225. ],
  226. )),
  227. isValid ? SizedBox() : WidgetErrorTextField()
  228. ],
  229. );
  230. }));
  231. });
  232. }
  233. Widget _dropdownUnitTypes() {
  234. return GetBuilder<ChangeUnit>(builder: (data) {
  235. return DropdownButtonFormField<String>(
  236. itemHeight: 100,
  237. value: data.selectedUnit.isEmpty ? null : data.selectedUnit,
  238. items: data.currentUnits
  239. .map((label) => DropdownMenuItem(
  240. child: Text(label),
  241. value: label,
  242. ))
  243. .toList(),
  244. onChanged: (value) {
  245. var currentQuantity = _quantityController.text;
  246. num assignValue = currentQuantity.parseDoubleThousand();
  247. if (assignValue != null) {
  248. var oldSelected = data.selectedUnit;
  249. if (oldSelected == value) {
  250. } else {
  251. assignValue = UtilAction.convertUnit(
  252. inputValue: assignValue,
  253. oldUnit: oldSelected,
  254. newUnit: value);
  255. }
  256. _quantityController.text = assignValue.formatNumtoStringDecimal();
  257. }
  258. changeUnit.updateSelected(value);
  259. },
  260. );
  261. });
  262. }
  263. _quantityField() {
  264. return WidgetTextFormFieldNumber(
  265. hintValue: "Tổng lượng sử dụng *",
  266. labelText: "Tổng lượng sử dụng *",
  267. textController: _quantityController,
  268. validator: (String value) {
  269. return Validators.validateNotNullOrEmpty(
  270. value, label_validate_input_empty);
  271. },
  272. onChanged: (value) {
  273. if (!Validators.stringNotNullOrEmpty(value) &&
  274. !Validators.stringNotNullOrEmpty(_dosageController.text) &&
  275. Get.find<ChangeSupply>().selectedSupplyId <= 0) {
  276. changeFormField.change(false);
  277. } else {
  278. changeFormField.change(true);
  279. }
  280. },
  281. );
  282. }
  283. _buttonInForm() {
  284. return GetBuilder<ChangeButtonInForm>(builder: (_) {
  285. return Row(
  286. mainAxisAlignment: MainAxisAlignment.start,
  287. children: [
  288. _.isEdit
  289. ? OutlineButton(
  290. shape: RoundedRectangleBorder(
  291. borderRadius: new BorderRadius.circular(8.0)),
  292. child: Text("Huỷ"),
  293. onPressed: () {
  294. changeButton.resetValue();
  295. _resetForm();
  296. _hidenKeyboard(context);
  297. })
  298. : SizedBox(),
  299. _.isEdit
  300. ? Expanded(
  301. child: FlatButton(
  302. onPressed: () {
  303. if (_formSupplyKey.currentState.validate()) {
  304. _formSupplyKey.currentState.save();
  305. if (changeSelectedSupply.selectedSupplyId <= 0) {
  306. changeSelectedSupply.changeValid(false);
  307. } else {
  308. changeSelectedSupply.changeValid(true);
  309. }
  310. var currentSupply =
  311. changeSelectedSupply.currentSupply;
  312. if (currentSupply.id != null) {
  313. var quantityWithCurrentSupplyUnit =
  314. UtilAction.convertUnit(
  315. inputValue: _quantityController.text
  316. .parseDoubleThousand(),
  317. oldUnit: changeUnit.selectedUnit,
  318. newUnit: changeSelectedSupply
  319. .currentSupply.unit);
  320. SuppliesUsing newSup = SuppliesUsing()
  321. ..dosage = _dosageController.text
  322. ..quantity = quantityWithCurrentSupplyUnit
  323. ..tbSuppliesInWarehouseId = currentSupply.id
  324. ..suppliesInWarehouseId = currentSupply.id
  325. ..supplyName = currentSupply.tbSuppliesName
  326. ..supplyUnit = currentSupply.unit
  327. ..unit = currentSupply.unit;
  328. changeSupplyUsing.editSupply(
  329. changeSupplyUsing.currentIndex, newSup);
  330. _resetForm();
  331. _hidenKeyboard(context);
  332. }
  333. } else {
  334. Utils.showSnackBarWarning(
  335. message: "Vui lòng nhập hoá chất và số lượng");
  336. if (changeSelectedSupply.selectedSupplyId <= 0) {
  337. changeSelectedSupply.changeValid(false);
  338. } else {
  339. changeSelectedSupply.changeValid(true);
  340. }
  341. }
  342. },
  343. child: Text(
  344. "Sửa hoá chất xử lý",
  345. style: TextStyle(color: Colors.blue),
  346. )),
  347. )
  348. : Expanded(
  349. child: FlatButton(
  350. onPressed: () {
  351. if (_formSupplyKey.currentState.validate()) {
  352. _formSupplyKey.currentState.save();
  353. if (changeSelectedSupply.selectedSupplyId <= 0) {
  354. changeSelectedSupply.changeValid(false);
  355. } else {
  356. changeSelectedSupply.changeValid(true);
  357. }
  358. var currentSupply =
  359. changeSelectedSupply.currentSupply;
  360. if (currentSupply.id != null) {
  361. var quantityWithCurrentSupplyUnit =
  362. UtilAction.convertUnit(
  363. inputValue: _quantityController.text
  364. .parseDoubleThousand(),
  365. oldUnit: changeUnit.selectedUnit,
  366. newUnit: changeSelectedSupply
  367. .currentSupply.unit);
  368. SuppliesUsing newSup = SuppliesUsing()
  369. ..dosage = _dosageController.text
  370. ..quantity = quantityWithCurrentSupplyUnit
  371. ..tbSuppliesInWarehouseId = currentSupply.id
  372. ..suppliesInWarehouseId = currentSupply.id
  373. ..supplyName = currentSupply.tbSuppliesName
  374. ..supplyUnit = currentSupply.unit
  375. ..unit = currentSupply.unit;
  376. changeSupplyUsing.addSupply(newSup);
  377. _resetForm();
  378. _hidenKeyboard(context);
  379. }
  380. } else {
  381. Utils.showSnackBarWarning(
  382. message: "Vui lòng nhập hoá chất và số lượng");
  383. if (changeSelectedSupply.selectedSupplyId <= 0) {
  384. changeSelectedSupply.changeValid(false);
  385. } else {
  386. changeSelectedSupply.changeValid(true);
  387. }
  388. //
  389. }
  390. },
  391. child: Text(
  392. "+ Thêm hoá chất xử lý",
  393. style: TextStyle(color: Colors.blue),
  394. )),
  395. )
  396. ],
  397. );
  398. });
  399. }
  400. Widget _formEdit() {
  401. return Container(
  402. padding: EdgeInsets.all(8.0),
  403. margin: EdgeInsets.all(8.0),
  404. decoration: BoxDecoration(
  405. shape: BoxShape.rectangle,
  406. borderRadius: BorderRadius.circular(10),
  407. color: Colors.white,
  408. border: Border.all(color: Colors.grey[300])),
  409. child: Form(
  410. key: _formSupplyKey,
  411. child: Column(
  412. children: [
  413. _btnSelectSubstrates(),
  414. TextFormField(
  415. keyboardType: TextInputType.text,
  416. controller: _dosageController,
  417. decoration: InputDecoration(labelText: "Liều lượng"),
  418. onSaved: (newValue) {},
  419. onChanged: (value) {
  420. if (!Validators.stringNotNullOrEmpty(
  421. _quantityController.text) &&
  422. !Validators.stringNotNullOrEmpty(value) &&
  423. Get.find<ChangeSupply>().selectedSupplyId <= 0) {
  424. changeFormField.change(false);
  425. } else {
  426. changeFormField.change(true);
  427. }
  428. },
  429. ),
  430. Row(
  431. mainAxisSize: MainAxisSize.min,
  432. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  433. crossAxisAlignment: CrossAxisAlignment.center,
  434. children: [
  435. Expanded(
  436. flex: 2,
  437. child: Container(
  438. height: 86,
  439. child: _quantityField(),
  440. ),
  441. ),
  442. SizedBox(
  443. width: 16.0,
  444. ),
  445. Expanded(
  446. flex: 1,
  447. child: Align(
  448. alignment: Alignment.bottomCenter,
  449. child: _dropdownUnitTypes(),
  450. )),
  451. ]),
  452. _buttonInForm()
  453. ],
  454. ),
  455. ));
  456. }
  457. _resetForm() {
  458. changeSupplyUsing.changeIndexEdit(-1);
  459. changeButton.resetValue();
  460. _dosageController.text = "";
  461. _quantityController.text = "";
  462. changeUnit.initValue();
  463. changeSelectedSupply.initValue();
  464. changeFormField.change(false);
  465. }
  466. _hidenKeyboard(BuildContext context) {
  467. FocusScopeNode currentFocus = FocusScope.of(context);
  468. if (!currentFocus.hasPrimaryFocus) {
  469. currentFocus.unfocus();
  470. }
  471. }
  472. @override
  473. Widget build(BuildContext context) {
  474. return Column(
  475. children: [
  476. Padding(
  477. padding: const EdgeInsets.all(8.0),
  478. child: Align(
  479. alignment: Alignment.centerLeft,
  480. child: Text(
  481. 'Hoá chất xử lý',
  482. style: TextStyle(color: Colors.black54, fontSize: 14),
  483. ),
  484. ),
  485. ),
  486. _buildListSupply(),
  487. _formEdit()
  488. ],
  489. );
  490. }
  491. }