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.

417 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 Align(
  233. alignment: Alignment.centerRight,
  234. child: Row(
  235. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  236. children: [
  237. _.isEdit
  238. ? OutlineButton(
  239. shape: RoundedRectangleBorder(
  240. borderRadius: new BorderRadius.circular(8.0)),
  241. child: Text("Huỷ"),
  242. onPressed: () {
  243. changeButton.resetValue();
  244. _resetForm();
  245. _hidenKeyboard(context);
  246. })
  247. : SizedBox(),
  248. _.isEdit
  249. ? FlatButton(
  250. color: AppColors.DEFAULT,
  251. shape: RoundedRectangleBorder(
  252. borderRadius: new BorderRadius.circular(8.0)),
  253. onPressed: () {
  254. var currentSupply = changeSelectedSupply.currentSupply;
  255. if (currentSupply.id != null) {
  256. var quantityWithCurrentSupplyUnit =
  257. UtilAction.convertUnit(
  258. inputValue: _quantityController.text
  259. .parseDoubleThousand(),
  260. oldUnit: changeUnit.selectedUnit,
  261. newUnit:
  262. changeSelectedSupply.currentSupply.unit);
  263. SuppliesUsing newSup = SuppliesUsing()
  264. ..dosage = _dosageController.text
  265. ..quantity = quantityWithCurrentSupplyUnit
  266. ..tbSuppliesInWarehouseId = currentSupply.id
  267. ..suppliesInWarehouseId = currentSupply.id
  268. ..supplyName = currentSupply.tbSuppliesName
  269. ..supplyUnit = currentSupply.unit
  270. ..unit = currentSupply.unit;
  271. changeSupplyUsing.editSupply(
  272. changeSupplyUsing.currentIndex, newSup);
  273. _resetForm();
  274. _hidenKeyboard(context);
  275. }
  276. },
  277. child: Text(
  278. "Sửa",
  279. style: TextStyle(color: Colors.white),
  280. ))
  281. : FlatButton(
  282. color: AppColors.DEFAULT,
  283. shape: RoundedRectangleBorder(
  284. borderRadius: new BorderRadius.circular(8.0)),
  285. onPressed: () {
  286. var currentSupply = changeSelectedSupply.currentSupply;
  287. if (currentSupply.id != null) {
  288. var quantityWithCurrentSupplyUnit =
  289. UtilAction.convertUnit(
  290. inputValue: _quantityController.text
  291. .parseDoubleThousand(),
  292. oldUnit: changeUnit.selectedUnit,
  293. newUnit:
  294. changeSelectedSupply.currentSupply.unit);
  295. SuppliesUsing newSup = SuppliesUsing()
  296. ..dosage = _dosageController.text
  297. ..quantity = quantityWithCurrentSupplyUnit
  298. ..tbSuppliesInWarehouseId = currentSupply.id
  299. ..suppliesInWarehouseId = currentSupply.id
  300. ..supplyName = currentSupply.tbSuppliesName
  301. ..supplyUnit = currentSupply.unit
  302. ..unit = currentSupply.unit;
  303. changeSupplyUsing.addSupply(newSup);
  304. _resetForm();
  305. _hidenKeyboard(context);
  306. }
  307. },
  308. child: Text(
  309. "Thêm",
  310. style: TextStyle(color: Colors.white),
  311. ))
  312. ],
  313. ),
  314. );
  315. });
  316. }
  317. Widget _formEdit() {
  318. return Container(
  319. padding: EdgeInsets.all(8.0),
  320. decoration: BoxDecoration(
  321. shape: BoxShape.rectangle,
  322. borderRadius: BorderRadius.circular(10),
  323. color: Colors.white,
  324. border: Border.all(color: AppColors.DEFAULT)),
  325. child: Column(
  326. children: [
  327. Container(
  328. width: double.infinity,
  329. child: Text(
  330. "Hoá chất xử lý",
  331. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  332. ),
  333. ),
  334. _btnSelectSubstrates(),
  335. TextFormField(
  336. keyboardType: TextInputType.text,
  337. controller: _dosageController,
  338. decoration: InputDecoration(labelText: "Liều lượng"),
  339. onSaved: (newValue) {},
  340. onChanged: (value) {
  341. if (!Validators.stringNotNullOrEmpty(
  342. _quantityController.text) &&
  343. !Validators.stringNotNullOrEmpty(value) &&
  344. Get.find<ChangeSupply>().selectedSupplyId <= 0) {
  345. changeFormField.change(false);
  346. } else {
  347. changeFormField.change(true);
  348. }
  349. },
  350. ),
  351. Row(
  352. mainAxisSize: MainAxisSize.min,
  353. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  354. crossAxisAlignment: CrossAxisAlignment.center,
  355. children: [
  356. Expanded(
  357. flex: 2,
  358. child: Container(
  359. height: 70,
  360. child: _quantityField(),
  361. ),
  362. ),
  363. SizedBox(
  364. width: 16.0,
  365. ),
  366. Expanded(
  367. flex: 1,
  368. child: Align(
  369. alignment: Alignment.bottomCenter,
  370. child: _dropdownUnitTypes(),
  371. )),
  372. ]),
  373. _buttonInForm()
  374. ],
  375. ));
  376. }
  377. _resetForm() {
  378. changeSupplyUsing.changeIndexEdit(-1);
  379. changeButton.resetValue();
  380. _dosageController.text = "";
  381. _quantityController.text = "";
  382. changeUnit.initValue();
  383. changeSelectedSupply.initValue();
  384. changeFormField.change(false);
  385. }
  386. _hidenKeyboard(BuildContext context) {
  387. FocusScopeNode currentFocus = FocusScope.of(context);
  388. if (!currentFocus.hasPrimaryFocus) {
  389. currentFocus.unfocus();
  390. }
  391. }
  392. @override
  393. Widget build(BuildContext context) {
  394. return Column(
  395. children: [
  396. _buildListSupply(),
  397. SizedBox(
  398. height: 8.0,
  399. ),
  400. _formEdit()
  401. ],
  402. );
  403. }
  404. }