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.

466 lines
19KB

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