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.

272 lines
11KB

  1. import 'dart:convert';
  2. import 'package:farm_tpf/custom_model/End.dart';
  3. import 'package:farm_tpf/data/api/app_exception.dart';
  4. import 'package:farm_tpf/data/repository/repository.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/bloc/media_helper_bloc.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/widget_field_time_picker.dart';
  7. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  8. import 'package:farm_tpf/presentation/custom_widgets/widget_media_picker.dart';
  9. import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
  10. import 'package:farm_tpf/presentation/screens/actions/bloc/action_detail_bloc.dart';
  11. import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_file_controller.dart';
  12. import 'package:farm_tpf/utils/const_common.dart';
  13. import 'package:farm_tpf/utils/const_string.dart';
  14. import 'package:farm_tpf/utils/const_style.dart';
  15. import 'package:farm_tpf/utils/pref.dart';
  16. import 'package:farm_tpf/utils/validators.dart';
  17. import 'package:flutter/material.dart';
  18. import 'package:flutter_bloc/flutter_bloc.dart';
  19. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  20. import 'package:get/get.dart';
  21. import 'package:intl/intl.dart';
  22. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  23. import 'package:farm_tpf/utils/formatter.dart';
  24. import '../util_action.dart';
  25. class EditActionEndScreen extends StatefulWidget {
  26. final int cropId;
  27. final bool isEdit;
  28. final int activityId;
  29. EditActionEndScreen(
  30. {@required this.cropId, this.isEdit = false, this.activityId});
  31. @override
  32. _EditActionEndScreenState createState() => _EditActionEndScreenState();
  33. }
  34. class _EditActionEndScreenState extends State<EditActionEndScreen> {
  35. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  36. final _repository = Repository();
  37. GlobalKey<FormState> _formKey = GlobalKey();
  38. bool _autoValidate = false;
  39. End _end = End();
  40. final _descriptionController = TextEditingController();
  41. final _executeByController = TextEditingController();
  42. var pref = LocalPref();
  43. DateTime executeTime = DateTime.now();
  44. List<String> filePaths = List<String>();
  45. var changeFileController = Get.put(ChangeFileController());
  46. Future<Null> getSharedPrefs() async {
  47. var currentFullName = await pref.getString(DATA_CONST.CURRENT_FULL_NAME);
  48. _executeByController.text = currentFullName ?? "";
  49. }
  50. @override
  51. void initState() {
  52. super.initState();
  53. getSharedPrefs();
  54. changeFileController.initValue();
  55. _end.cropId = widget.cropId;
  56. }
  57. _validateInputs() async {
  58. if (_formKey.currentState.validate()) {
  59. _formKey.currentState.save();
  60. LoadingDialog.showLoadingDialog(context);
  61. filePaths = Get.find<ChangeFileController>().newFiles;
  62. try {
  63. _end.mediaDel = Get.find<ChangeFileController>().deleteFiles;
  64. var activityEnd = jsonEncode(_end.toJson()).toString();
  65. //ADD NEW
  66. if (_end.activityId == null) {
  67. _repository.createAction((value) {
  68. LoadingDialog.hideLoadingDialog(context);
  69. Get.back(result: value);
  70. Utils.showSnackBarSuccess(message: label_add_success);
  71. }, (error) {
  72. LoadingDialog.hideLoadingDialog(context);
  73. Utils.showSnackBarError(message: AppException.handleError(error));
  74. },
  75. apiAddAction: ConstCommon.apiAddEnd,
  76. paramActivity: ConstCommon.paramsActionEnd,
  77. activityAction: activityEnd,
  78. filePaths: filePaths);
  79. } else {
  80. //UPDATE
  81. _repository.updateAction((value) {
  82. LoadingDialog.hideLoadingDialog(context);
  83. Get.back(result: value);
  84. Utils.showSnackBarSuccess(message: label_update_success);
  85. }, (error) {
  86. LoadingDialog.hideLoadingDialog(context);
  87. Utils.showSnackBarError(message: AppException.handleError(error));
  88. },
  89. apiUpdateAction: ConstCommon.apiUpdateEnd,
  90. paramActivity: ConstCommon.paramsActionEnd,
  91. activityAction: activityEnd,
  92. filePaths: filePaths);
  93. }
  94. } catch (e) {
  95. LoadingDialog.hideLoadingDialog(context);
  96. print(e.toString());
  97. }
  98. } else {
  99. _autoValidate = true;
  100. }
  101. }
  102. Widget _btnExecuteTimePicker() {
  103. return WidgetFieldDateTimePicker(
  104. initDateTime: executeTime,
  105. onUpdateDateTime: (selectedDate) {
  106. _end.executeDate =
  107. selectedDate.convertLocalDateTimeToStringUtcDateTime();
  108. });
  109. }
  110. _actionAppBar() {
  111. IconButton iconButton;
  112. if (1 == 1) {
  113. iconButton = IconButton(
  114. icon: Icon(
  115. Icons.done,
  116. color: Colors.black,
  117. ),
  118. onPressed: () {
  119. FocusScopeNode currentFocus = FocusScope.of(context);
  120. if (!currentFocus.hasPrimaryFocus) {
  121. currentFocus.unfocus();
  122. }
  123. _validateInputs();
  124. },
  125. );
  126. return <Widget>[iconButton];
  127. }
  128. return <Widget>[Container()];
  129. }
  130. Widget _descriptionField() {
  131. return TextFormField(
  132. keyboardType: TextInputType.text,
  133. decoration: InputDecoration(labelText: "Ghi chú"),
  134. controller: _descriptionController,
  135. onSaved: (newValue) {
  136. _end.description = newValue;
  137. },
  138. );
  139. }
  140. Widget _executeByField() {
  141. return TextFormField(
  142. keyboardType: TextInputType.text,
  143. decoration: InputDecoration(labelText: "Người thực hiện"),
  144. enabled: false,
  145. controller: _executeByController,
  146. onSaved: (newValue) {},
  147. );
  148. }
  149. @override
  150. Widget build(BuildContext context) => KeyboardDismisser(
  151. gestures: [
  152. GestureType.onTap,
  153. GestureType.onPanUpdateDownDirection,
  154. ],
  155. child: Scaffold(
  156. key: _scaffoldKey,
  157. appBar: AppBar(
  158. centerTitle: true,
  159. title: Text(plot_action_finish),
  160. actions: _actionAppBar()),
  161. body: KeyboardDismisser(
  162. child: MultiBlocProvider(
  163. providers: [
  164. BlocProvider<ActionDetailBloc>(
  165. create: (context) =>
  166. ActionDetailBloc(repository: Repository())
  167. ..add(FetchData(
  168. isNeedFetchData: widget.isEdit,
  169. apiActivity: ConstCommon.apiDetailEnd,
  170. activityId: widget.activityId))),
  171. BlocProvider<MediaHelperBloc>(
  172. create: (context) =>
  173. MediaHelperBloc()..add(ChangeListMedia(items: [])),
  174. )
  175. ],
  176. child: Form(
  177. key: _formKey,
  178. autovalidate: _autoValidate,
  179. child: SingleChildScrollView(
  180. padding: EdgeInsets.all(8.0),
  181. child: BlocConsumer<ActionDetailBloc, ActionDetailState>(
  182. listener: (context, state) async {
  183. if (state is ActionDetailFailure) {
  184. LoadingDialog.hideLoadingDialog(context);
  185. } else if (state is ActionDetailSuccess) {
  186. LoadingDialog.hideLoadingDialog(context);
  187. _end = End.fromJson(state.item);
  188. _end.activityId = widget.activityId;
  189. _descriptionController.text =
  190. _end.description ?? "";
  191. _executeByController.text = _end.createdByName;
  192. Get.find<ChangeDateTimePicker>().change(_end
  193. .executeDate
  194. .convertStringServerDateTimeToLocalDateTime());
  195. //Show media
  196. if (Validators.stringNotNullOrEmpty(_end.media)) {
  197. BlocProvider.of<MediaHelperBloc>(context).add(
  198. ChangeListMedia(
  199. items: UtilAction.convertFilePathToMedia(
  200. _end.media)));
  201. }
  202. } else if (state is ActionDetailInitial) {
  203. } else if (state is ActionDetailLoading) {
  204. LoadingDialog.showLoadingDialog(context);
  205. }
  206. },
  207. builder: (context, state) {
  208. return Column(
  209. children: <Widget>[
  210. Container(
  211. width: double.infinity,
  212. child: Text(
  213. "Ngày thực hiện *",
  214. style: TextStyle(
  215. color: Colors.black54, fontSize: 13.0),
  216. ),
  217. ),
  218. _btnExecuteTimePicker(),
  219. SizedBox(
  220. height: 8.0,
  221. ),
  222. _descriptionField(),
  223. SizedBox(
  224. height: 8.0,
  225. ),
  226. _executeByField(),
  227. SizedBox(
  228. height: 8.0,
  229. ),
  230. BlocBuilder<MediaHelperBloc, MediaHelperState>(
  231. builder: (context, state) {
  232. if (state is MediaHelperSuccess) {
  233. return WidgetMediaPicker(
  234. currentItems: state.items,
  235. onChangeFiles: (newPathFiles,
  236. deletePathFiles) async {
  237. Get.find<ChangeFileController>().change(
  238. newPathFiles, deletePathFiles);
  239. });
  240. } else {
  241. return Center(
  242. child: CircularProgressIndicator());
  243. }
  244. }),
  245. ],
  246. );
  247. },
  248. ),
  249. )),
  250. ))));
  251. @override
  252. void dispose() {
  253. _descriptionController.dispose();
  254. _executeByController.dispose();
  255. super.dispose();
  256. }
  257. }