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.

483 lines
20KB

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