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.

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