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.

376 lines
15KB

  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/controller/ChangeFormButton.dart';
  4. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeSupplyUsing.dart';
  5. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeUnit.dart';
  6. import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_supply.dart';
  7. import 'package:farm_tpf/presentation/screens/actions/util_action.dart';
  8. import 'package:farm_tpf/presentation/screens/resources/sc_resource_helper.dart';
  9. import 'package:farm_tpf/utils/const_color.dart';
  10. import 'package:farm_tpf/utils/const_common.dart';
  11. import 'package:farm_tpf/utils/const_style.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:get/get.dart';
  14. import 'package:farm_tpf/utils/formatter.dart';
  15. import 'package:intl/intl.dart';
  16. import 'package:pattern_formatter/pattern_formatter.dart';
  17. class WidgetPlantSupply extends StatefulWidget {
  18. final List<SuppliesUsing> currentItems;
  19. final Function(List<SuppliesUsing> supplyChanges) onChangeSupplies;
  20. WidgetPlantSupply({this.currentItems, @required this.onChangeSupplies});
  21. @override
  22. _WidgetPlantSupplyState createState() => _WidgetPlantSupplyState();
  23. }
  24. class _WidgetPlantSupplyState extends State<WidgetPlantSupply> {
  25. final _dosageController = TextEditingController();
  26. final _quantityController = TextEditingController();
  27. final changeSelectedSupply = Get.put(ChangeSupply());
  28. final changeSupplyUsing = Get.put(ChangeSupplyUsing());
  29. final changeUnit = Get.put(ChangeUnit());
  30. final changeButton = Get.put(ChangeButtonInForm());
  31. @override
  32. void initState() {
  33. super.initState();
  34. changeSelectedSupply.initValue();
  35. changeSupplyUsing.init(widget.currentItems);
  36. changeUnit.initValue();
  37. changeButton.resetValue();
  38. }
  39. Widget _buildListSupply() {
  40. return GetBuilder<ChangeSupplyUsing>(builder: (value) {
  41. widget.onChangeSupplies(value.currentItems);
  42. if (value.currentItems.isEmpty) {
  43. return Container();
  44. } else {
  45. return Container(
  46. height: 80,
  47. child: ListView.builder(
  48. physics: ClampingScrollPhysics(),
  49. scrollDirection: Axis.horizontal,
  50. shrinkWrap: true,
  51. itemCount: value.currentItems.length,
  52. itemBuilder: (context, index) {
  53. return GestureDetector(
  54. onTap: () {
  55. print("edit");
  56. changeSupplyUsing.changeIndexEdit(index);
  57. changeButton.updateToEdit(true);
  58. var editedSupplyUsing = value.currentItems[index];
  59. var editedSupply = Supply()
  60. ..id = editedSupplyUsing.tbSuppliesInWarehouseId
  61. ..tbSuppliesName = editedSupplyUsing.supplyName
  62. ..unit = editedSupplyUsing.supplyUnit;
  63. changeSelectedSupply.change(editedSupply);
  64. changeUnit
  65. .updateListByUnitName(editedSupplyUsing.supplyUnit);
  66. changeUnit.updateSelected(editedSupplyUsing.supplyUnit);
  67. _dosageController.text = editedSupplyUsing.dosage;
  68. _quantityController.text = editedSupplyUsing.quantity
  69. .formatNumtoStringDecimal();
  70. },
  71. child: Card(
  72. child: Stack(
  73. alignment: Alignment.bottomCenter,
  74. overflow: Overflow.visible,
  75. children: <Widget>[
  76. Positioned(
  77. child: ClipRRect(
  78. borderRadius: BorderRadius.circular(8),
  79. child: Container(
  80. padding: EdgeInsets.all(4),
  81. width: 150,
  82. child: Column(
  83. children: [
  84. SizedBox(
  85. height: 12.0,
  86. ),
  87. Flexible(
  88. child: Text(
  89. value.currentItems[index].supplyName ??
  90. "",
  91. overflow: TextOverflow.ellipsis,
  92. maxLines: 1),
  93. ),
  94. Flexible(
  95. child: Text(
  96. "${value.currentItems[index].dosage ?? ""}")),
  97. Flexible(
  98. child: Text(
  99. "${value.currentItems[index].quantity.formatNumtoStringDecimal() ?? ""} ${value.currentItems[index].supplyUnit ?? ""}")),
  100. ],
  101. ),
  102. ),
  103. )),
  104. Positioned(
  105. top: -10,
  106. right: -10,
  107. child: IconButton(
  108. icon: Icon(
  109. Icons.cancel,
  110. color: Colors.redAccent,
  111. ),
  112. onPressed: () {
  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. num assignValue = currentQuantity.parseDoubleThousand();
  180. if (assignValue != null) {
  181. var oldSelected = data.selectedUnit;
  182. if (oldSelected == value) {
  183. } else {
  184. assignValue = UtilAction.convertUnit(
  185. inputValue: assignValue,
  186. oldUnit: oldSelected,
  187. newUnit: value);
  188. }
  189. _quantityController.text = assignValue.formatNumtoStringDecimal();
  190. }
  191. changeUnit.updateSelected(value);
  192. },
  193. );
  194. });
  195. }
  196. _quantityField() {
  197. return TextFormField(
  198. keyboardType: TextInputType.numberWithOptions(decimal: true),
  199. inputFormatters: [
  200. ThousandsFormatter(
  201. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  202. ],
  203. decoration: InputDecoration(labelText: "Tổng lượng sử dụng"),
  204. controller: _quantityController);
  205. }
  206. _buttonInForm() {
  207. return GetBuilder<ChangeButtonInForm>(builder: (_) {
  208. return Align(
  209. alignment: Alignment.centerRight,
  210. child: Row(
  211. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  212. children: [
  213. _.isEdit
  214. ? OutlineButton(
  215. shape: RoundedRectangleBorder(
  216. borderRadius: new BorderRadius.circular(8.0)),
  217. child: Text("Huỷ"),
  218. onPressed: () {
  219. changeButton.resetValue();
  220. _resetForm();
  221. _hidenKeyboard(context);
  222. })
  223. : SizedBox(),
  224. _.isEdit
  225. ? FlatButton(
  226. color: COLOR_CONST.DEFAULT,
  227. shape: RoundedRectangleBorder(
  228. borderRadius: new BorderRadius.circular(8.0)),
  229. onPressed: () {
  230. var currentSupply = changeSelectedSupply.currentSupply;
  231. if (currentSupply.id != null) {
  232. var quantityWithCurrentSupplyUnit =
  233. UtilAction.convertUnit(
  234. inputValue: _quantityController.text
  235. .parseDoubleThousand(),
  236. oldUnit: changeUnit.selectedUnit,
  237. newUnit:
  238. changeSelectedSupply.currentSupply.unit);
  239. SuppliesUsing newSup = SuppliesUsing()
  240. ..dosage = _dosageController.text
  241. ..quantity = quantityWithCurrentSupplyUnit
  242. ..tbSuppliesInWarehouseId = currentSupply.id
  243. ..suppliesInWarehouseId = currentSupply.id
  244. ..supplyName = currentSupply.tbSuppliesName
  245. ..supplyUnit = currentSupply.unit
  246. ..unit = currentSupply.unit;
  247. changeSupplyUsing.editSupply(
  248. changeSupplyUsing.currentIndex, newSup);
  249. _resetForm();
  250. _hidenKeyboard(context);
  251. }
  252. },
  253. child: Text(
  254. "Sửa",
  255. style: TextStyle(color: Colors.white),
  256. ))
  257. : FlatButton(
  258. color: COLOR_CONST.DEFAULT,
  259. shape: RoundedRectangleBorder(
  260. borderRadius: new BorderRadius.circular(8.0)),
  261. onPressed: () {
  262. var currentSupply = changeSelectedSupply.currentSupply;
  263. if (currentSupply.id != null) {
  264. var quantityWithCurrentSupplyUnit =
  265. UtilAction.convertUnit(
  266. inputValue: _quantityController.text
  267. .parseDoubleThousand(),
  268. oldUnit: changeUnit.selectedUnit,
  269. newUnit:
  270. changeSelectedSupply.currentSupply.unit);
  271. SuppliesUsing newSup = SuppliesUsing()
  272. ..dosage = _dosageController.text
  273. ..quantity = quantityWithCurrentSupplyUnit
  274. ..tbSuppliesInWarehouseId = currentSupply.id
  275. ..suppliesInWarehouseId = currentSupply.id
  276. ..supplyName = currentSupply.tbSuppliesName
  277. ..supplyUnit = currentSupply.unit
  278. ..unit = currentSupply.unit;
  279. changeSupplyUsing.addSupply(newSup);
  280. _resetForm();
  281. _hidenKeyboard(context);
  282. }
  283. },
  284. child: Text(
  285. "Thêm",
  286. style: TextStyle(color: Colors.white),
  287. ))
  288. ],
  289. ),
  290. );
  291. });
  292. }
  293. Widget _formEdit() {
  294. return Container(
  295. padding: EdgeInsets.all(8.0),
  296. decoration: BoxDecoration(
  297. shape: BoxShape.rectangle,
  298. borderRadius: BorderRadius.circular(10),
  299. color: Colors.white,
  300. border: Border.all(color: COLOR_CONST.DEFAULT)),
  301. child: Column(
  302. children: [
  303. Container(
  304. width: double.infinity,
  305. child: Text(
  306. "Hoá chất xử lý",
  307. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  308. ),
  309. ),
  310. _btnSelectSubstrates(),
  311. TextFormField(
  312. keyboardType: TextInputType.text,
  313. controller: _dosageController,
  314. decoration: InputDecoration(labelText: "Liều lượng"),
  315. onSaved: (newValue) {},
  316. ),
  317. Row(
  318. mainAxisSize: MainAxisSize.min,
  319. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  320. crossAxisAlignment: CrossAxisAlignment.center,
  321. children: [
  322. Expanded(
  323. flex: 2,
  324. child: Container(
  325. height: 70,
  326. child: _quantityField(),
  327. ),
  328. ),
  329. SizedBox(
  330. width: 16.0,
  331. ),
  332. Expanded(
  333. flex: 1,
  334. child: Align(
  335. alignment: Alignment.bottomCenter,
  336. child: _dropdownUnitTypes(),
  337. )),
  338. ]),
  339. _buttonInForm()
  340. ],
  341. ));
  342. }
  343. _resetForm() {
  344. changeSupplyUsing.changeIndexEdit(-1);
  345. changeButton.resetValue();
  346. _dosageController.text = "";
  347. _quantityController.text = "";
  348. changeUnit.initValue();
  349. changeSelectedSupply.initValue();
  350. }
  351. _hidenKeyboard(BuildContext context) {
  352. FocusScopeNode currentFocus = FocusScope.of(context);
  353. if (!currentFocus.hasPrimaryFocus) {
  354. currentFocus.unfocus();
  355. }
  356. }
  357. @override
  358. Widget build(BuildContext context) {
  359. return Column(
  360. children: [_formEdit(), _buildListSupply()],
  361. );
  362. }
  363. }