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.

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