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.

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