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.

203 lines
8.1KB

  1. import 'package:farm_tpf/data/repository/repository.dart';
  2. import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
  3. import 'package:farm_tpf/presentation/custom_widgets/bloc/media_helper_bloc.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/button_widget.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/widget_field_time_picker.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  7. import 'package:farm_tpf/presentation/custom_widgets/widget_media_picker.dart';
  8. import 'package:farm_tpf/presentation/screens/actions/cubit/action_ui_cubit.dart';
  9. import 'package:farm_tpf/utils/pref.dart';
  10. import 'package:farm_tpf/utils/validators.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:flutter_bloc/flutter_bloc.dart';
  13. import 'package:get/get.dart';
  14. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  15. import 'state_management_helper/change_file_controller.dart';
  16. class ActionScreen extends StatefulWidget {
  17. final int idAction;
  18. final String title;
  19. ActionScreen({@required this.idAction, @required this.title});
  20. @override
  21. _ActionScreenState createState() => _ActionScreenState();
  22. }
  23. class _ActionScreenState extends State<ActionScreen> {
  24. final _scaffoldKey = GlobalKey<ScaffoldState>();
  25. var _formKey = GlobalKey<FormState>();
  26. var pref = LocalPref();
  27. final _executeByController = TextEditingController();
  28. DateTime executeTime = DateTime.now();
  29. List<String> filePaths = List<String>();
  30. var changeFileController = Get.put(ChangeFileController());
  31. Future<Null> getSharedPrefs() async {
  32. var currentFullName = await pref.getString(DATA_CONST.CURRENT_FULL_NAME);
  33. _executeByController.text = currentFullName ?? "";
  34. }
  35. @override
  36. void initState() {
  37. super.initState();
  38. getSharedPrefs();
  39. }
  40. _validateInputs() async {
  41. if (_formKey.currentState.validate()) {
  42. _formKey.currentState.save();
  43. LoadingDialog.showLoadingDialog(context);
  44. filePaths = Get.find<ChangeFileController>().newFiles;
  45. //Create request general model
  46. try {
  47. //ADD NEW
  48. //Update
  49. } catch (e) {
  50. LoadingDialog.hideLoadingDialog(context);
  51. print(e.toString());
  52. }
  53. } else {
  54. //
  55. }
  56. }
  57. Widget _btnExecuteTimePicker() {
  58. return WidgetFieldDateTimePicker(
  59. initDateTime: executeTime,
  60. onUpdateDateTime: (selectedDate) {
  61. //
  62. });
  63. }
  64. Widget _executeByField() {
  65. return TextFormField(
  66. keyboardType: TextInputType.text,
  67. decoration: InputDecoration(labelText: "Người thực hiện"),
  68. enabled: false,
  69. controller: _executeByController,
  70. onSaved: (newValue) {},
  71. );
  72. }
  73. @override
  74. Widget build(BuildContext context) => KeyboardDismisser(
  75. gestures: [
  76. GestureType.onTap,
  77. GestureType.onPanUpdateDownDirection,
  78. ],
  79. child: Scaffold(
  80. backgroundColor: Colors.white,
  81. key: _scaffoldKey,
  82. appBar: AppBarWidget(
  83. isBack: true,
  84. action: InkWell(
  85. child: Text(
  86. 'Lưu',
  87. style: TextStyle(
  88. color: Colors.red, fontWeight: FontWeight.normal),
  89. ),
  90. onTap: () {},
  91. ),
  92. ),
  93. body: KeyboardDismisser(
  94. child: MultiBlocProvider(
  95. providers: [
  96. BlocProvider<ActionUiCubit>(
  97. create: (context) =>
  98. ActionUiCubit(repository: Repository())
  99. ..getActionUIForm(widget.idAction)),
  100. BlocProvider<MediaHelperBloc>(
  101. create: (context) =>
  102. MediaHelperBloc()..add(ChangeListMedia(items: [])),
  103. )
  104. ],
  105. child: Form(
  106. key: _formKey,
  107. child: SafeArea(
  108. child: SingleChildScrollView(
  109. child: BlocConsumer<ActionUiCubit, ActionUiState>(
  110. listener: (context, state) async {
  111. if (state is ActionUiLoading) {
  112. LoadingDialog.hideLoadingDialog(context);
  113. } else if (state is ActionUiSuccess) {
  114. LoadingDialog.hideLoadingDialog(context);
  115. } else if (state is ActionUiFailure) {
  116. LoadingDialog.showLoadingDialog(context);
  117. }
  118. },
  119. builder: (context, state) {
  120. return Column(
  121. children: [
  122. Padding(
  123. padding: const EdgeInsets.all(8.0),
  124. child: Column(
  125. children: <Widget>[
  126. Container(
  127. width: double.infinity,
  128. child: Text(
  129. "Ngày thực hiện *",
  130. style: TextStyle(
  131. color: Colors.black54,
  132. fontSize: 13.0),
  133. ),
  134. ),
  135. _btnExecuteTimePicker(),
  136. SizedBox(
  137. height: 8.0,
  138. ),
  139. _executeByField(),
  140. SizedBox(
  141. height: 8.0,
  142. ),
  143. ],
  144. ),
  145. ),
  146. Container(
  147. width: double.infinity,
  148. height: 16,
  149. color: Colors.grey[200],
  150. ),
  151. BlocBuilder<MediaHelperBloc, MediaHelperState>(
  152. builder: (context, state) {
  153. if (state is MediaHelperSuccess) {
  154. return WidgetMediaPicker(
  155. currentItems: state.items,
  156. onChangeFiles: (newPathFiles,
  157. deletePathFiles) async {
  158. Get.find<ChangeFileController>()
  159. .change(newPathFiles,
  160. deletePathFiles);
  161. });
  162. } else {
  163. return Center(
  164. child: CircularProgressIndicator());
  165. }
  166. }),
  167. Padding(
  168. padding: const EdgeInsets.all(8.0),
  169. child: ButtonWidget(
  170. title: 'CẬP NHẬT',
  171. onPressed: () {
  172. FocusScopeNode currentFocus =
  173. FocusScope.of(context);
  174. if (!currentFocus.hasPrimaryFocus) {
  175. currentFocus.unfocus();
  176. }
  177. _validateInputs();
  178. }),
  179. ),
  180. ],
  181. );
  182. },
  183. ),
  184. ),
  185. )),
  186. ))));
  187. @override
  188. void dispose() {
  189. _executeByController.dispose();
  190. super.dispose();
  191. }
  192. }