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.

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