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.

466 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. print("Delete");
  111. changeSupplyUsing.deleteSupply(index);
  112. }),
  113. )
  114. ],
  115. )));
  116. }));
  117. }
  118. });
  119. }
  120. Widget _btnSelectSubstrates() {
  121. return GetBuilder<ChangeSupply>(builder: (data) {
  122. return FlatButton(
  123. padding:
  124. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  125. onPressed: () {
  126. Navigator.of(context)
  127. .push(MaterialPageRoute(
  128. builder: (_) => ResourceHelperScreen(
  129. titleName: "Hoá chất xử lý",
  130. type: ConstCommon.supplyTypeAll,
  131. selectedId: changeSelectedSupply.selectedSupplyId),
  132. fullscreenDialog: false))
  133. .then((value) {
  134. if (value != null) {
  135. var result = value as Supply;
  136. changeSelectedSupply.change(result);
  137. changeUnit.updateListByUnitName(result.unit);
  138. }
  139. });
  140. },
  141. child: Container(
  142. padding: EdgeInsets.only(
  143. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  144. decoration: BoxDecoration(
  145. border: kBorderTextField,
  146. ),
  147. child: Row(
  148. children: [
  149. GetBuilder<ChangeSupply>(
  150. builder: (_) => Expanded(
  151. child: Text(
  152. changeSelectedSupply.selectedSupplyName ??
  153. "Hoá chất xử lý",
  154. style: TextStyle(
  155. fontSize: 14.0, color: Colors.black87)))),
  156. Icon(
  157. Icons.arrow_drop_down,
  158. color: Colors.grey,
  159. ),
  160. ],
  161. )));
  162. });
  163. }
  164. Widget _dropdownUnitTypes() {
  165. return GetBuilder<ChangeUnit>(builder: (data) {
  166. return DropdownButtonFormField<String>(
  167. itemHeight: 100,
  168. value: data.selectedUnit.isEmpty ? null : data.selectedUnit,
  169. items: data.currentUnits
  170. .map((label) => DropdownMenuItem(
  171. child: Text(label),
  172. value: label,
  173. ))
  174. .toList(),
  175. onChanged: (value) {
  176. var currentQuantity = _quantityController.text;
  177. num assignValue = currentQuantity.parseDoubleThousand();
  178. if (assignValue != null) {
  179. var oldSelected = data.selectedUnit;
  180. if (oldSelected == value) {
  181. } else {
  182. assignValue = UtilAction.convertUnit(
  183. inputValue: assignValue,
  184. oldUnit: oldSelected,
  185. newUnit: value);
  186. }
  187. _quantityController.text = assignValue.formatNumtoStringDecimal();
  188. }
  189. changeUnit.updateSelected(value);
  190. },
  191. );
  192. });
  193. }
  194. _quantityField() {
  195. return TextFormField(
  196. keyboardType: TextInputType.numberWithOptions(decimal: true),
  197. inputFormatters: [
  198. ThousandsFormatter(
  199. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  200. ],
  201. decoration: InputDecoration(labelText: "Tổng lượng sử dụng"),
  202. controller: _quantityController,
  203. onSaved: (newValue) {
  204. print(newValue);
  205. },
  206. onChanged: (v) {
  207. print(v);
  208. },
  209. );
  210. }
  211. _buttonInForm() {
  212. return GetBuilder<ChangeButtonInForm>(builder: (_) {
  213. return Align(
  214. alignment: Alignment.centerRight,
  215. child: Row(
  216. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  217. children: [
  218. _.isEdit
  219. ? OutlineButton(
  220. shape: RoundedRectangleBorder(
  221. borderRadius: new BorderRadius.circular(8.0)),
  222. child: Text("Huỷ"),
  223. onPressed: () {
  224. changeButton.resetValue();
  225. _resetForm();
  226. _hidenKeyboard(context);
  227. })
  228. : SizedBox(),
  229. _.isEdit
  230. ? FlatButton(
  231. color: COLOR_CONST.DEFAULT,
  232. shape: RoundedRectangleBorder(
  233. borderRadius: new BorderRadius.circular(8.0)),
  234. onPressed: () {
  235. var currentSupply = changeSelectedSupply.currentSupply;
  236. if (currentSupply.id != null) {
  237. var quantityWithCurrentSupplyUnit =
  238. UtilAction.convertUnit(
  239. inputValue: _quantityController.text
  240. .parseDoubleThousand(),
  241. oldUnit: changeUnit.selectedUnit,
  242. newUnit:
  243. changeSelectedSupply.currentSupply.unit);
  244. SuppliesUsing newSup = SuppliesUsing()
  245. ..dosage = _dosageController.text
  246. ..quantity = quantityWithCurrentSupplyUnit
  247. ..tbSuppliesInWarehouseId = currentSupply.id
  248. ..suppliesInWarehouseId = currentSupply.id
  249. ..supplyName = currentSupply.tbSuppliesName
  250. ..supplyUnit = currentSupply.unit
  251. ..unit = currentSupply.unit;
  252. changeSupplyUsing.editSupply(
  253. changeSupplyUsing.currentIndex, newSup);
  254. _resetForm();
  255. _hidenKeyboard(context);
  256. }
  257. },
  258. child: Text(
  259. "Sửa",
  260. style: TextStyle(color: Colors.white),
  261. ))
  262. : FlatButton(
  263. color: COLOR_CONST.DEFAULT,
  264. shape: RoundedRectangleBorder(
  265. borderRadius: new BorderRadius.circular(8.0)),
  266. onPressed: () {
  267. var currentSupply = changeSelectedSupply.currentSupply;
  268. if (currentSupply.id != null) {
  269. var quantityWithCurrentSupplyUnit =
  270. UtilAction.convertUnit(
  271. inputValue: _quantityController.text
  272. .parseDoubleThousand(),
  273. oldUnit: changeUnit.selectedUnit,
  274. newUnit:
  275. changeSelectedSupply.currentSupply.unit);
  276. SuppliesUsing newSup = SuppliesUsing()
  277. ..dosage = _dosageController.text
  278. ..quantity = quantityWithCurrentSupplyUnit
  279. ..tbSuppliesInWarehouseId = currentSupply.id
  280. ..suppliesInWarehouseId = currentSupply.id
  281. ..supplyName = currentSupply.tbSuppliesName
  282. ..supplyUnit = currentSupply.unit
  283. ..unit = currentSupply.unit;
  284. changeSupplyUsing.addSupply(newSup);
  285. _resetForm();
  286. _hidenKeyboard(context);
  287. }
  288. },
  289. child: Text(
  290. "Thêm",
  291. style: TextStyle(color: Colors.white),
  292. ))
  293. ],
  294. ),
  295. );
  296. });
  297. }
  298. Widget _formEdit() {
  299. return Container(
  300. padding: EdgeInsets.all(8.0),
  301. decoration: BoxDecoration(
  302. shape: BoxShape.rectangle,
  303. borderRadius: BorderRadius.circular(10),
  304. color: Colors.white,
  305. border: Border.all(color: COLOR_CONST.DEFAULT)),
  306. child: Column(
  307. children: [
  308. Container(
  309. width: double.infinity,
  310. child: Text(
  311. "Hoá chất xử lý",
  312. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  313. ),
  314. ),
  315. _btnSelectSubstrates(),
  316. TextFormField(
  317. keyboardType: TextInputType.text,
  318. controller: _dosageController,
  319. decoration: InputDecoration(labelText: "Liều lượng"),
  320. onSaved: (newValue) {},
  321. ),
  322. Row(
  323. mainAxisSize: MainAxisSize.min,
  324. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  325. crossAxisAlignment: CrossAxisAlignment.center,
  326. children: [
  327. Expanded(
  328. flex: 2,
  329. child: Container(
  330. height: 70,
  331. child: _quantityField(),
  332. ),
  333. ),
  334. SizedBox(
  335. width: 16.0,
  336. ),
  337. Expanded(
  338. flex: 1,
  339. child: Align(
  340. alignment: Alignment.bottomCenter,
  341. child: _dropdownUnitTypes(),
  342. )),
  343. ]),
  344. _buttonInForm()
  345. ],
  346. ));
  347. }
  348. _resetForm() {
  349. changeSupplyUsing.changeIndexEdit(-1);
  350. changeButton.resetValue();
  351. _dosageController.text = "";
  352. _quantityController.text = "";
  353. changeUnit.initValue();
  354. changeSelectedSupply.initValue();
  355. }
  356. _hidenKeyboard(BuildContext context) {
  357. FocusScopeNode currentFocus = FocusScope.of(context);
  358. if (!currentFocus.hasPrimaryFocus) {
  359. currentFocus.unfocus();
  360. }
  361. }
  362. @override
  363. Widget build(BuildContext context) {
  364. return Column(
  365. children: [_formEdit(), _buildListSupply()],
  366. );
  367. }
  368. }
  369. class ChangeUnit extends GetxController {
  370. List<String> currentUnits;
  371. String selectedUnit;
  372. initValue() {
  373. currentUnits = [];
  374. selectedUnit = "";
  375. update();
  376. }
  377. updateListByUnitName(String unitName) {
  378. if (unitName == "kg" || unitName == "g") {
  379. currentUnits = ["kg", "g"];
  380. selectedUnit = unitName;
  381. } else if (unitName == "l" || unitName == "m") {
  382. currentUnits = ["l", "ml"];
  383. selectedUnit = unitName;
  384. } else if (unitName == "m" || unitName == "cm" || unitName == "mm") {
  385. currentUnits = ["m", "cm", "mm"];
  386. selectedUnit = unitName;
  387. } else {
  388. currentUnits = [];
  389. }
  390. update();
  391. }
  392. updateSelected(String selected) {
  393. selectedUnit = selected;
  394. update();
  395. }
  396. }
  397. class ChangeButtonInForm extends GetxController {
  398. bool isEdit;
  399. void resetValue() {
  400. isEdit = false;
  401. update();
  402. }
  403. void updateToEdit(bool isChangeEdit) {
  404. isEdit = isChangeEdit;
  405. update();
  406. }
  407. }
  408. class ChangeSupplyUsing extends GetxController {
  409. List<SuppliesUsing> currentItems;
  410. SuppliesUsing currentSupplyUsing;
  411. int currentIndex;
  412. void init(List<SuppliesUsing> initItems) {
  413. currentItems = initItems;
  414. currentSupplyUsing = SuppliesUsing();
  415. currentIndex = -1;
  416. update();
  417. }
  418. void changeIndexEdit(int index) {
  419. currentIndex = index;
  420. update();
  421. }
  422. void changeInitList(List<SuppliesUsing> sups) {
  423. currentItems = sups;
  424. update();
  425. }
  426. void addSupply(SuppliesUsing supplyUsing) {
  427. currentItems.insert(0, supplyUsing);
  428. currentSupplyUsing = SuppliesUsing();
  429. update();
  430. }
  431. void deleteSupply(int index) {
  432. currentItems.removeAt(index);
  433. currentSupplyUsing = SuppliesUsing();
  434. update();
  435. }
  436. void editSupply(int index, SuppliesUsing supplyUsing) {
  437. currentItems[index] = supplyUsing;
  438. currentSupplyUsing = SuppliesUsing();
  439. update();
  440. }
  441. }