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.

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