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.

379 lines
14KB

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