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.

423 lines
17KB

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