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.

393 lines
16KB

  1. import 'package:farm_tpf/custom_model/SuppliesUsing.dart';
  2. import 'package:farm_tpf/custom_model/Supply.dart';
  3. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeFormButton.dart';
  4. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeSupplyUsing.dart';
  5. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeUnit.dart';
  6. import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_supply.dart';
  7. import 'package:farm_tpf/presentation/screens/resources/sc_resource_helper.dart';
  8. import 'package:farm_tpf/utils/const_color.dart';
  9. import 'package:farm_tpf/utils/const_common.dart';
  10. import 'package:farm_tpf/utils/const_style.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:get/get.dart';
  13. import 'package:farm_tpf/utils/formatter.dart';
  14. import 'package:intl/intl.dart';
  15. import 'package:pattern_formatter/pattern_formatter.dart';
  16. import '../util_action.dart';
  17. class WidgetDungSupply extends StatefulWidget {
  18. final List<SuppliesUsing> currentItems;
  19. final Function(List<SuppliesUsing> supplyChanges) onChangeSupplies;
  20. WidgetDungSupply({this.currentItems, @required this.onChangeSupplies});
  21. @override
  22. _WidgetDungSupplyState createState() => _WidgetDungSupplyState();
  23. }
  24. class _WidgetDungSupplyState extends State<WidgetDungSupply> {
  25. final _dosageController = TextEditingController();
  26. final _quantityController = TextEditingController();
  27. final _howToUseController = 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: 100,
  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. _howToUseController.text = editedSupplyUsing.howToUse;
  70. _quantityController.text = editedSupplyUsing.quantity
  71. .formatNumtoStringDecimal();
  72. },
  73. child: Card(
  74. child: Stack(
  75. alignment: Alignment.bottomCenter,
  76. overflow: Overflow.visible,
  77. children: <Widget>[
  78. Positioned(
  79. child: ClipRRect(
  80. borderRadius: BorderRadius.circular(8),
  81. child: Container(
  82. padding: EdgeInsets.all(4),
  83. width: 150,
  84. child: Column(
  85. children: [
  86. SizedBox(
  87. height: 12.0,
  88. ),
  89. Flexible(
  90. child: Text(
  91. value.currentItems[index].supplyName ??
  92. "",
  93. overflow: TextOverflow.ellipsis,
  94. maxLines: 1),
  95. ),
  96. Flexible(
  97. child: Text(
  98. "${value.currentItems[index].dosage ?? ""}")),
  99. Flexible(
  100. child: Text(
  101. "${value.currentItems[index].quantity.formatNumtoStringDecimal() ?? ""} ${value.currentItems[index].supplyUnit ?? ""}")),
  102. Flexible(
  103. child: Text(
  104. "${value.currentItems[index].howToUse ?? ""}")),
  105. ],
  106. ),
  107. ),
  108. )),
  109. Positioned(
  110. top: -10,
  111. right: -10,
  112. child: IconButton(
  113. icon: Icon(
  114. Icons.cancel,
  115. color: Colors.redAccent,
  116. ),
  117. onPressed: () {
  118. changeSupplyUsing.deleteSupply(index);
  119. }),
  120. )
  121. ],
  122. )));
  123. }));
  124. }
  125. });
  126. }
  127. Widget _btnSelectSubstrates() {
  128. return GetBuilder<ChangeSupply>(builder: (data) {
  129. return FlatButton(
  130. padding:
  131. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  132. onPressed: () {
  133. Navigator.of(context)
  134. .push(MaterialPageRoute(
  135. builder: (_) => ResourceHelperScreen(
  136. titleName: "Phân bón",
  137. type: ConstCommon.supplyTypeDung,
  138. selectedId: changeSelectedSupply.selectedSupplyId),
  139. fullscreenDialog: false))
  140. .then((value) {
  141. if (value != null) {
  142. var result = value as Supply;
  143. changeSelectedSupply.change(result);
  144. changeUnit.updateListByUnitName(result.unit);
  145. }
  146. });
  147. },
  148. child: Container(
  149. padding: EdgeInsets.only(
  150. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  151. decoration: BoxDecoration(
  152. border: kBorderTextField,
  153. ),
  154. child: Row(
  155. children: [
  156. GetBuilder<ChangeSupply>(
  157. builder: (_) => Expanded(
  158. child: Text(
  159. changeSelectedSupply.selectedSupplyName ??
  160. "Tên thương mại",
  161. style: TextStyle(
  162. fontSize: 14.0, color: Colors.black87)))),
  163. Icon(
  164. Icons.arrow_drop_down,
  165. color: Colors.grey,
  166. ),
  167. ],
  168. )));
  169. });
  170. }
  171. Widget _dropdownUnitTypes() {
  172. return GetBuilder<ChangeUnit>(builder: (data) {
  173. return DropdownButtonFormField<String>(
  174. itemHeight: 100,
  175. value: data.selectedUnit.isEmpty ? null : data.selectedUnit,
  176. items: data.currentUnits
  177. .map((label) => DropdownMenuItem(
  178. child: Text(label),
  179. value: label,
  180. ))
  181. .toList(),
  182. onChanged: (value) {
  183. var currentQuantity = _quantityController.text;
  184. num assignValue = currentQuantity.parseDoubleThousand();
  185. if (assignValue != null) {
  186. var oldSelected = data.selectedUnit;
  187. if (oldSelected == value) {
  188. } else {
  189. assignValue = UtilAction.convertUnit(
  190. inputValue: assignValue,
  191. oldUnit: oldSelected,
  192. newUnit: value);
  193. }
  194. _quantityController.text = assignValue.formatNumtoStringDecimal();
  195. }
  196. changeUnit.updateSelected(value);
  197. },
  198. );
  199. });
  200. }
  201. _quantityField() {
  202. return TextFormField(
  203. keyboardType: TextInputType.numberWithOptions(decimal: true),
  204. inputFormatters: [
  205. ThousandsFormatter(
  206. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  207. ],
  208. decoration: InputDecoration(labelText: "Tổng lượng sử dụng *"),
  209. controller: _quantityController);
  210. }
  211. _buttonInForm() {
  212. return GetBuilder<ChangeButtonInForm>(builder: (_) {
  213. return Align(
  214. alignment: Alignment.centerRight,
  215. child: Row(
  216. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  217. children: [
  218. _.isEdit
  219. ? OutlineButton(
  220. shape: RoundedRectangleBorder(
  221. borderRadius: new BorderRadius.circular(8.0)),
  222. child: Text("Huỷ"),
  223. onPressed: () {
  224. changeButton.resetValue();
  225. _resetForm();
  226. _hidenKeyboard(context);
  227. })
  228. : SizedBox(),
  229. _.isEdit
  230. ? FlatButton(
  231. color: COLOR_CONST.DEFAULT,
  232. shape: RoundedRectangleBorder(
  233. borderRadius: new BorderRadius.circular(8.0)),
  234. onPressed: () {
  235. var currentSupply = changeSelectedSupply.currentSupply;
  236. var currentQuantity =
  237. _quantityController.text.parseDoubleThousand();
  238. if (currentSupply.id != null && currentQuantity > 0) {
  239. var quantityWithCurrentSupplyUnit =
  240. UtilAction.convertUnit(
  241. inputValue: currentQuantity,
  242. oldUnit: changeUnit.selectedUnit,
  243. newUnit:
  244. changeSelectedSupply.currentSupply.unit);
  245. SuppliesUsing newSup = SuppliesUsing()
  246. ..dosage = _dosageController.text
  247. ..howToUse = _howToUseController.text
  248. ..quantity = quantityWithCurrentSupplyUnit
  249. ..tbSuppliesInWarehouseId = currentSupply.id
  250. ..suppliesInWarehouseId = currentSupply.id
  251. ..supplyName = currentSupply.tbSuppliesName
  252. ..supplyUnit = currentSupply.unit
  253. ..unit = currentSupply.unit;
  254. changeSupplyUsing.editSupply(
  255. changeSupplyUsing.currentIndex, newSup);
  256. _resetForm();
  257. _hidenKeyboard(context);
  258. }
  259. },
  260. child: Text(
  261. "Sửa",
  262. style: TextStyle(color: Colors.white),
  263. ))
  264. : FlatButton(
  265. color: COLOR_CONST.DEFAULT,
  266. shape: RoundedRectangleBorder(
  267. borderRadius: new BorderRadius.circular(8.0)),
  268. onPressed: () {
  269. var currentSupply = changeSelectedSupply.currentSupply;
  270. var currentQuantity =
  271. _quantityController.text.parseDoubleThousand();
  272. if (currentSupply.id != null && currentQuantity > 0) {
  273. var quantityWithCurrentSupplyUnit =
  274. UtilAction.convertUnit(
  275. inputValue: currentQuantity,
  276. oldUnit: changeUnit.selectedUnit,
  277. newUnit:
  278. changeSelectedSupply.currentSupply.unit);
  279. SuppliesUsing newSup = SuppliesUsing()
  280. ..dosage = _dosageController.text
  281. ..howToUse = _howToUseController.text
  282. ..quantity = quantityWithCurrentSupplyUnit
  283. ..tbSuppliesInWarehouseId = currentSupply.id
  284. ..suppliesInWarehouseId = currentSupply.id
  285. ..supplyName = currentSupply.tbSuppliesName
  286. ..supplyUnit = currentSupply.unit
  287. ..unit = currentSupply.unit;
  288. changeSupplyUsing.addSupply(newSup);
  289. _resetForm();
  290. _hidenKeyboard(context);
  291. }
  292. },
  293. child: Text(
  294. "Thêm",
  295. style: TextStyle(color: Colors.white),
  296. ))
  297. ],
  298. ),
  299. );
  300. });
  301. }
  302. Widget _formEdit() {
  303. return Container(
  304. padding: EdgeInsets.all(8.0),
  305. decoration: BoxDecoration(
  306. shape: BoxShape.rectangle,
  307. borderRadius: BorderRadius.circular(10),
  308. color: Colors.white,
  309. border: Border.all(color: COLOR_CONST.DEFAULT)),
  310. child: Column(
  311. children: [
  312. Container(
  313. width: double.infinity,
  314. child: Text(
  315. "Tên thương mại *",
  316. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  317. ),
  318. ),
  319. _btnSelectSubstrates(),
  320. TextFormField(
  321. keyboardType: TextInputType.text,
  322. controller: _dosageController,
  323. decoration: InputDecoration(labelText: "Liều lượng sử dụng"),
  324. onSaved: (newValue) {},
  325. ),
  326. Row(
  327. mainAxisSize: MainAxisSize.min,
  328. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  329. crossAxisAlignment: CrossAxisAlignment.center,
  330. children: [
  331. Expanded(
  332. flex: 2,
  333. child: Container(
  334. height: 70,
  335. child: _quantityField(),
  336. ),
  337. ),
  338. SizedBox(
  339. width: 16.0,
  340. ),
  341. Expanded(
  342. flex: 1,
  343. child: Align(
  344. alignment: Alignment.bottomCenter,
  345. child: _dropdownUnitTypes(),
  346. )),
  347. ]),
  348. TextFormField(
  349. keyboardType: TextInputType.text,
  350. controller: _howToUseController,
  351. decoration: InputDecoration(labelText: "Phương pháp sử dụng"),
  352. onSaved: (newValue) {},
  353. ),
  354. _buttonInForm()
  355. ],
  356. ));
  357. }
  358. _resetForm() {
  359. changeSupplyUsing.changeIndexEdit(-1);
  360. changeButton.resetValue();
  361. _dosageController.text = "";
  362. _howToUseController.text = "";
  363. _quantityController.text = "";
  364. changeUnit.initValue();
  365. changeSelectedSupply.initValue();
  366. }
  367. _hidenKeyboard(BuildContext context) {
  368. FocusScopeNode currentFocus = FocusScope.of(context);
  369. if (!currentFocus.hasPrimaryFocus) {
  370. currentFocus.unfocus();
  371. }
  372. }
  373. @override
  374. Widget build(BuildContext context) {
  375. return Column(
  376. children: [_formEdit(), _buildListSupply()],
  377. );
  378. }
  379. }