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.

366 lines
13KB

  1. import 'package:farm_tpf/custom_model/CropPlot.dart';
  2. import 'package:farm_tpf/data/api/app_exception.dart';
  3. import 'package:farm_tpf/data/repository/repository.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/widget_toast.dart';
  7. import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
  8. import 'package:farm_tpf/presentation/screens/plot_detail/bloc_plot_information.dart';
  9. import 'package:farm_tpf/utils/const_color.dart';
  10. import 'package:farm_tpf/utils/const_string.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:get/get.dart';
  13. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  14. import 'package:farm_tpf/utils/formatter.dart';
  15. class PlotInformationScreen extends StatefulWidget {
  16. final int cropId;
  17. PlotInformationScreen({@required this.cropId});
  18. @override
  19. _PlotInformationScreenState createState() => _PlotInformationScreenState();
  20. }
  21. class _PlotInformationScreenState extends State<PlotInformationScreen> {
  22. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  23. GlobalKey<FormState> _formKey = GlobalKey();
  24. TextEditingController _statusController = TextEditingController();
  25. TextEditingController _houseNameController = TextEditingController();
  26. TextEditingController _codeController = TextEditingController();
  27. TextEditingController _supplyNameController = TextEditingController();
  28. TextEditingController _seedingDateController = TextEditingController();
  29. TextEditingController _timeSoakSeedController = TextEditingController();
  30. TextEditingController _timeNurserySeedController = TextEditingController();
  31. TextEditingController _quantityPlantController = TextEditingController();
  32. TextEditingController _currentPlantController = TextEditingController();
  33. TextEditingController _timeEndGrowController = TextEditingController();
  34. TextEditingController _mainTechnicianController = TextEditingController();
  35. TextEditingController _areaController = TextEditingController();
  36. TextEditingController _descriptionController = TextEditingController();
  37. bool _autoValidate = false;
  38. TbCropDTO _crop = TbCropDTO();
  39. final controller = Get.put(DescriptionChangeControler());
  40. Repository _repository = Repository();
  41. @override
  42. void initState() {
  43. super.initState();
  44. getPlotInfoBloc.getPlotInfo(widget.cropId, (data) {
  45. var result = data as CropPlot;
  46. _crop = result.tbCropDTO;
  47. var statusCrop;
  48. switch (_crop.status) {
  49. case "STATUS_ARE_ACTIVE":
  50. statusCrop = plot_status_active;
  51. break;
  52. case "STATUS_FINISHED":
  53. statusCrop = plot_status_end;
  54. break;
  55. default:
  56. statusCrop = plot_status_unknown;
  57. }
  58. _statusController.text = statusCrop;
  59. _houseNameController.text = _crop.netHouseName?.toString();
  60. _codeController.text = _crop.code?.toString();
  61. _supplyNameController.text = _crop.suppliesName?.toString();
  62. _areaController.text = _crop.areaM2.formatNumtoStringDecimal();
  63. _seedingDateController.text = result.sowingDate.format_DDMMYY_HHmm();
  64. _timeSoakSeedController.text =
  65. result.soakSeedsTime.formatNumtoStringDecimal();
  66. _timeNurserySeedController.text =
  67. result.soakSeedsTime.formatNumtoStringDecimal();
  68. _quantityPlantController.text =
  69. result.numberPlants.formatNumtoStringDecimal();
  70. _currentPlantController.text =
  71. result.numberCurrentPlants.formatNumtoStringDecimal();
  72. _timeEndGrowController.text = _crop.endDate.format_DDMMYY_HHmm();
  73. _descriptionController.text =
  74. _crop.description == null ? "" : _crop.description.toString();
  75. var technicians = _crop.tbDetailUsers.map((e) => e.fullName).join(", ");
  76. _mainTechnicianController.text = technicians;
  77. }, (err) {});
  78. }
  79. _validateInputs() async {
  80. if (_formKey.currentState.validate()) {
  81. _formKey.currentState.save();
  82. LoadingDialog.showLoadingDialog(context);
  83. _repository.updatePlot(_crop).then((value) {
  84. LoadingDialog.hideLoadingDialog(context);
  85. Utils.showSnackBarSuccess(message: label_update_success);
  86. controller.initValue();
  87. }).catchError((error) {
  88. LoadingDialog.hideLoadingDialog(context);
  89. Utils.showSnackBarError(message: AppException.handleError(error));
  90. });
  91. } else {
  92. _autoValidate = true;
  93. }
  94. }
  95. Widget _statusField() {
  96. return TextFormField(
  97. enabled: false,
  98. controller: _statusController,
  99. decoration: InputDecoration(labelText: "Trạng thái"),
  100. );
  101. }
  102. Widget _houseNameField() {
  103. return TextFormField(
  104. enabled: false,
  105. controller: _houseNameController,
  106. decoration: InputDecoration(labelText: "Nhà màng"),
  107. );
  108. }
  109. Widget _codeField() {
  110. return TextFormField(
  111. enabled: false,
  112. controller: _codeController,
  113. decoration: InputDecoration(labelText: "Mã lô"),
  114. );
  115. }
  116. Widget _supplyNameField() {
  117. return TextFormField(
  118. enabled: false,
  119. controller: _supplyNameController,
  120. decoration: InputDecoration(labelText: "Giống"),
  121. );
  122. }
  123. Widget _seedingDateField() {
  124. return TextFormField(
  125. enabled: false,
  126. controller: _seedingDateController,
  127. decoration: InputDecoration(labelText: "Ngày gieo trồng"),
  128. );
  129. }
  130. Widget _timeSoakSeedField() {
  131. return TextFormField(
  132. enabled: false,
  133. controller: _timeSoakSeedController,
  134. decoration: InputDecoration(labelText: "Thời gian ngâm hạt"),
  135. );
  136. }
  137. Widget _timeNurserySeedField() {
  138. return TextFormField(
  139. enabled: false,
  140. controller: _timeNurserySeedController,
  141. decoration: InputDecoration(labelText: "Thời gian vô khây ươm"),
  142. );
  143. }
  144. Widget _quantityPlantField() {
  145. return TextFormField(
  146. enabled: false,
  147. controller: _quantityPlantController,
  148. decoration: InputDecoration(labelText: "Số lượng cây trồng"),
  149. );
  150. }
  151. Widget _currentPlantField() {
  152. return TextFormField(
  153. enabled: false,
  154. controller: _currentPlantController,
  155. decoration: InputDecoration(labelText: "Số lượng cây hiện tại"),
  156. );
  157. }
  158. Widget _timeEndGrowField() {
  159. return TextFormField(
  160. enabled: false,
  161. controller: _timeEndGrowController,
  162. decoration: InputDecoration(labelText: "Ngày kết thúc canh tác"),
  163. );
  164. }
  165. Widget _mainTechnicianField() {
  166. return TextFormField(
  167. enabled: false,
  168. controller: _mainTechnicianController,
  169. decoration: InputDecoration(labelText: "Kỹ sư trực tiếp"),
  170. );
  171. }
  172. Widget _areaField() {
  173. return TextFormField(
  174. enabled: false,
  175. controller: _areaController,
  176. decoration: InputDecoration(labelText: "Diện tích (m\u00B2)"),
  177. );
  178. }
  179. Widget _descriptionField() {
  180. return TextFormField(
  181. keyboardType: TextInputType.text,
  182. controller: _descriptionController,
  183. decoration: InputDecoration(labelText: "Ghi chú"),
  184. onSaved: (newValue) {
  185. _crop.description = newValue;
  186. },
  187. onChanged: (newValue) {
  188. controller.changeValue(_crop.description, newValue);
  189. },
  190. );
  191. }
  192. _actionAppBar() {
  193. return <Widget>[
  194. GetBuilder<DescriptionChangeControler>(
  195. builder: (_) {
  196. return IconButton(
  197. icon: Icon(
  198. Icons.done,
  199. ),
  200. disabledColor: Colors.grey,
  201. onPressed: controller.isChanged == false
  202. ? null
  203. : () {
  204. FocusScopeNode currentFocus = FocusScope.of(context);
  205. if (!currentFocus.hasPrimaryFocus) {
  206. currentFocus.unfocus();
  207. }
  208. _validateInputs();
  209. },
  210. );
  211. },
  212. )
  213. ];
  214. }
  215. @override
  216. Widget build(BuildContext context) => KeyboardDismisser(
  217. gestures: [
  218. GestureType.onTap,
  219. GestureType.onPanUpdateDownDirection,
  220. ],
  221. child: Container(
  222. color: COLOR_CONST.ITEM_BG,
  223. child: SafeArea(
  224. top: false,
  225. bottom: true,
  226. child: Scaffold(
  227. key: _scaffoldKey,
  228. appBar: AppBar(
  229. centerTitle: true,
  230. title: Text("Thông tin lô"),
  231. actions: _actionAppBar()),
  232. body: KeyboardDismisser(
  233. child: StreamBuilder(
  234. stream: getPlotInfoBloc.actions,
  235. builder: (BuildContext context,
  236. AsyncSnapshot<dynamic> snapshot) {
  237. if (snapshot.hasData) {
  238. return Form(
  239. key: _formKey,
  240. autovalidate: _autoValidate,
  241. child: SingleChildScrollView(
  242. padding: EdgeInsets.all(8.0),
  243. child: Column(
  244. children: <Widget>[
  245. _statusField(),
  246. SizedBox(
  247. height: 4.0,
  248. ),
  249. _houseNameField(),
  250. SizedBox(
  251. height: 4.0,
  252. ),
  253. _codeField(),
  254. SizedBox(
  255. height: 4.0,
  256. ),
  257. _supplyNameField(),
  258. SizedBox(
  259. height: 4.0,
  260. ),
  261. _seedingDateField(),
  262. SizedBox(
  263. height: 4.0,
  264. ),
  265. _timeSoakSeedField(),
  266. SizedBox(
  267. height: 4.0,
  268. ),
  269. _timeNurserySeedField(),
  270. SizedBox(
  271. height: 4.0,
  272. ),
  273. _quantityPlantField(),
  274. SizedBox(
  275. height: 4.0,
  276. ),
  277. _currentPlantField(),
  278. SizedBox(
  279. height: 4.0,
  280. ),
  281. _timeEndGrowField(),
  282. SizedBox(
  283. height: 4.0,
  284. ),
  285. _mainTechnicianField(),
  286. SizedBox(
  287. height: 4.0,
  288. ),
  289. _areaField(),
  290. SizedBox(
  291. height: 4.0,
  292. ),
  293. _descriptionField()
  294. ],
  295. ),
  296. ));
  297. } else if (snapshot.hasError) {
  298. return Center(
  299. child: Text(snapshot.error.toString()),
  300. );
  301. } else {
  302. return LoadingListPage();
  303. }
  304. }))))));
  305. @override
  306. void dispose() {
  307. _statusController.dispose();
  308. _houseNameController.dispose();
  309. _codeController.dispose();
  310. _supplyNameController.dispose();
  311. _seedingDateController.dispose();
  312. _timeSoakSeedController.dispose();
  313. _timeNurserySeedController.dispose();
  314. _quantityPlantController.dispose();
  315. _currentPlantController.dispose();
  316. _timeEndGrowController.dispose();
  317. _mainTechnicianController.dispose();
  318. _areaController.dispose();
  319. _descriptionController.dispose();
  320. super.dispose();
  321. }
  322. }
  323. class DescriptionChangeControler extends GetxController {
  324. bool isChanged = false;
  325. void initValue() {
  326. isChanged = false;
  327. update();
  328. }
  329. void changeValue(String oldValue, String newValue) {
  330. if (oldValue != newValue) {
  331. isChanged = true;
  332. } else {
  333. isChanged = false;
  334. }
  335. if (oldValue.isNullOrBlank && newValue.isEmpty) {
  336. isChanged = false;
  337. }
  338. update();
  339. }
  340. }