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.

409 lines
16KB

  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. Navigator.of(context)
  140. .push(MaterialPageRoute(
  141. builder: (_) => ResourceHelperScreen(
  142. titleName: "Hoá chất xử lý",
  143. type: ConstCommon.supplyTypeAll,
  144. selectedId: changeSelectedSupply.selectedSupplyId),
  145. fullscreenDialog: false))
  146. .then((value) {
  147. if (value != null) {
  148. var result = value as Supply;
  149. changeSelectedSupply.change(result);
  150. changeUnit.updateListByUnitName(result.unit);
  151. changeFormField.change(true);
  152. }
  153. });
  154. },
  155. child: Container(
  156. padding: EdgeInsets.only(
  157. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  158. decoration: BoxDecoration(
  159. border: kBorderTextField,
  160. ),
  161. child: Row(
  162. children: [
  163. GetBuilder<ChangeSupply>(
  164. builder: (_) => Expanded(
  165. child: Text(
  166. changeSelectedSupply.selectedSupplyName ??
  167. "Hoá chất xử lý",
  168. style: TextStyle(
  169. fontSize: 14.0, color: Colors.black87)))),
  170. Icon(
  171. Icons.arrow_drop_down,
  172. color: Colors.grey,
  173. ),
  174. ],
  175. )));
  176. });
  177. }
  178. Widget _dropdownUnitTypes() {
  179. return GetBuilder<ChangeUnit>(builder: (data) {
  180. return DropdownButtonFormField<String>(
  181. itemHeight: 100,
  182. value: data.selectedUnit.isEmpty ? null : data.selectedUnit,
  183. items: data.currentUnits
  184. .map((label) => DropdownMenuItem(
  185. child: Text(label),
  186. value: label,
  187. ))
  188. .toList(),
  189. onChanged: (value) {
  190. var currentQuantity = _quantityController.text;
  191. num assignValue = currentQuantity.parseDoubleThousand();
  192. if (assignValue != null) {
  193. var oldSelected = data.selectedUnit;
  194. if (oldSelected == value) {
  195. } else {
  196. assignValue = UtilAction.convertUnit(
  197. inputValue: assignValue,
  198. oldUnit: oldSelected,
  199. newUnit: value);
  200. }
  201. _quantityController.text = assignValue.formatNumtoStringDecimal();
  202. }
  203. changeUnit.updateSelected(value);
  204. },
  205. );
  206. });
  207. }
  208. _quantityField() {
  209. return WidgetTextFormFieldNumber(
  210. hintValue: "Tổng lượng sử dụng",
  211. textController: _quantityController,
  212. onChanged: (value) {
  213. if (!Validators.stringNotNullOrEmpty(value) &&
  214. !Validators.stringNotNullOrEmpty(_dosageController.text) &&
  215. Get.find<ChangeSupply>().selectedSupplyId <= 0) {
  216. changeFormField.change(false);
  217. } else {
  218. changeFormField.change(true);
  219. }
  220. },
  221. );
  222. }
  223. _buttonInForm() {
  224. return GetBuilder<ChangeButtonInForm>(builder: (_) {
  225. return Align(
  226. alignment: Alignment.centerRight,
  227. child: Row(
  228. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  229. children: [
  230. _.isEdit
  231. ? OutlineButton(
  232. shape: RoundedRectangleBorder(
  233. borderRadius: new BorderRadius.circular(8.0)),
  234. child: Text("Huỷ"),
  235. onPressed: () {
  236. changeButton.resetValue();
  237. _resetForm();
  238. _hidenKeyboard(context);
  239. })
  240. : SizedBox(),
  241. _.isEdit
  242. ? FlatButton(
  243. color: COLOR_CONST.DEFAULT,
  244. shape: RoundedRectangleBorder(
  245. borderRadius: new BorderRadius.circular(8.0)),
  246. onPressed: () {
  247. var currentSupply = changeSelectedSupply.currentSupply;
  248. if (currentSupply.id != null) {
  249. var quantityWithCurrentSupplyUnit =
  250. UtilAction.convertUnit(
  251. inputValue: _quantityController.text
  252. .parseDoubleThousand(),
  253. oldUnit: changeUnit.selectedUnit,
  254. newUnit:
  255. changeSelectedSupply.currentSupply.unit);
  256. SuppliesUsing newSup = SuppliesUsing()
  257. ..dosage = _dosageController.text
  258. ..quantity = quantityWithCurrentSupplyUnit
  259. ..tbSuppliesInWarehouseId = currentSupply.id
  260. ..suppliesInWarehouseId = currentSupply.id
  261. ..supplyName = currentSupply.tbSuppliesName
  262. ..supplyUnit = currentSupply.unit
  263. ..unit = currentSupply.unit;
  264. changeSupplyUsing.editSupply(
  265. changeSupplyUsing.currentIndex, newSup);
  266. _resetForm();
  267. _hidenKeyboard(context);
  268. }
  269. },
  270. child: Text(
  271. "Sửa",
  272. style: TextStyle(color: Colors.white),
  273. ))
  274. : FlatButton(
  275. color: COLOR_CONST.DEFAULT,
  276. shape: RoundedRectangleBorder(
  277. borderRadius: new BorderRadius.circular(8.0)),
  278. onPressed: () {
  279. var currentSupply = changeSelectedSupply.currentSupply;
  280. if (currentSupply.id != null) {
  281. var quantityWithCurrentSupplyUnit =
  282. UtilAction.convertUnit(
  283. inputValue: _quantityController.text
  284. .parseDoubleThousand(),
  285. oldUnit: changeUnit.selectedUnit,
  286. newUnit:
  287. changeSelectedSupply.currentSupply.unit);
  288. SuppliesUsing newSup = SuppliesUsing()
  289. ..dosage = _dosageController.text
  290. ..quantity = quantityWithCurrentSupplyUnit
  291. ..tbSuppliesInWarehouseId = currentSupply.id
  292. ..suppliesInWarehouseId = currentSupply.id
  293. ..supplyName = currentSupply.tbSuppliesName
  294. ..supplyUnit = currentSupply.unit
  295. ..unit = currentSupply.unit;
  296. changeSupplyUsing.addSupply(newSup);
  297. _resetForm();
  298. _hidenKeyboard(context);
  299. }
  300. },
  301. child: Text(
  302. "Thêm",
  303. style: TextStyle(color: Colors.white),
  304. ))
  305. ],
  306. ),
  307. );
  308. });
  309. }
  310. Widget _formEdit() {
  311. return Container(
  312. padding: EdgeInsets.all(8.0),
  313. decoration: BoxDecoration(
  314. shape: BoxShape.rectangle,
  315. borderRadius: BorderRadius.circular(10),
  316. color: Colors.white,
  317. border: Border.all(color: COLOR_CONST.DEFAULT)),
  318. child: Column(
  319. children: [
  320. Container(
  321. width: double.infinity,
  322. child: Text(
  323. "Hoá chất xử lý",
  324. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  325. ),
  326. ),
  327. _btnSelectSubstrates(),
  328. TextFormField(
  329. keyboardType: TextInputType.text,
  330. controller: _dosageController,
  331. decoration: InputDecoration(labelText: "Liều lượng"),
  332. onSaved: (newValue) {},
  333. onChanged: (value) {
  334. if (!Validators.stringNotNullOrEmpty(
  335. _quantityController.text) &&
  336. !Validators.stringNotNullOrEmpty(value) &&
  337. Get.find<ChangeSupply>().selectedSupplyId <= 0) {
  338. changeFormField.change(false);
  339. } else {
  340. changeFormField.change(true);
  341. }
  342. },
  343. ),
  344. Row(
  345. mainAxisSize: MainAxisSize.min,
  346. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  347. crossAxisAlignment: CrossAxisAlignment.center,
  348. children: [
  349. Expanded(
  350. flex: 2,
  351. child: Container(
  352. height: 70,
  353. child: _quantityField(),
  354. ),
  355. ),
  356. SizedBox(
  357. width: 16.0,
  358. ),
  359. Expanded(
  360. flex: 1,
  361. child: Align(
  362. alignment: Alignment.bottomCenter,
  363. child: _dropdownUnitTypes(),
  364. )),
  365. ]),
  366. _buttonInForm()
  367. ],
  368. ));
  369. }
  370. _resetForm() {
  371. changeSupplyUsing.changeIndexEdit(-1);
  372. changeButton.resetValue();
  373. _dosageController.text = "";
  374. _quantityController.text = "";
  375. changeUnit.initValue();
  376. changeSelectedSupply.initValue();
  377. }
  378. _hidenKeyboard(BuildContext context) {
  379. FocusScopeNode currentFocus = FocusScope.of(context);
  380. if (!currentFocus.hasPrimaryFocus) {
  381. currentFocus.unfocus();
  382. }
  383. }
  384. @override
  385. Widget build(BuildContext context) {
  386. return Column(
  387. children: [
  388. _buildListSupply(),
  389. SizedBox(
  390. height: 8.0,
  391. ),
  392. _formEdit()
  393. ],
  394. );
  395. }
  396. }