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